PostgreSQL "no pg_hba.conf entry for host": allow this connection, the safe way
Fix PostgreSQL "FATAL: no pg_hba.conf entry for host": read the host, user and SSL in the message, add a narrow rule to pg_hba.conf, reload it, and handle Docker and SSL.
by OhSudo ··
Not sure this is your cause?Find it step by step: Can't connect to the database →
Part of a learning path:SQL and PostgreSQL, step by step
FATAL: no pg_hba.conf entry for host "10.0.0.20", user "app", database "shop", no encryptionIt sounds alarming, and it’s actually helpful. You reached PostgreSQL, so the network, the port and listen_addresses are all fine. It’s refusing because no line in its rules file, pg_hba.conf, covers this combination. The message tells you the exact four values it looked for.
- The connectionhost 10.0.0.20 · user app · database shop · no encryption(works)compared with each line
- pg_hba.confread from the top: TYPE, DATABASE, USER, ADDRESSnothing matches(fails)
- No line matchesFATAL: no pg_hba.conf entry for host(breaks here)
TL;DR
Add a line to pg_hba.conf, matching the values in the message:
# TYPE DATABASE USER ADDRESS METHODhost shop app 10.0.0.20/32 scram-sha-256Then reload:
sudo -u postgres psql -c 'SELECT pg_reload_conf();'1. Read the message
Each part of it maps to a column:
| In the message | Column | Here |
|---|---|---|
host "10.0.0.20" |
ADDRESS |
Where you’re connecting from, as the server sees it |
user "app" |
USER |
The database user |
database "shop" |
DATABASE |
The database |
no encryption or SSL encryption |
TYPE |
Whether the connection uses TLS |
The address is worth a second look: behind a NAT, a proxy or Docker, it’s not the one you’d guess.
2. Find and edit the file
sudo -u postgres psql -c 'SHOW hba_file;'It’s usually /etc/postgresql/18/main/pg_hba.conf on Ubuntu and Debian, and /var/lib/pgsql/18/data/pg_hba.conf on RHEL. Add your rule:
# TYPE DATABASE USER ADDRESS METHODhost shop app 10.0.0.20/32 scram-sha-256host shop app 10.0.1.0/24 scram-sha-256/32 means one address, and /24 a whole subnet of 256. The Subnet Calculator works these out.
The order matters. PostgreSQL uses the first line that matches, and doesn’t read further. A broad reject line above yours wins, so put yours before it.
Reload, which doesn’t drop anyone’s connection:
sudo systemctl reload postgresqlCheck it, as a typo makes a line invalid without much noise:
The error column shows lines PostgreSQL couldn’t read.
3. The type: SSL or not
FATAL: no pg_hba.conf entry for host "10.0.0.20", user "app", database "shop", no encryptionThe type column decides which connections a line covers:
| Type | Covers |
|---|---|
local |
The Unix socket, with no network |
host |
TCP, with or without TLS |
hostssl |
TCP with TLS only |
hostnossl |
TCP without TLS only |
So if only hostssl lines exist, then the fix is on the client, which should use TLS, rather than a new rule. This is the usual case on managed services, where you can’t edit the file anyway:
psql "host=10.0.0.5 dbname=shop user=app sslmode=require"In a connection string, that’s postgresql://app:pass@10.0.0.5/shop?sslmode=require. On Amazon RDS, rds.force_ssl does this, and its message says no encryption. On RDS the file is managed for you, and you control access with security groups and parameter groups.
4. Other values that don’t match
host "::1": an IPv6 connection tolocalhost. You need a line for::1/128, or connect to127.0.0.1instead.- A name in place of an address: use the address, or a domain with a leading dot such as
.example.com, which needs working reverse DNS and is slower. database "replication": that’s a special word for replicas, and it needs its own line.alldoesn’t include it.
5. Docker
Containers come from a changing address on a Docker network, often 172.18.0.x. The official image allows others by default with a line like this one:
host all all all scram-sha-256So if you see this error there, your data folder has an older or edited file. Don’t guess: docker exec db cat /var/lib/postgresql/data/pg_hba.conf. On version 18 images, the data folder moved, so use docker exec db psql -U postgres -c 'SHOW hba_file;' to check. Then allow the Docker network as narrowly as you can. The file lives in the volume, so it survives recreating the container, and POSTGRES_HOST_AUTH_METHOD only applies on first start.
6. After you’ve fixed it
A different message means you’ve moved forward:
password authentication failed for user: a rule matches now, and the password’s wrong.Peer authentication failed: apeerrule matched.Connection refusedmeans this wasn’t really your problem: see PostgreSQL connection refused.
Set a strong password with the Password Generator, and check the Port Lookup for why 5432 shouldn’t be public.
Warning
Avoid the first answer you’ll find online: host all all 0.0.0.0/0 trust. trust means no password at all, for any user, from anywhere. Even with scram-sha-256, 0.0.0.0/0 lets the whole internet try to log in. List only the addresses that need access, and keep a firewall in front.
Shell, we have a problem!
Similar, but different: Connection refused means you never reached PostgreSQL, so pg_hba.conf isn’t involved yet. pg_hba.conf rejects connection means a line matched, with the method reject. FATAL: sorry, too many clients already is about connection limits.
Found this useful? Share it.
Found a mistake? Let us know at contact@ohsudo.com.