Sub topic 4
Connection Types: `local`, `host`, `hostssl`, and `hostnossl`
Assume you know basic SQL and psql, PostgreSQL roles, Linux configuration files, and IP address notation. This chapter focuses on how PostgreSQL classifies a connection before it applies an authentication method.
Why the connection type matters
PostgreSQL accepts connections through either a Unix-domain socket or TCP/IP:
localmatches Unix-domain socket connections on the database server.hostmatches TCP/IP connections whether or not they use TLS.hostsslmatches only TCP/IP connections protected by TLS.hostnosslmatches only TCP/IP connections without TLS.
TLS, or Transport Layer Security, encrypts traffic between the client and server. The connection type does not decide whether a user is allowed to connect by itself. It selects which pg_hba.conf records are eligible. The selected record then applies an authentication method such as scram-sha-256, peer, or cert.
How `psql` chooses a connection type
When you omit -h, psql normally connects through a local Unix-domain socket:
psql -d appdbThis connection can match a local record. When you provide a host name or IP address, psql uses TCP/IP:
psql -h 127.0.0.1 -d appdbThis connection can match host, hostssl, or hostnossl, depending on whether TLS is used.
The hostname localhost usually causes a TCP/IP connection, but client and operating-system configuration can affect address selection. Use an explicit IP address when testing a particular path.
Reading the address field
local records have no address field because Unix sockets are not selected by IP address. TCP/IP records do have an address field:
# TYPE DATABASE USER ADDRESS METHOD
local all all peer
host appdb app_user 127.0.0.1/32 scram-sha-256
hostssl appdb app_user 10.0.0.0/24 scram-sha-256
hostnossl appdb app_user 192.168.1.0/24 reject127.0.0.1/32 means exactly one IPv4 address. 10.0.0.0/24 covers addresses from 10.0.0.0 through 10.0.0.255. IPv6 addresses use IPv6 CIDR notation, such as ::1/128.
A hostssl record matches only when the server has TLS enabled and the client successfully negotiates TLS. A hostnossl record is useful when you want to reject or separately control unencrypted TCP/IP connections.
Choosing between `host`, `hostssl`, and `hostnossl`
Use hostssl for remote application traffic when credentials and data must be encrypted. Use hostnossl to reject accidental plaintext connections. A broad host record can unintentionally allow either encrypted or unencrypted traffic, so place it carefully or avoid it when TLS is required.
Visualizing the connection paths
Check the intended path
Test each path explicitly:
psql -d appdb
psql "host=127.0.0.1 dbname=appdb sslmode=disable"
psql "host=db.example.com dbname=appdb sslmode=require"For each test, PostgreSQL scans pg_hba.conf from top to bottom. The first record matching the connection type, database, user, and address controls the result.
Exercise
Which connection type can match psql -h 127.0.0.1 -d appdb when the client uses sslmode=disable: local, hostssl, hostnossl, or all three?
Show answer
hostnossl and host can match. local requires a Unix-domain socket, and hostssl requires TLS.