Skip to content

no pg_hba.conf entry for host: why PostgreSQL rejects you

PostgreSQLtested on PostgreSQL 18.7

The server is reachable, but no line in pg_hba.conf matches the connection. Every line names type, database, role, address and method – and the message names exactly these fields. If no line matches, the connection ends before a password is even checked.

Symptom

psql -h 172.16.0.182 -U notes_app -d postgres -c 'SELECT 1;'
psql: error: connection to server at "172.16.0.182", port 5432 failed: FATAL:  no
pg_hba.conf entry for host "172.16.0.123", user "notes_app", database "postgres",
SSL encryption

The role may connect to the database notes, not to postgres. The message lists what the server saw: client address, role, database and whether the connection was encrypted.

Diagnosis

Read the rules in effect from pg_hba_file_rules and compare them with the fields in the message.

psql -c "SELECT line_number AS nr, database, user_name, address, auth_method
  FROM pg_hba_file_rules WHERE type = 'host';"
 nr  |   database    |  user_name  |  address   |  auth_method
-----+---------------+-------------+------------+---------------
 125 | {all}         | {all}       | 127.0.0.1  | scram-sha-256
 127 | {all}         | {all}       | ::1        | scram-sha-256
 131 | {replication} | {all}       | 127.0.0.1  | scram-sha-256
 132 | {replication} | {all}       | ::1        | scram-sha-256
 133 | {notes}       | {notes_app} | 172.16.0.0 | scram-sha-256
(5 rows)

Causes

Combination missingRole, database or network are not allowed

The rule lets exactly one role into exactly one database, and only from the lab network. pg_hba.conf rejects every other combination.

psql -h 172.16.0.182 -U postgres -d notes -c 'SELECT 1;'
psql: error: connection to server at "172.16.0.182", port 5432 failed: FATAL:  no
pg_hba.conf entry for host "172.16.0.123", user "postgres", database "notes", ...

no encryptionhostssl requires TLS, the client comes without

hostssl instead of host enforces TLS on the server side. A client with sslmode=disable then matches no line at all – the message ends in "no encryption".

sudo sed -i 's/^host  notes  tag03_report /hostssl  notes  tag03_report /' $H
psql "$C sslmode=disable" -Atc 'SELECT 1'
psql: error: connection to server at "172.16.0.182", port 5432 failed: FATAL:  no
pg_hba.conf entry for host "172.16.0.123", user "tag03_report", database "notes",
no encryption

not reloadedNew line not loaded or broken

A change to pg_hba.conf only takes effect after a reload. If the file contains an error, the server doesn't load it at all and keeps the old version. pg_hba_file_rules reads the file fresh and shows errors before the reload.

psql -c "SELECT line_number AS nr, error FROM pg_hba_file_rules
  WHERE error IS NOT NULL;"
 nr  |                     error
-----+-----------------------------------------------
 135 | invalid authentication method "scram-sha-265"
(1 row)
... LOG:  /etc/postgresql/18/main/pg_hba.conf was not reloaded

Fix

Add a matching line – as narrow as possible, with its own role, database and network per application. Then check pg_hba_file_rules for errors and reload; no restart is needed.
echo 'host  notes  notes_app  172.16.0.0/24  scram-sha-256' \
  | sudo tee -a /etc/postgresql/18/main/pg_hba.conf
psql -Atc 'SELECT count(*) FROM pg_hba_file_rules WHERE error IS NOT NULL' \
  -c 'SELECT pg_reload_conf()'
0
t

Don't: Adding a rule like "all all 0.0.0.0/0". Keep pg_hba.conf as narrow as possible: one role and database per application, only its network.

▶
Break it yourself and find it – free
PostgreSQL hands-on, day 3: configuration and access – reading and changing pg_hba.conf, SCRAM instead of MD5 and TLS with your own CA.
Go to day 3 →

FAQ

Why does the rejection show up twice?

With the default sslmode=prefer, psql tries again without encryption after the TLS attempt. With sslmode=require it shows up only once.

What is the difference to "pg_hba.conf rejects connection"?

There a reject line matched. The first matching line decides – lines below it are never reached.

Reload or restart?

For pg_hba.conf a reload is enough (SELECT pg_reload_conf()). listen_addresses, on the other hand, needs a restart.

Related error patterns

More