Sub topic 3
Anatomy of a pg_hba.conf Record
Assume you know basic SQL and psql, understand PostgreSQL roles and databases, and can read IP addresses in CIDR notation. This chapter focuses on how to read one pg_hba.conf record. PostgreSQL uses these records to decide how a client must authenticate before it can connect.
The record layout
A non-comment record normally has this structure:
type database user address auth-method [options]Whitespace separates fields. You can use spaces or tabs. A # starts a comment, and blank lines are ignored.
The address field is omitted for local records. Therefore, the two main layouts are:
local database user auth-method [options]
host database user address auth-method [options]Here is a small example:
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all scram-sha-256
host appdb app_user 10.0.0.0/24 scram-sha-256
hostssl appdb app_user 10.0.0.0/24 scram-sha-256
host all all 0.0.0.0/0 rejectReading each field
Type
The type field describes how the client connects:
localmatches a Unix-domain socket connection on the database server.hostmatches TCP/IP connections, whether or not they use TLS.hostsslmatches TCP/IP connections that use TLS.hostnosslmatches TCP/IP connections that do not use TLS.
A hostssl record does not enable TLS by itself. The server must also have SSL configured and enabled. The client must successfully negotiate TLS before this record can match.
Database
The database field limits the rule to one or more databases. For example, appdb matches only that database, while all matches every normal database.
The special value replication matches physical replication connections. It does not match an ordinary connection to a database named replication.
You can list several values with commas:
host appdb,reporting analyst 192.0.2.0/24 scram-sha-256User
The user field identifies the PostgreSQL role requested by the client. Use all for every role, or list several roles with commas.
A value beginning with + refers to members of a PostgreSQL role group. For example, +app_users matches roles that belong to the app_users group.
Address
The address field applies to network connections. CIDR notation defines the allowed client range:
192.0.2.10/32
192.0.2.0/24/32 identifies one IPv4 address. /24 identifies a larger subnet. Use ::1/128 for the IPv6 loopback address.
Do not use an address field on a local record. local connections use the operating system socket, not a network address.
Authentication method and options
The method specifies how PostgreSQL verifies the client. Common methods include:
peer, which checks the operating system user for a local socket connectionscram-sha-256, which performs password authentication using a SCRAM verifiercert, which authenticates with a client TLS certificatereject, which refuses the connection
Some methods accept options. For example, a certificate rule can include map=staffmap to use a name map from pg_ident.conf.
A practical reading method
For each incoming connection, ask:
- Is it a socket connection or TCP/IP?
- If it uses TCP/IP, does it use TLS?
- Which database is requested?
- Which PostgreSQL role is requested?
- Does the client address fit the CIDR range?
- What method and options does the first matching record require?
Exercise
Which record matches a TLS connection from 192.0.2.25 to appdb as app_user if it appears before all broader records?
Show answer
A record with type hostssl, database appdb, user app_user, and an address range containing 192.0.2.25 matches, provided its earlier fields also match.