porsager / porsager/postgres

PGPASSFILE not working in Node.js

Open
#964 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the connection from test.mjs with the provided .env file and the node --env-file=.env test.mjs command. Trace how postgres() obtains connection settings and handles PGPASSFILE; done means the script uses the .pgpass credentials instead of falling back to 127.0.0.1:5432.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, postgres
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.