Sub topic 2
Finding and reloading pg_hba.conf
This chapter assumes you can run psql as a superuser, edit files on a Linux server with sudo, and read basic SQL. You don't need to know the rule syntax yet. The next chapter covers it. Here you learn where the file lives, how to change it safely, and how to confirm that PostgreSQL is using your change.
What the file is for
pg_hba.conf is the host-based authentication file. "HBA" stands for host-based authentication. The server reads it each time a client connects, before any SQL runs. The file is a list of rules. Each rule says which connection types, databases, roles and client addresses it applies to, and which authentication method the client must pass.
The file only decides how a client proves who it is. Whether that role may read a table is decided later by privileges (GRANT). The first chapter covered this split between authentication and authorization.
Finding the file
The location depends on how PostgreSQL was installed. Don't guess. Ask the running server:
SHOW hba_file;
SHOW ident_file;
SHOW data_directory;hba_file is a server setting that holds the full path of the rules file. ident_file is the path of pg_ident.conf, which a later chapter covers. data_directory is where the cluster's files live. A cluster is one PostgreSQL server instance and its data.
Typical results:
| Installation | Typical `hba_file` |
|---|---|
| Debian and Ubuntu packages (version 16, cluster `main`) | `/etc/postgresql/16/main/pg_hba.conf` |
| Red Hat, Rocky and Fedora packages (PGDG, version 16) | `/var/lib/pgsql/16/data/pg_hba.conf` |
| Built from source or `initdb` by hand | `<data_directory>/pg_hba.conf` |
| Docker official image | `/var/lib/postgresql/data/pg_hba.conf` |
initdb creates the first copy of the file when it initializes a cluster. Debian-style packages then move the configuration files into /etc. They point hba_file at the new location from postgresql.conf.
If you can't connect at all, you can't run SHOW. In that case ask the process what it was started with:
ps -o args= -C postgres | head -n 1
sudo -u postgres psql -c "SHOW hba_file;"The first command shows the -D data directory or -c config_file=... option. The second works on most systems because the local postgres rule uses peer authentication. If your rules block it, read the postgresql.conf that the first command revealed and look for hba_file.
Ownership and permissions
PostgreSQL runs as the operating system user postgres. That user must be able to read the file. Keep ownership and permissions tight:
sudo ls -l /etc/postgresql/16/main/pg_hba.confExpect the owner to be postgres, with a mode such as -rw-r-----. Nobody except administrators should be able to write the file. Anyone who can edit it can open the database to the world.
Anatomy of the file at a glance
The next chapter covers the record format in detail. For this chapter you only need these points:
- Each non-blank line is one rule. Lines that start with
#are comments. - Fields are separated by spaces or tabs.
- The server reads rules from top to bottom.
- Since PostgreSQL 16, the file can pull in other files with
include,include_if_existsandinclude_dir. These affect where rules come from, soSHOW hba_fileshows only the top-level file.
Reloading is not restarting
PostgreSQL reads pg_hba.conf at startup and again whenever the main server process (the postmaster) receives a reload request. A reload is a SIGHUP signal. The postmaster re-reads its configuration files without stopping. A restart stops and starts the server and drops every connection.
You almost never need a restart for pg_hba.conf. Any of these reloads it:
SELECT pg_reload_conf();# Run from a shell, as the postgres user or with the right -D
sudo -u postgres pg_ctl reload -D /var/lib/postgresql/16/main
# systemd on Red Hat style systems
sudo systemctl reload postgresql-16
# Debian and Ubuntu helper for one cluster
sudo pg_ctlcluster 16 main reload
# Send the signal directly
sudo kill -HUP "$(sudo head -n 1 /var/lib/postgresql/16/main/postmaster.pid)"All of these do the same thing underneath. The first one needs an existing superuser session, which is why it is the most convenient. pg_reload_conf() returns t when the signal was sent. It does not tell you the file was valid.
A reload also re-reads postgresql.conf and pg_ident.conf. The same reload applies changes to all three files.
What a reload does to existing sessions
Rules are checked only when a connection starts. A reload therefore has these effects:
- New connections are checked against the new rules.
- Sessions that are already open stay connected, even if the new rules would reject them.
- Changing a rule never terminates anyone.
To remove a connection that the new rules would deny, end it yourself:
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE usename = 'app_user'
AND pid <> pg_backend_pid();What happens if the file is wrong
The result of a syntax error depends on when it is found:
| When | Result |
|---|---|
| At server start | The server refuses to start and logs the line with the error. |
| At reload | The server logs the error, keeps the previous rules, and keeps running. |
The reload behavior is a safety net, but it can hide a problem. You edit the file, run pg_reload_conf(), get t, and assume the change is live. In fact the old rules are still in force. Always confirm the result instead of trusting the return value.
Checking the file before and after reloading
The view pg_hba_file_rules shows the file as the server parses it from disk. Each row is one rule, and the error column is filled when a line could not be parsed. Reading the view needs superuser rights unless a superuser has granted access to it.
Check the file before you reload:
SELECT line_number, type, database, user_name, address, auth_method, error
FROM pg_hba_file_rules
ORDER BY line_number;To find only the broken lines:
SELECT line_number, error
FROM pg_hba_file_rules
WHERE error IS NOT NULL;The view reads the file at the time you query it, so it works as a pre-flight check. If this query returns no rows, there are no syntax errors. Then reload:
SELECT pg_reload_conf();
SELECT pg_conf_load_time();pg_conf_load_time() returns the time of the last configuration load. If the time did not move forward, the server has not re-read the files. The pg_hba_file_rules view shows what is on disk, not what is loaded. After a failed reload the two differ, so use the log to learn which version is active.
The server log is the final check. On Debian it is usually /var/log/postgresql/postgresql-16-main.log. On Red Hat it is under log/ in the data directory, or in the journal:
sudo journalctl -u postgresql-16 --since "5 minutes ago"A successful reload logs received SIGHUP, reloading configuration files. A bad file also logs a line like invalid authentication method "scram" with the line number.
The full edit cycle
In practice the cycle looks like this:
- Open a second superuser session and leave it connected.
- Back up the file:
sudo cp pg_hba.conf pg_hba.conf.bak. - Make one change.
- Check
pg_hba_file_rulesfor errors. - Reload.
- Connect from a new terminal with the role and address the rule is meant for.
- If the new connection fails, restore the backup and reload again.
Making one change at a time keeps the cause of a failure easy to find.
Common mistakes
- Editing the wrong file. A copy in the data directory may be ignored if
hba_filepoints elsewhere. Always runSHOW hba_file;first. - Restarting when a reload would do. A restart drops every connection and is rarely needed here.
- Trusting
pg_reload_conf()returningt. It means only that the signal was sent. - Forgetting that open sessions are unaffected. A tightened rule does not disconnect anyone already in.
- Editing as the wrong user. If you save the file with
sudoand root-only permissions, the server may not be able to read it. Check the owner and mode afterwards. - Locking yourself out. A new
rejector a changed method on your own rule can block you. Keep the spare session.
Exercise
You add a new rule to pghba.conf, run SELECT pgreload_conf(); and get t. A new client is still rejected as if the rule did not exist. Name two things you would check, and the query or command for each.
Show answer
First, check whether the file has a parse error that made the server keep the old rules: run SELECT linenumber, error FROM pghbafilerules WHERE error IS NOT NULL; and read the server log for the message and line number. Second, check that you edited the file the server actually uses: run SHOW hbafile; and compare the path with the file you changed. You can also run SELECT pgconfloadtime(); to see whether the configuration was reloaded after your edit.
Exercise
A role has an open session when you change pg_hba.conf so that the role is rejected, and you reload. Is the session ended? How do you end it?
Show answer
No. Rules are checked only when a connection is made, so the open session continues. End it with SELECT pgterminatebackend(pid) FROM pgstatactivity WHERE usename = 'the_role'; using the role's name.