Sub topic 10
Applying and validating changes with `pg_hba_file_rules`
Assume you know basic SQL, psql, PostgreSQL roles, and CIDR notation. This chapter shows how to apply a pg_hba.conf change safely and verify what PostgreSQL parsed.
Reloading is different from restarting
PostgreSQL reads pg_hba.conf when the server starts. It also rereads the file after a configuration reload. A reload keeps existing client sessions connected and applies the new rules to later connection attempts.
The pg_reload_conf() function asks the server to reread its configuration files:
SELECT pg_reload_conf();A result of t means PostgreSQL accepted the reload request. It does not prove that every line in pg_hba.conf is valid. Check the parsed rules separately.
You can find the active HBA file with:
SHOW hba_file;This is safer than assuming a distribution-specific path such as /var/lib/postgresql/data/pg_hba.conf.
Inspecting the parsed rules
The pg_hba_file_rules view exposes the HBA records in file order. Query the fields that determine matching and authentication:
SELECT
line_number,
type,
database,
user_name,
address,
netmask,
auth_method,
options,
error
FROM pg_hba_file_rules
ORDER BY line_number;The line_number column lets you check first-match ordering. The error column identifies a line PostgreSQL could not parse. An error usually means that the rule is not usable, so fix it before testing clients.
For a compact validation sequence, run:
SELECT pg_reload_conf();
SELECT line_number, type, database, user_name,
address, auth_method, error
FROM pg_hba_file_rules
ORDER BY line_number;A successful reload with a non-null error still requires correction. Also inspect the PostgreSQL server log when a reload or connection test behaves unexpectedly. The view validates syntax and parsing. It does not tell you which rule an arbitrary client will match.
Testing the effective behavior
A rule matches only when all of its relevant fields match the connection. Check the connection type, database, role, client address, and rule order. For example, a connection through a Unix socket uses a local rule. A connection to 127.0.0.1 uses a host rule instead.
Test from the same kind of client network that the rule is meant to serve:
psql "host=db.example.com dbname=appdb user=app_user sslmode=verify-full channel_binding=require"After connecting, use \conninfo in psql to confirm the server, database, user, and transport. A failed test can mean that no rule matched, the first matching rule rejected the connection, the password was wrong, or TLS verification failed.
A repeatable workflow
- Back up the current HBA file.
- Add or edit the narrowest rule needed.
- Check the rule's position relative to broader rules.
- Run
pg_reload_conf(). - Query
pg_hba_file_rulesand confirmerroris null. - Open a new test connection.
- Check the server log if the result differs from your prediction.
Exercise
pg_reload_conf() returns t, but pg_hba_file_rules.error is non-null for the new line. Is the change ready for production?
Show answer
No. The reload request succeeded, but PostgreSQL could not parse that line. Correct the HBA syntax, reload again, and verify that the error is null before testing the connection.