Skip to content
ohsudo, Home
Databases

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.

Intermediate4 min readApplies to PostgreSQL 13–18 · Ubuntu · Debian · RHEL / Rocky / AlmaLinux · Docker · Amazon RDS

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 encryption

It 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.

  1. The connectionhost 10.0.0.20 · user app · database shop · no encryption(works)
  2. pg_hba.confread from the top: TYPE, DATABASE, USER, ADDRESS
  3. No line matchesFATAL: no pg_hba.conf entry for host(breaks here)
PostgreSQL reads pg_hba.conf from the top, and the first line that matches the connection decides. When no line matches, it refuses with this message.

TL;DR

Add a line to pg_hba.conf, matching the values in the message:

# TYPE DATABASE USER ADDRESS METHOD
host shop app 10.0.0.20/32 scram-sha-256

Then reload:

Terminal window
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

Terminal window
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 METHOD
host shop app 10.0.0.20/32 scram-sha-256
host 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:

Terminal window
sudo systemctl reload postgresql

Check it, as a typo makes a line invalid without much noise:

SELECT line_number, type, database, user_name, address, auth_method, error
FROM pg_hba_file_rules;

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 encryption

The 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:

Terminal window
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 to localhost. You need a line for ::1/128, or connect to 127.0.0.1 instead.
  • 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. all doesn’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-256

So 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:

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.

Tools

Try it yourself

Articles

Related articles