Sub topic 4
Connection types: local, host, hostssl and hostnossl
This chapter assumes you can read a basic pg_hba.conf record (type, database, user, address, method) and that you know CIDR notation. The first field of every record is the connection type. It decides which kinds of connections the rest of the record can match, so a wrong type is a common reason a rule never applies.
What the type field tests
PostgreSQL clients reach the server over one of two transports:
- A Unix-domain socket, which is a file on the server's own filesystem. Only processes on the same machine can use it.
- A TCP/IP socket, which can come from the same machine (
127.0.0.1,::1) or from the network.
The type field also records whether a TCP connection is encrypted. The server learns this during connection setup, before it reads the user name or database. It then compares the connection against each record's type, in file order.
| Type | Matches | Address field |
|---|---|---|
| `local` | Unix-domain socket connections | Omitted |
| `host` | Any TCP/IP connection, encrypted or not | Required |
| `hostssl` | TCP/IP connections that use TLS | Required |
| `hostnossl` | TCP/IP connections that do not use TLS | Required |
| `hostgssenc` | TCP/IP connections that use GSSAPI encryption | Required |
| `hostnogssenc` | TCP/IP connections that do not use GSSAPI encryption | Required |
This chapter covers the first four. The two gssenc types work the same way as hostssl and hostnossl, but for GSSAPI (Kerberos) encryption. They exist in PostgreSQL 12 and later.
local
A local record matches connections made through a Unix-domain socket. It has no address field, because the client's location is the machine itself:
# TYPE DATABASE USER METHOD
local all postgres peer
local all all scram-sha-256On Linux, psql uses the socket by default when you give no -h option. The socket file lives in the directory named by unix_socket_directories, often /var/run/postgresql. You can ask for it explicitly by passing a directory as the host:
psql -h /var/run/postgresql -d appdb -U app_userTwo points often cause confusion:
psql -h localhostis a TCP connection to127.0.0.1or::1. It matcheshostrecords, notlocalones.- The
peermethod works only withlocalrecords. It reads the connecting process's operating system user name from the kernel. Apeerrule on ahostline is invalid.
You can check which transport a session used. For a socket connection, inet_client_addr() returns NULL:
SELECT inet_client_addr() AS client_addr,
inet_server_addr() AS server_addr;If both columns are NULL, the session came through a Unix socket and local records applied.
host
A host record matches any TCP/IP connection, with or without TLS. It needs an address field, which can take these forms:
- A CIDR range such as
10.0.0.0/24or2001:db8::/32. - An IP address plus a mask in a separate column, such as
10.0.0.0 255.255.255.0. - A host name such as
app.example.com. The server resolves it and compares addresses, which is slower and depends on DNS. A name that starts with a dot matches a domain suffix. - A keyword:
all,samehost(any address of the server itself) orsamenet(any address in a subnet the server is directly connected to).
Use host when the rule should apply whether or not the client uses TLS. Because it says nothing about encryption, a host rule is the least specific choice for a remote client. If it uses a password method, the SCRAM exchange keeps the password off the wire. The queries and results that follow are still sent unencrypted unless TLS is active.
The server must also be listening on a network interface at all. listen_addresses defaults to localhost, so a host rule for 10.0.0.0/24 has no effect until the server listens on an address that clients in that range can reach.
hostssl
A hostssl record matches only TCP connections that negotiated TLS. Use it to say "this client may connect, but only encrypted":
hostssl appdb app_user 10.0.0.0/24 scram-sha-256Requirements and behavior:
- The server needs TLS configured:
ssl = on, plusssl_cert_fileandssl_key_file. Ifsslis off, ahostsslrecord can never match. The server reports this when it reads the file, andpg_hba_file_rulesshows it. - The
certmethod (client certificate authentication) works only onhostsslrecords. - The client chooses whether to attempt TLS. With libpq's default
sslmode=prefer, the client tries TLS first and falls back to plain text if the server does not offer it. If you want to guarantee encryption, the server must refuse plain text throughhostsslrules and arejectfor everything else. A client setting is not enough.
You can confirm what a session negotiated with the pg_stat_ssl view:
SELECT ssl, version, cipher
FROM pg_stat_ssl
WHERE pid = pg_backend_pid();For a socket connection, ssl is false. A TCP connection that used TLS shows ssl = true and a protocol such as TLSv1.3.
hostnossl
A hostnossl record is the mirror image. It matches only TCP connections that do not use TLS. This type is mostly useful for refusing unencrypted remote connections with a clear rule:
hostnossl all all 0.0.0.0/0 rejectIt can also allow plain-text traffic from a path you trust, such as a loopback address or a private link that you protect another way. Put these rules where their intent is obvious, because someone later reading host rules may not notice that a hostnossl line above them already captured the non-TLS traffic.
Combining types in one file
Here is a small file that uses every type in this chapter:
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all scram-sha-256
host all all 127.0.0.1/32 scram-sha-256
host all all ::1/128 scram-sha-256
hostssl appdb app_user 10.0.0.0/24 scram-sha-256
hostnossl all all 0.0.0.0/0 reject
host all all 0.0.0.0/0 rejectRecords are tried from top to bottom, and the first record that matches the connection decides the outcome. These are the results for different clients:
- A shell on the server running
psql -U postgresuses the socket and matches line 1. The method ispeer. - A client on
10.0.0.7using TLS asapp_userforappdbmatches line 5 and must pass SCRAM. - The same client without TLS fails line 5, because
hostssldoes not match. It reaches line 6 and is rejected. - A TLS client from
203.0.113.9fails line 5 on address, skips line 6 because it uses TLS, and is rejected by line 7.
Line 7 is a catch-all that also covers the TLS case from outside the permitted range. Lines 6 and 7 together reject every other TCP connection. They could be written as line 7 alone, because host matches both encrypted and unencrypted connections. Keeping line 6 only makes the non-TLS refusal visible in the file.
Unmatched connections are already denied. If no record matches, PostgreSQL refuses the connection. An explicit reject is still useful because it states the intent.
Common mistakes
- Using
hostwherelocalwas meant. If an application connects with-h localhost, thelocalrules never apply to it. - Placing a broad
hostorrejectrecord above a more specifichostsslrecord. The first match wins, so the specific record is never reached. - Writing a
hostsslrecord on a server withssl = off. The record cannot match, and the clients you meant to admit are refused. - Forgetting the address column on a
host-type record. The line is a syntax error, and the server ignores it. - Expecting
localrules to restrict network clients. They apply only to socket connections.
When a connection fails, the server's error message reports the client address, user, database and, depending on version, whether encryption was in use. That detail tells you which type the server saw, so compare it with your records.
Exercise
A server has these two records in this order: host all all 10.0.0.0/24 scram-sha-256 and then hostnossl all all 10.0.0.0/24 reject. A client at 10.0.0.5 connects without TLS. Which record applies, and what would you change to refuse unencrypted connections from that range?
Show answer
The first record applies. host matches both encrypted and unencrypted TCP connections, and the first match wins, so the client is offered SCRAM and the hostnossl reject line is never reached. To refuse unencrypted connections, move the hostnossl ... reject record above the host record, or replace the host record with hostssl.