What the message looks like
psql: error: connection to server at "db.example.com" (203.0.113.20), port 5432 failed: FATAL: no pg_hba.conf entry for host "198.51.100.7", user "appuser", database "appdb", SSL encryptionCommon causes
- No
hostline exists for the client address, or the address range is too narrow or written wrongly. - The user or database does not match the line, for example the line is for
allusers but a different database. - The client uses SSL but the line is
hostnossl, or the other way round withhostssl. - The file was changed but PostgreSQL was not reloaded.
- The client comes from another address than you expect, for example a Docker network, a NAT or a proxy.
Check
- Which rules does the server really use, in order? Also shows errors in the file
sudo -u postgres psql -c 'SELECT line_number, type, database, user_name, address, auth_method, error FROM pg_hba_file_rules' - On which addresses does the server listen?
sudo -u postgres psql -c 'SHOW listen_addresses' - Is the port open on the server?
sudo ss -tlnp | grep 5432 - The address the server saw: it is in the error text. Also in the log
sudo tail -n 20 /var/log/postgresql/postgresql-*-main.log
Fix
- Take the address, user, database and SSL mode from the error text. They are exactly what the server saw.
- Add one narrow line to
pg_hba.conf, for examplehostssl appdb appuser 198.51.100.7/32 scram-sha-256. Use the real client address or a small range. - Reload:
sudo systemctl reload postgresql, then check the rule list again. - Do not use
0.0.0.0/0withtrust. That lets anyone on the network in without a password. - If the client is not reachable at all (connection refused),
listen_addressesor the firewall is the next place to look.
Typical scenario
You move the application to its own VM while the database stays on the old server. At the first start the log says "no pg_hba.conf entry for host 198.51.100.7, user app, database appdb, no encryption". The server is reachable, but its access file does not know this combination. The "no encryption" at the end means the client came without SSL; check whether pg_hba.conf has only hostssl lines.
FAQ
Do I need to restart PostgreSQL?
No. A reload is enough for pg_hba.conf: systemctl reload postgresql or SELECT pg_reload_conf();.
The client address in the error is not mine.
Then a proxy, NAT or the Docker bridge changes the source address. Write the rule for the address the server reports, or fix the network path.
What does "no encryption" in the message mean?
The client opened the connection without SSL. If there are only hostssl lines for this address, none matches and the server refuses. Either the client connects with sslmode=require, or you add a suitable host line, which is not recommended for external connections.
Is the line in pg_hba.conf enough without listen_addresses?
No. If the server listens only on localhost, the client never reaches the file check and gets "Connection refused" or a timeout. Conversely, the message "no pg_hba.conf entry" shows the connection is already up. For remote access you need listen_addresses, the firewall and the line.
How do I check whether PostgreSQL reads my pg_hba.conf correctly?
With SELECT * FROM pg_hba_file_rules; (from PostgreSQL 10), as an administrator. The view lists every line of the file with type, address, method and an error field for syntax errors. Do not forget the order: the first matching line applies, even if a better one follows below.