Safeguard
Security Guides•

How does pg_hba.conf decide who can connect to PostgreSQL?

PostgreSQL uses the first pg_hba.conf record that matches the connection and never falls through. Order rules carefully and check listen_addresses too.

Safeguard Security Team
3 min read

PostgreSQL reads pg_hba.conf from the top and uses the first record that matches the connection type, client address, database and user. There is no fall through. If that record rejects the login, later records are never tried. If nothing matches, access is denied.

What a record contains

Each line names a connection type, a client address range where relevant, a database, a user and an authentication method. The documentation describes the types you will see most:

TypeMatches
localUnix socket connections
hostTCP connections, with or without SSL
hostsslTCP connections made with SSL only
hostnosslTCP connections made without SSL

The documentation also notes that a host record matches SSL and non SSL attempts alike. If you want encryption to be mandatory, use hostssl and make sure no broader host line sits above it.

The two gates

Two separate settings control who can reach the server. The first is network reach. The PostgreSQL docs state that remote TCP connections are not possible unless listen_addresses has an appropriate value, because the default is to listen only on the local loopback address. The second is pg_hba.conf, which decides who may authenticate once the network allows the packet through.

Both gates matter. A permissive pg_hba.conf on a server that only listens on loopback is not reachable from outside. A server bound to every interface with a broad rule is.

Why order is the whole game

Because the first match wins, a wide rule placed early can swallow narrower ones below it. Consider two lines in this order:

host    all    all    10.0.0.0/8    scram-sha-256
host    billing    app_runtime    10.0.5.0/24    reject

The second line is dead. Every connection from the 10.0.5.0 range already matched the first line. Put the specific rule first, then the general one.

A review routine

  1. List the records in the order PostgreSQL reads them.
  2. For each, ask which connections it catches and whether a line above already catches them.
  3. Look for broad address ranges with all as the database or user.
  4. Look for hostssl versus host, and decide whether plain TCP should exist at all.
  5. Confirm the authentication method is scram-sha-256 and not trust. See what trust means in pg_hba.conf.
  6. Check listen_addresses against what you expect.
  7. Keep the file in version control and review changes like code.

The documentation also describes a reject method, useful for filtering out specific hosts from a group, with a later line allowing the rest of a network. That pattern only works when the reject line comes first.

Managed databases

Hosted services may not expose this file at all. Check your provider's documentation for how network and authentication rules are set, and whether rule order matters there.

For connection encryption, see sslmode require versus verify-full.

Safeguard scans dependencies and infrastructure code in your repositories, and policy gates can hold a pull request that changes access rules. A free scan is at gold.safeguard.sh/scan.

Quick check

What happens if no pg_hba.conf record matches?

Access is denied.

Does a later record act as a backup if the first fails?

No. The docs say there is no fall through or backup. The first matching record decides.

Is pg_hba.conf enough to hide the server from the internet?

No. Also check listen_addresses and your firewall. They are separate controls.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.