Sub topic 2
Finding and Reloading `pg_hba.conf`
This chapter assumes you know basic SQL and psql, understand PostgreSQL roles and databases, and can edit files on a Linux server. You should also know that an IP address range such as 10.0.0.0/24 uses CIDR notation.
Find the active file
pg_hba.conf is PostgreSQL's host-based authentication file. It contains rules that decide which clients may authenticate, but it is separate from postgresql.conf, which contains general server settings.
Do not assume that the file is in the data directory. Package managers, containers, and managed installations can place it elsewhere. Ask the running server for the exact path:
SHOW hba_file;
SELECT name, setting
FROM pg_settings
WHERE name IN ('data_directory', 'config_file', 'hba_file');The hba_file setting returns an absolute path in most installations. If a configuration uses a relative path, PostgreSQL interprets it relative to the data directory.
You need operating-system permission to edit the file. A database superuser can read configuration metadata, but database privileges do not automatically grant permission to change files on the server.
Understand reloads
PostgreSQL reads pg_hba.conf when the server starts and again when its configuration is reloaded. A reload keeps the server running and applies authentication changes to new connection attempts. Existing sessions are not authenticated again, so an already-open connection normally continues using its current session.
The usual SQL method is:
SELECT pg_reload_conf();A result of t means PostgreSQL accepted the request to reload configuration. It does not prove that every line in pg_hba.conf was valid. Check the parsed rules after editing.
An administrator can also request a reload from the operating system:
pg_ctl reload -D "$PGDATA"The service-manager command varies by installation. For example, a systemd service might use systemctl reload postgresql, but the service name can differ between distributions and PostgreSQL versions.
Validate the file after editing
PostgreSQL exposes the parsed contents through pg_hba_file_rules. The view includes the source line number and an error column. A non-null error indicates that PostgreSQL could not parse that record.
SELECT line_number,
type,
database,
user_name,
address,
auth_method,
error
FROM pg_hba_file_rules
ORDER BY line_number;The view is especially useful for finding spelling mistakes, missing fields, invalid CIDR ranges, and unsupported authentication methods. It also lets you confirm that the server is reading the file you edited.
A reliable workflow is:
- Query
SHOW hba_fileand record the path. - Back up the current file.
- Edit only the intended records.
- Request a reload with
pg_reload_conf(). - Inspect
pg_hba_file_rules. - Test a new connection from the relevant client network.
- Keep an existing administrative session open until the test succeeds.
Exercise
Does SELECT pg_reload_conf(); prove that every authentication rule is valid?
Show answer
No. It confirms that PostgreSQL received the reload request. Inspect pg_hba_file_rules, especially its error column, and then test a new connection.