Sub topic 3
Anatomy of a pg_hba.conf record
This chapter assumes you can edit configuration files on a Linux server, use psql, and read CIDR notation such as 10.0.0.0/24. It also assumes you know the difference between a PostgreSQL role and a database. You should already know where pg_hba.conf lives and how to reload it. This chapter takes a single record apart field by field.
What a record is
pg_hba.conf is a plain text file. Each non-blank, non-comment line is one record, and each record is a rule that says: "if a connection looks like this, authenticate it this way." A record has a fixed set of fields separated by spaces or tabs.
# TYPE DATABASE USER ADDRESS METHOD [OPTIONS]
hostssl appdb app_user 10.0.0.0/24 scram-sha-256The general shapes are:
local database user auth-method [auth-options]
host database user address auth-method [auth-options]
hostssl database user address auth-method [auth-options]
hostnossl database user address auth-method [auth-options]Only local records omit the address field, because a Unix-domain socket has no IP address. Every other type needs one.
Lines that start with # are comments. A # also starts a comment in the middle of a line. A record cannot span lines, except that a line ending in a backslash continues on the next line.
The six positions
| Position | Field | What it matches |
|---|---|---|
| 1 | Connection type | How the client reached the server |
| 2 | Database | The database name in the connection request |
| 3 | User | The role name in the connection request |
| 4 | Address | The client's IP address or host name (not used by `local`) |
| 5 | Method | The authentication method to apply |
| 6 | Options | Optional `name=value` settings for that method |
Fields 1 to 4 are conditions. A record applies to a connection only if all of them match. Field 5 is the action, and field 6 refines the action.
The diagram below shows the flow. A connection request supplies four facts, and the record's conditions are checked against them.
Field 1: connection type
The type describes the transport. The next chapter covers the four types in detail. For now, remember the split:
localmatches Unix-domain socket connections.hostmatches TCP/IP connections, with or without TLS.hostsslmatches TCP/IP connections that use TLS.hostnosslmatches TCP/IP connections that do not use TLS.
There are also hostgssenc and hostnogssenc for GSSAPI-encrypted connections. They are rare and not used in this tutorial.
Field 2: database
The database field names the database the client asked for. It accepts:
- A database name, such as
appdb. - A comma-separated list of names, such as
appdb,reports. all, which matches any database.sameuser, which matches when the requested database has the same name as the requesting role.samerole, which matches when the requested role is a member of a role with the same name as the database.replication, which matches physical replication connections. It does not match a database called "replication". To match a database with that name, quote it as"replication".- A file reference, written as
@filename, which reads a list of names from a file in the same directory aspg_hba.conf. - A regular expression, if the item starts with
/, such as/^app_.*.
A note on all: it does not match replication connections. A replication connection needs an explicit replication record.
Field 3: user
The user field names the role the client is connecting as. It accepts:
- A role name, such as
app_user. - A comma-separated list.
all, which matches any role.- A name starting with
+, which matches any role that is a direct or indirect member of the named role.+analystsmatchesanalystsand every role granted membership in it. - A file reference with
@filename. - A regular expression starting with
/.
The + prefix is the practical way to manage rules by group. You grant membership in a role such as analysts with ordinary GRANT, and one record covers everyone in it.
# TYPE DATABASE USER ADDRESS METHOD
hostssl reports +analysts 10.1.0.0/16 scram-sha-256Quoting matters. A double-quoted name is taken literally, so "all" matches a role named all, not every role. Case rules follow SQL: unquoted names in the file are compared exactly as written, so write role names with the same case they have in the catalog.
Field 4: address
The address field applies to host, hostssl, and hostnossl records. It can be:
- An IPv4 or IPv6 address with a mask length in CIDR notation:
192.168.1.0/24,127.0.0.1/32,::1/128. - An IP address and a separate netmask column:
192.168.1.0 255.255.255.0. In that case the record has an extra column, so the method moves to position 6. all, which matches any address.samehost, which matches any address of the server itself.samenet, which matches any address in a subnet the server is directly connected to.- A host name, such as
db-client.example.com. A name starting with a dot, such as.example.com, matches any host in that domain.
Prefer CIDR addresses over host names. A host name match depends on a forward and reverse DNS lookup for every connection attempt, which makes authentication slower and ties access control to DNS. A name must resolve in both directions, and the forward lookup must return the client's address.
A mask length of /32 for IPv4 or /128 for IPv6 means exactly one host. 0.0.0.0/0 matches every IPv4 address and ::/0 matches every IPv6 address.
A common source of confusion: the address is the client's address as the server sees it. If the client connects through a proxy, a NAT device, or a connection pooler, the server sees the address of that intermediary, not of the original client.
Field 5: authentication method
The method is the action taken when the record matches. Common values are trust, reject, peer, password, scram-sha-256, and cert. The chapter "Choosing an authentication method" compares them. Two points matter for reading records.
First, reject is a valid method. It ends the search and refuses the connection, which makes it useful as an explicit catch-all.
Second, a record that matches decides the outcome. If the method fails (wrong password, for instance), PostgreSQL does not fall through to the next record. The next chapter explains this first-match behavior in detail.
Field 6: options
Options are name=value pairs after the method. Which options exist depends on the method. For example, peer and ident accept map=, which names a mapping in pg_ident.conf. The cert method has clientcert=, and ldap and radius have several settings.
# TYPE DATABASE USER ADDRESS METHOD
local all all peer map=osmap
hostssl all all 0.0.0.0/0 cert clientcert=verify-fullSeparate multiple options with spaces. Quote a value that contains spaces with double quotes.
Quoting and special words
Keywords such as all, replication, sameuser, and samerole are special only when unquoted. Putting double quotes around them turns them into ordinary names. This protects you in one direction and surprises you in the other. If you have a database literally named all, write it as "all".
A comma-separated list cannot have spaces around the commas unless the whole line is otherwise valid. Write appdb,reports, not appdb, reports, to avoid ambiguity.
Reading a full file
Here is the example file from this tutorial, read one record at a time.
# 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
host all all 0.0.0.0/0 reject- Local socket connections by the role
postgresto any database usepeer. The operating system user name must match. - Any other role connecting over the socket must give a SCRAM password.
- Connections from
127.0.0.1(IPv4 loopback) need a SCRAM password. - Connections from
::1(IPv6 loopback) need a SCRAM password. app_usermay reachappdbfrom10.0.0.0/24, but only over TLS, and must give a SCRAM password.- Any other IPv4 TCP connection is refused.
Notice that record 6 only covers IPv4 addresses. An IPv6 client from outside the loopback address matches no record, and PostgreSQL rejects it with a "no pg_hba.conf entry" error. If you want an explicit catch-all for IPv6, add host all all ::/0 reject.
Inspecting records with SQL
You can see how the server parsed your file with the pg_hba_file_rules view. It shows each record's fields as the server understood them, and an error column for lines it could not parse.
SELECT line_number, type, database, user_name, address, auth_method, options, error
FROM pg_hba_file_rules
ORDER BY line_number;Note that database and user_name are arrays, because each field can hold a list. A record with appdb,reports shows {appdb,reports}. Reading the view is a good habit after any edit, and the later chapter on applying changes uses it in full.
Common mistakes in record syntax
- Using
hostwith no address, orlocalwith an address. The server reports a syntax error for that line, and the view shows it inerror. - Writing the netmask in both forms, for example
10.0.0.0/24 255.255.255.0. - Forgetting that
allfor the database does not include replication connections. - Using a host name that does not resolve in both directions.
- Editing the file and not reloading. The running server keeps using the old rules until it re-reads the file.
- Relying on a typo-prone method name. An unknown method makes the line invalid. On reload, the server skips that line and logs an error, so the record is silently absent from the rules in use.
Exercise
Write one pg_hba.conf record that lets any member of the role analysts connect to the database reports from the subnet 10.1.0.0/16 over TLS, using SCRAM. Then say which field would need to change if the server also had to accept the same users from the single host 10.2.0.5.
Show answer
hostssl reports +analysts 10.1.0.0/16 scram-sha-256. The + prefix matches members of the role. For the extra host, add a second record with the address 10.2.0.5/32, or use a different address field in a new line. The address field (field 4) is the one that changes, since one record holds only one address.
Exercise
A record reads host all all 10.0.0.0/24 scram-sha-256. A client at 10.0.0.9 tries to connect to a replication stream. Does this record match, and why?
Show answer
No. The database value all does not match replication connections. A record with replication in the database field is required.