Sub topic 10
Applying and validating changes with pg_hba_file_rules
This chapter assumes you can edit pg_hba.conf with the record format and first-match ordering from the earlier chapters. It also assumes you can run SQL as a superuser in psql. Here you will learn how a change reaches the running server and how to check it before and after it takes effect. Getting this wrong has a real cost: a typo can lock every client out, or silently leave the old rules in place while you believe the new ones are active.
What happens when you edit the file
PostgreSQL does not watch pg_hba.conf. Saving the file changes nothing for the running server. The postmaster reads the file at startup and again when it receives a reload request (a SIGHUP signal). Each new backend process then uses the rules loaded at that time.
Two consequences follow:
- Connections that are already open are not affected by a reload. A rule you remove does not disconnect anyone.
- New connections are checked against the rules loaded at the last successful reload.
You can ask for a reload in several ways. All of them end in the same signal.
| Method | Command | Notes |
|---|---|---|
| SQL | `SELECT pg_reload_conf();` | Needs superuser, or a grant of `EXECUTE` on the function. |
| Service manager | `sudo systemctl reload postgresql` | The unit name varies by distribution, for example `postgresql@16-main`. |
| pg_ctl | `pg_ctl reload -D /path/to/data` | Run as the operating system user that owns the cluster. |
| Signal | `kill -HUP <postmaster pid>` | The postmaster PID is the first line of `postmaster.pid`. |
pg_reload_conf() returns true when the signal was sent. It does not tell you the new file was accepted. That is the main trap in this chapter.
What a failed reload does
If the postmaster finds a syntax error in pg_hba.conf during a reload, it rejects the whole file and keeps the previous rules. It writes a message like the following to the server log:
LOG: invalid authentication method "scram-sha256"
CONTEXT: line 5 of configuration file "/etc/postgresql/16/main/pg_hba.conf"
LOG: pg_hba.conf was not reloadedThe behavior is different at startup. If the file is invalid when the server starts, the server refuses to start, because it has no earlier set of rules to fall back on.
The failed-reload behavior is safe, but it is quiet. pg_reload_conf() still returns true, and you will not see the problem unless you read the log or query the system view described next.
The pg_hba_file_rules view
pg_hba_file_rules is a system view that parses pg_hba.conf and returns one row for each rule line. Reading it requires superuser privileges by default, although you can grant access to another role.
Its most useful columns are:
| Column | Meaning |
|---|---|
| `line_number` | Line of the rule in the file. |
| `type` | `local`, `host`, `hostssl`, `hostnossl`, and so on. |
| `database` | Array of database names or keywords such as `all`. |
| `user_name` | Array of role names or keywords. |
| `address` | Host name or IP address, or a keyword such as `all`. Empty for `local`. |
| `netmask` | Netmask, when the address was written with a separate mask. |
| `auth_method` | The method, for example `scram-sha-256`. |
| `options` | Array of `name=value` options such as `clientcert=verify-full`. |
| `error` | Parse error text for this line, or `NULL` if the line is valid. |
In PostgreSQL 16 and later the view also has rule_number and file_name. The file_name column matters when you use include directives to split the configuration over several files. A line with an error has no rule_number, because it is not a usable rule.
The view reads the file on disk each time you query it. It does not report the rules the server is currently enforcing. This distinction is the key to using it well:
- Queried after you edit the file and before you reload, it shows what the server would load.
- Queried after a failed reload, it shows the new bad file, while the server still enforces the old one.
Validating before you reload
Start with the file from the earlier examples. Suppose you have just added a rule for the appdb database and made a typo in the method name on line 6:
# 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-sha256
host all all 0.0.0.0/0 rejectBefore reloading, look for problems:
SELECT line_number, error
FROM pg_hba_file_rules
WHERE error IS NOT NULL;The result points at the bad line:
line_number | error
-------------+----------------------------------------
6 | invalid authentication method "scram-sha256"Fix the spelling to scram-sha-256, save the file, and run the query again. When it returns no rows, the file parses cleanly.
Then review the whole rule list in file order, which is also the order PostgreSQL uses to match:
SELECT line_number, type, database, user_name, address, auth_method, error
FROM pg_hba_file_rules
ORDER BY line_number;Read it from the top as if you were a connecting client. Ask which rule your connection would hit first. A broad rule near the top, such as host all all 0.0.0.0/0 reject, hides every more specific rule below it. The view will not warn you about that. It reports syntax only, so rule order is your responsibility.
What the view cannot tell you
The view checks that each line is well formed. It does not check that the configuration is sensible. In particular:
- It does not check that a role or database named in a rule exists. A rule for a misspelled role is valid and simply never matches.
- It does not test whether a client address would match a rule.
- It does not confirm that the roles' stored passwords match the method. A
scram-sha-256rule works for a role that still has an MD5 password, but anmd5rule cannot be satisfied by a SCRAM verifier in every case. Checkpg_authidas shown in the migration chapter. - It does not show what is currently enforced, as explained above.
You can also find risky rules quickly with an ordinary query on the view:
SELECT line_number, type, database, user_name, address, auth_method
FROM pg_hba_file_rules
WHERE auth_method IN ('trust', 'password', 'md5')
ORDER BY line_number;Any row returned is a rule worth a second look. trust lets a matching client in with no password at all. password sends the password as clear text.
Reloading and confirming it worked
Once the file parses without errors, reload and check that the server accepted it.
SELECT pg_reload_conf();
SELECT pg_conf_load_time();pg_conf_load_time() returns the time the configuration files were last loaded successfully. If it is not close to the current time, the reload did not happen or did not succeed. Compare it with now(), and read the end of the log if the times differ.
sudo tail -n 20 /var/log/postgresql/postgresql-16-main.logThe log path depends on your installation. Look for pg_hba.conf was not reloaded, which means the old rules are still in force.
Testing without locking yourself out
A validated file can still be wrong in practice. To stay safe:
- Keep an administrator session open while you test. An open session survives the reload, so you can revert the file from it.
- Keep a working local path for the superuser, such as the
local all postgres peerrule, so you can always reach the server from the host itself. - Test from a new connection that resembles the real client, with the same host, database, and role:
psql "host=db.example.com dbname=appdb user=app_user sslmode=verify-full" -c "SELECT current_user;"- Test a connection that should be refused, and confirm that it is. A rule set that lets everything in is also a failure.
If a test fails, restore the previous file, query pg_hba_file_rules again, and reload. Keeping the old version under version control or in a backup copy makes this a single command.
Common mistakes
- Reloading and walking away. The function returned
true, but the log says the file was rejected. - Restarting when a reload would do. Rule changes never need a restart. A restart disconnects every client.
- Testing from the wrong connection type. A test over the Unix socket matches
localrules. A test over127.0.0.1matcheshostrules. Test the path your application uses. - Trusting the view as enforcement state. It reflects the file on disk, so compare with
pg_conf_load_time()and the log.
Exercise
You edit pghba.conf, run SELECT pgreload_conf(); and get true. New connections still follow the old rules. List two things you would check to find out why, and say which one tells you what the server is actually enforcing.
Show answer
Query pghbafilerules with WHERE error IS NOT NULL to find a syntax error in the file, and read the server log for "pghba.conf was not reloaded". Also compare pgconfload_time() with the current time. The load time and the log tell you whether the server accepted the file, so they show what is enforced. The view only shows the parsed contents of the file on disk, not the rules in force.
Exercise
A rule "host all all 0.0.0.0/0 reject" sits on line 3 of a file, above "hostssl appdb appuser 10.0.0.0/24 scram-sha-256" on line 8. Does pghbafilerules report an error for line 8, and will app_user at 10.0.0.5 connect?
Show answer
No error is reported, because line 8 is valid syntax. The view does not check rule order. A non-TLS connection from 10.0.0.5 matches line 3 first and is rejected, because the first matching rule wins and processing stops there. A TLS connection also matches line 3, since "host" matches both TLS and non-TLS TCP connections, so app_user is rejected either way. Move the specific rule above the reject rule.