Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Node.js gets 'no pg_hba.conf entry for host' from PostgreSQL

Errorerror: no pg_hba.conf entry for host

Short answer

The server found no pg_hba.conf rule matching your connection type, address, database and user, so it denied access. On hosted Postgres such as Heroku the usual cause is a client connecting without SSL. On a server you run, add a host line for the client address and reload.

  • Applies to PostgreSQL 16, node-postgres 8, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the full error. It names the host, user and database the server saw, and says whether SSL was on. Those four values are exactly what a rule must match.

    pool.query('select 1').catch(err => console.error(err.message));
  2. 2Match what you see, then jump to its fix

    1. 1The host is a managed provider such as Heroku, and the error mentions the encryption state of the connectionA hosted database requires SSL and your client connects without itFix
    2. 2The error names an IP address that is not in any host lineNo rule in pg_hba.conf covers your client addressFix
    3. 3Only hostssl lines exist, and the client connects without SSLA hostssl-only rule rejects your non-SSL connectionFix

What does the error look like?

  • error: no pg_hba.conf entry for host

Cause 1 of 3

A hosted database requires SSL and your client connects without it

1Check

Heroku Postgres requires SSL connections. Some clients connect over SSL by default, and some need sslmode=require or an ssl option.

echo "$DATABASE_URL"

2Fix

Turn SSL on in node-postgres. Heroku's own Node.js example passes ssl with rejectUnauthorized false, or you can set the PGSSLMODE config var.

const { Pool } = require('pg');
const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
  ssl: { rejectUnauthorized: false },
});
// or: heroku config:set PGSSLMODE=no-verify

3Verify

The query succeeds.

node -e "require('./db').query('select 1').then(r=>console.log(r.rows))"

Confirmed by devcenter.heroku.com

Did this fix it?

Cause 2 of 3

No rule in pg_hba.conf covers your client address

1Check

The first rule matching connection type, client address, database and user is used. If no rule matches, access is denied. Remote clients also need listen_addresses to include the server's interface.

psql -c "SELECT line_number, type, database, user_name, address, auth_method FROM pg_hba_file_rules;"
psql -c "SHOW listen_addresses;"

2Fix

Add a host line for the client's address, using the narrowest range that works, and set listen_addresses if remote connections are needed.

# pg_hba.conf
# TYPE  DATABASE  USER     ADDRESS           METHOD
host    mydb      myuser   203.0.113.10/32   scram-sha-256

3Verify

Reload the configuration and connect again.

psql -c "SELECT pg_reload_conf();"

Confirmed by postgresql.org

Did this fix it?

Cause 3 of 3

A hostssl-only rule rejects your non-SSL connection

1Check

A hostssl record matches TCP connections made with SSL only. A host record matches SSL and non-SSL ones. A client using sslmode prefer may connect without SSL.

psql -c "SELECT type, address FROM pg_hba_file_rules;"

2Fix

Make the client use SSL with sslmode=require in the connection string, or keep SSL on and verify properly with verify-full.

DATABASE_URL=postgres://user:password@host:5432/db?sslmode=require

3Verify

The same query now connects.

psql "$DATABASE_URL" -c "select 1"

Confirmed by postgresql.org

Did this fix it?

How do I stop it coming back?

  • Keep pg_hba.conf rules narrow: one address and one database per line.
  • Query pg_hba_file_rules after each edit; rows with an error value point to a bad line.
  • Use sslmode=verify-full with a trusted CA where the provider supports it, instead of disabling verification.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.