postgres icon indicating copy to clipboard operation
postgres copied to clipboard

PGPASSFILE not working in Node.js

Open jcpc91 opened this issue 4 months ago • 0 comments

I created a .pgpass file in the user's home directory:

vi ~/.pgpass\

I added the connection details:

182.10.203.23:5432:database:username:verystrongpass\

I then tested the connection using psql and it successfully connects to the database using the credentials from the file:

psql

Next, I prepared the development environment:

  1. Created a .env file to set up the environment: NODE_ENV=development PGPASSFILE=/home/username/.pgpass

  2. Installed the postgres npm package:

npm install postgres

  1. Created a JavaScript file (test.mjs) to connect to the database:

import postgres from 'postgres' const sql = postgres() const data = await sqlselect * from mytable console.log(data)

4 Ran the script

node --env-file=.env test.mjs

I got the following error:

Error: connect ECONNREFUSED 127.0.0.1:5432

This indicates that it is trying to connect using the default localhost:5432 connection and not using the configuration from .pgpass.

How can I configure the connection to read from PGPASSFILE?

jcpc91 avatar Oct 18 '24 20:10 jcpc91