Could not resolve "pg-native"

I’m using Workers to try to connect to a PostgreSQL database hosted in Azure.

The problem is every time, I try to run the code, I get this error:

You can mark the path “pg-native” as external to exclude it from the bundle, which will remove this error. You can also surround this “require” call with a try/catch block to handle this failure at run-time instead of bundle-time.

X [ERROR] Build failed with 1 error:

node_modules/pg/lib/native/client.js:4:21: ERROR: Could not resolve “pg-native”

I tried npm install pg-native many times, and it results with errors. From other thread I read, that pg-native can’t be bundled through esbuild. But in that case what’s the solution, if any, or any workaround?

Here’s my code:

const { Client } = require('pg');

addEventListener('fetch', event => {

async function handleRequest(request) {
  const client = new Client({
    user: 'XXX',
    host: 'XXX',
    database: ' XXX  ',
    password: ' XXX  ',
    port: 5432,

  await client.connect();

  const res = await client.query('SELECT * from  XXX ');

  await client.end();

  return new Response(res.rows[0].message);

Perhaps this post might help

Or this blog

1 Like

Hello @anon9246926,

Thanks for your answers, I already saw these methods, but I want to avoid adding another layer between the database and the Workers. I instinctively thought that connecting to a relational DB won’t be that of a challenge, but it doesn’t seem that easy from what I’m reading. Right now I’m trying to explore the CF D1 database, I will report my discoveries if it can help anyone.

1 Like