brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: pg_hba_file_rules shows PostgreSQL login rules and the line that is broken

Postgresql
~/tips/tip-postgresql-pg-hba-file-rules

FROM pg_hba_file_rules

Lists the rules in pg_hba.conf as rows, with an error message for lines that cannot be parsed.

Editing pg_hba.conf feels risky, because a typo can lock people out at the next reload. PostgreSQL gives you a way to check the file before you reload.

SELECT line_number, error
FROM pg_hba_file_rules
WHERE error IS NOT NULL;

The view turns the file into rows: rule_number, type, database, user_name, address, auth_method and more. The rule number is the order in which rules are considered when someone logs in, so the first matching line wins. A line that could not be processed has only line_number and error filled in, and a NULL rule_number.

The useful part is that the view reports on the current contents of the file, not on what the server last loaded. So the routine is: edit the file, run the query, fix anything it reports, and only then run SELECT pg_reload_conf();.

Two things to know. By default only superusers can read the view. And because it reads the file, it can differ from the rules the server is using right now, until you reload. Checking the view does not apply anything.

Further reading: pg_hba_file_rules

More Postgresql tips FROM pg_settings LOAD 'auto_explain'; CREATE EXTENSION pgstattuple; pg_dump -O -x -f mydb.sql mydb FROM pg_stat_user_indexes All Postgresql tips (24) All topics

Link