Node.js gets 'no pg_hba.conf entry for host' from PostgreSQL
error: no pg_hba.conf entry for hostShort 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.
Fix it fast
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));2Match what you see, then jump to its fix
- 1
The 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
The error names an IP address that is not in any host lineNo rule in pg_hba.conf covers your client addressFix - 3
Only hostssl lines exist, and the client connects without SSLA hostssl-only rule rejects your non-SSL connectionFix
- 1
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-verify3Verify
The query succeeds.
node -e "require('./db').query('select 1').then(r=>console.log(r.rows))"Confirmed by devcenter.heroku.com
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-2563Verify
Reload the configuration and connect again.
psql -c "SELECT pg_reload_conf();"Confirmed by postgresql.org
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=require3Verify
The same query now connects.
psql "$DATABASE_URL" -c "select 1"Confirmed by postgresql.org
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
Back end
Knex runs out of connections and the query waits until it times out
Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
Every query needs a free connection from Knex's pool.
3 causes, each with a fixBack end
Node.js cannot reach PostgreSQL: connect ETIMEDOUT in production
at TCPConnectWrap.afterConnect [as oncomplete]
ETIMEDOUT means the other side never answered the connection attempt.
3 causes, each with a fixBack end
node-postgres pool times out waiting for a client
Error: timeout exceeded when trying to connect
The pool has no free client to give you.
2 causes, each with a fixBack end
node-postgres: SCRAM-SERVER-FIRST-MESSAGE client password must be a string
SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a non-empty string
The server asked for SCRAM authentication and node-postgres had no string to hash.
4 causes, each with a fixBack end
PostgreSQL error: bind message has N parameter formats but 0 parameters
QueryFailedError: bind message has N parameter formats but 0 parameters
One query is carrying more bound values than the Postgres protocol can describe.
2 causes, each with a fixBack end
Prisma Client fails with Invalid prisma.user.findUnique() invocation
The table `{table}` does not exist in the current database.
The first line, Invalid prisma.user.findUnique() invocation, only says which call failed.
4 causes, each with a fix