Your app, script or database client tries to connect to PostgreSQL and gets something like this:
FATAL: no pg_hba.conf entry for host "203.0.113.25", user "app", database "appdb", SSL offThe good news is in the wording: the server is running, it's reachable, and it answered. It just has no rule that lets this particular connection in, so it refused before even asking for a password. The fix is a rule in a file called pg_hba.conf, or a change on the client side so the connection matches a rule that already exists.
What pg_hba.conf does
pg_hba.conf ("host-based authentication") is PostgreSQL's list of who may connect, from where, and how they prove who they are. Each line has the same columns:
# TYPE DATABASE USER ADDRESS METHOD
host appdb app 203.0.113.25/32 scram-sha-256| Column | What it matches |
|---|---|
| TYPE | local (a Unix socket on the same machine), host (TCP, with or without SSL), hostssl (TCP with SSL only) or hostnossl (TCP without SSL only) |
| DATABASE | A database name, or all |
| USER | A role name, or all |
| ADDRESS | The client's IP address range, such as 203.0.113.25/32 for one address or 10.0.0.0/8 for a network |
| METHOD | How the client proves itself: scram-sha-256 (a password; PostgreSQL's docs call it the most secure of the password methods), md5 (older), peer, reject and others |
PostgreSQL reads the file from the top and uses the first line whose type, database, user and address all match. If none match, you get this error.
Read the message first
Every part of the error is a clue:
host "203.0.113.25"is the address the server saw. It's often not the one you expect: a home router, a cloud NAT gateway, a Docker network (172.17.x.xis Docker's usual default), or IPv6 (::1instead of127.0.0.1for a local connection).useranddatabaseare what the client asked for. A typo in the connection string shows up here.- The end of the line says whether the connection used SSL, for example
SSL offor, in newer versions, wording likeno encryption. This matters when the only matching rule is ahostsslline.
Step 1: Find the right pg_hba.conf
A server can have several PostgreSQL versions or clusters installed, each with its own file. Ask the running server which one it uses. On the database server:
sudo -u postgres psql -c "SHOW hba_file;"On Debian and Ubuntu packages it's usually under /etc/postgresql/<version>/main/; on many other installs it sits in the data directory. Edit the file that command names, not one you found by searching.
Step 2: Add a line for the connection
Add a line that matches what the error reported, as narrow as you can make it: one database, one user, the smallest address range that covers your client. For the error above:
host appdb app 203.0.113.25/32 scram-sha-256If you want this connection to require SSL, use hostssl instead of host. Put the new line above any broader line that would otherwise match first, such as a catch-all reject line at the end.
Step 3: Reload the configuration
PostgreSQL reads pg_hba.conf when it starts and when it's told to reload. A reload doesn't disconnect anyone. From psql as a superuser:
SELECT pg_reload_conf();Or from the shell, on a systemd install: sudo systemctl reload postgresql. Then check that the file loaded without mistakes; this lists any line PostgreSQL couldn't read:
SELECT line_number, error FROM pg_hba_file_rules WHERE error IS NOT NULL;No rows means every line was understood. Then try connecting again.
The usual causes
An SSL mismatch
If the only rule that fits your client is a hostssl line and the message ends with SSL off (or no encryption), the client connected without SSL. Turn SSL on in the client, usually by adding sslmode=require to the connection string:
postgresql://app:password@db.example.com:5432/appdb?sslmode=requireThis is the most common cause on managed databases, which often only accept SSL connections.
The client's address isn't what you think
The rule says 10.0.0.0/8 but the server sees the request coming from a public address, a NAT gateway or a Docker bridge. Use the exact address from the error message to write the rule, and remember that it can change if your client's network changes.
The rule names a different database or user
A rule for database app doesn't match a connection to appdb. Compare the names in the rule with the ones in the error, letter for letter.
You can't edit pg_hba.conf at all
Managed services (Amazon RDS, Google Cloud SQL, Azure, Supabase, Neon and others) don't let you edit the file. There the fix is on the client side, usually turning on SSL as above, or in the provider's own network or access settings, such as allowing your IP address. Their documentation lists the settings that apply.
A different error means a different problem
Connection refusedor a timeout: the server never answered, sopg_hba.confwasn't involved yet. Check that PostgreSQL is running, thatlisten_addressesinpostgresql.confincludes the address you connect to (it'slocalhostonly by default), and that a firewall allows port 5432.password authentication failed for user: a rule matched and the password was wrong. The fix is the password, notpg_hba.conf. If the password is inside a connection URL, characters like@or#must be percent-encoded; the connection string checker shows the encoded version.- Too many connections or remaining connection slots are reserved: the server is full. See Too many connections, or for the reserved-slots message, remaining connection slots are reserved.