bun icon indicating copy to clipboard operation
bun copied to clipboard

ECONNREFUSED: Failed to connect when using `node-postgres` and `postgres` packages

Open danielkaczmarczyk opened this issue 1 year ago • 2 comments
trafficstars

What version of Bun is running?

1.1.8+89d25807f

What platform is your computer?

Darwin 23.4.0 arm64 arm

What steps can reproduce the bug?

Create two files that attempt to connect to a pg db:

When using the postgres package:

const sql = postgres({
  // config vars here
});
const users = await sql`select * from users;`

When using node-pg:


const { Pool } = pg;
const pool = new Pool({ max: 10 });

const makeDbClientQuery = (query, client) => {
  return client.query(query[0], query[1]);
};

const extractRows = (response) => response.rows;

export const exec = async (queries) => {
  const client = await pool.connect();

  try {
    await client.query("begin");
    const responses = await Promise.all(queries.map(query => makeDbClientQuery(query, client)));
    await client.query("commit");
    return responses.map(extractRows);
  } catch (e) {
    await client.query('rollback')
    throw e;
  } finally {
    client.release();
  }
}

const res = await exec([['select * from users;', '']]);
pool.end();
console.log(res)```

## Both libraries raise the following error:

```ECONNREFUSED: Failed to connect
 syscall: "connect"

running the same code with node does not raise an error.

What is the expected behavior?

Successful connection and query execution.

What do you see instead?

 syscall: "connect"

Additional information

No response

danielkaczmarczyk avatar May 20 '24 19:05 danielkaczmarczyk

Can you share anything about your connections details? e.g.

  • are you connecting to localhost?
  • using TLS/SSL?

Electroid avatar May 20 '24 20:05 Electroid

Thanks @Electroid for your response.

I am doing both in a way:

  • the postgres server I am trying to connect to is in GCP, and has TSL/SSL enabled
  • I don't directly connect to it, I am running a official GCP proxy on localhost to access the databases in GCP

edit:

here's the exact proxy image: https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.11.0/cloud-sql-proxy.darwin.arm64

danielkaczmarczyk avatar May 22 '24 19:05 danielkaczmarczyk