Host-Based Authentication (pg_hba.conf)
Table of Contents
1. What is pg_hba.conf?
HBA means Host-Based Authentication. The file pg_hba.conf tells PostgreSQL who may connect, to which database, from where, and how they must prove who they are. It lives in the data directory (/pgdata/pgsql-17/data/pg_hba.conf in this lab; /var/lib/pgsql/<version>/data/pg_hba.conf on a default install).
# TYPE DATABASE USER ADDRESS METHOD [OPTIONS]
| Field | Meaning |
|---|---|
TYPE | local (Unix socket), host (TCP/IP, with or without SSL), hostssl (SSL only), hostnossl (non-SSL only) |
DATABASE | Which databases: all, postgres, orcl, or the word replication for replication connections |
USER | Which roles: all, teja, … |
ADDRESS | Client IP address or network in CIDR form, such as 127.0.0.1/32 or 192.168.2.0/24. Not used for local. |
METHOD | How to authenticate: trust, md5, scram-sha-256, peer, ldap, reject, cert |
OPTIONS | Optional settings, for example clientcert=verify-full or the ldap... settings |
Important rule: PostgreSQL reads the file from top to bottom and uses the first line that matches the connection. It does not look at the lines below it. If no line matches, the connection is refused. This is why the order of lines matters in every example below.
2. Authentication Methods
| Method | What it does | Use it for |
|---|---|---|
trust | No password. Anyone who reaches the server is accepted. | Testing only. Not secure. |
md5 | Password required, checked as an MD5 hash. | Legacy clients only. Deprecated in PostgreSQL 18. |
scram-sha-256 | Password required, checked with salted SCRAM-SHA-256. Stronger than MD5. | Recommended for production. |
peer | Local connections only. The OS user name must match the PostgreSQL role name. | Local admin work and scripts under the same OS user. |
ldap | The password is checked against an LDAP / Active Directory server. | Central company logins (see section 11). |
reject | Always refuses the connection. | Blocking users or networks, like a firewall rule. |
cert | Requires an SSL/TLS client certificate. The certificate name must match the role. | High-security setups that use SSL. |
Example lines:
# TYPE DATABASE USER ADDRESS METHOD [OPTIONS]
host all all 127.0.0.1/32 trust
host all all 127.0.0.1/32 md5
host all all 127.0.0.1/32 scram-sha-256
local all all peer
host all all 0.0.0.0/0 reject
hostssl all all 192.168.1.0/24 scram-sha-256
hostssl all all 192.168.1.0/24 ldap ldapserver=ldap.example.com ldapprefix="uid=" ldapsuffix=",dc=example,dc=com"
hostssl all all 192.168.1.0/24 cert clientcert=verify-full
3. Lab Setup and Test User
| Item | Value |
|---|---|
| Server | pgdba01 (192.168.2.201) |
| PostgreSQL version | 17, started manually with pg_ctl |
| Data directory | /pgdata/pgsql-17/data (set as PGDATA in .bash_profile) |
| Log directory | /pglogs/pgsql-17/logs |
| Test user / database | teja / orcl |
Note: The network examples (sections 8 to 11) need listen_addresses = '*' in postgresql.conf, so the server accepts TCP connections on 192.168.2.201. Changing listen_addresses needs a restart. All commands run as the postgres OS user.
Create the test user and database:
postgres=# CREATE USER teja WITH PASSWORD 'teja123';
CREATE ROLE
postgres=# CREATE DATABASE orcl OWNER teja;
CREATE DATABASE
4. Contents of pg_hba.conf
Always take a backup before you change this file. Then look at the active (non-comment) lines that initdb created:
[postgres@pgdba01 ~]$ cp $PGDATA/pg_hba.conf $PGDATA/pg_hba.conf.bkp
[postgres@pgdba01 ~]$ grep -v '^\s*#' $PGDATA/pg_hba.conf | grep -v '^$'
local all all peer
host all all 127.0.0.1/32 scram-sha-256
host all all ::1/128 scram-sha-256
local replication all peer
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256
For the examples below we use a small file instead. Edit it with vi $PGDATA/pg_hba.conf so that it contains only these active lines:
# TYPE DATABASE USER ADDRESS METHOD
# Allow local peer access for postgres
local all postgres peer
# Allow local password access for all
local all all scram-sha-256
After every change, tell PostgreSQL to re-read the file. A reload is enough, no restart is needed:
[postgres@pgdba01 ~]$ pg_ctl reload
server signaled
5. Play with peer
peer works for local (socket) connections only. PostgreSQL asks the operating system who the caller is and compares that name with the role. File contents (as in section 4):
local all postgres peer
local all all scram-sha-256
You are logged in to Linux as postgres. The role teja does not match that OS user, so it falls to the second line and needs a password. The role postgres matches the first line and needs none:
[postgres@pgdba01 ~]$ psql -U teja -d orcl
Password for user teja: <---- it asks for a password
orcl=> exit
[postgres@pgdba01 ~]$ psql -U postgres -d orcl
orcl=# <---- no password for postgres
6. Play with trust
trust means no password at all. Use it only for testing. Change the last line of the file to trust and reload:
local all postgres peer
local all all trust
[postgres@pgdba01 ~]$ pg_ctl reload
server signaled
[postgres@pgdba01 ~]$ psql -U teja -d orcl
orcl=> <---- it does not ask for a password
Warning: With trust on a host line, anyone who can reach port 5432 can log in as any user, including postgres. Never use it on a server that other people can connect to.
7. Play with reject
reject refuses the connection. Change the last line to reject and reload:
local all postgres peer
local all all reject
[postgres@pgdba01 ~]$ pg_ctl reload
server signaled
[postgres@pgdba01 ~]$ psql -U teja -d orcl
psql: error: connection to server on socket "/run/postgresql/.s.PGSQL.5432" failed: FATAL: pg_hba.conf rejects connection for host "[local]", user "teja", database "orcl", no encryption
Tip: You can still log in as postgres and fix the file, because the peer rule for postgres is above the reject rule. Always keep one working admin rule at the top, or you can lock yourself out.
8. Restrict by User
Goal: over the network, allow only teja to reach database orcl, and block every other user. The more specific line must come first:
local all postgres peer
local all all scram-sha-256
host orcl teja 192.168.2.201/32 scram-sha-256
host orcl all 192.168.2.201/32 reject
[postgres@pgdba01 ~]$ pg_ctl reload
server signaled
[postgres@pgdba01 ~]$ psql -U teja -d orcl -h 192.168.2.201
Password for user teja:
orcl=> \conninfo
You are connected to database "orcl" as user "teja" on host "192.168.2.201" at port "5432".
orcl=> exit
[postgres@pgdba01 ~]$ psql -U postgres -d orcl -h 192.168.2.201
psql: error: connection to server at "192.168.2.201", port 5432 failed: FATAL: pg_hba.conf rejects connection for host "192.168.2.201", user "postgres", database "orcl", no encryption
Result: only teja can connect to orcl over the network. The user postgres is rejected.
9. Restrict by Network
Goal: block every connection that comes from the network 192.168.2.0/24:
local all postgres peer
local all all scram-sha-256
host all all 192.168.2.0/24 reject
[postgres@pgdba01 ~]$ pg_ctl reload
server signaled
[postgres@pgdba01 ~]$ psql -U teja -d orcl -h 192.168.2.201
psql: error: connection to server at "192.168.2.201", port 5432 failed: FATAL: pg_hba.conf rejects connection for host "192.168.2.201", user "teja", database "orcl", no encryption
Tip: The connection comes from the server’s own address (192.168.2.201), which is inside 192.168.2.0/24, so the rule matches. Local socket connections (no -h) are not affected, because they match a local line, not a host line.
10. SSL and Non-SSL (hostssl / hostnossl)
A host line accepts both SSL and non-SSL connections. To control this, use:
hostssl: matches only encrypted (SSL/TLS) connections.hostnossl: matches only plain, unencrypted connections.
Step 1: Enable SSL on the server. This lab uses a self-signed certificate. In production, use a certificate from your CA. PostgreSQL looks for server.crt and server.key in the data directory by default.
[postgres@pgdba01 ~]$ cd $PGDATA
[postgres@pgdba01 data]$ openssl req -new -x509 -days 365 -nodes -text -out server.crt -keyout server.key -subj "/CN=pgdba01"
[postgres@pgdba01 data]$ chmod 600 server.key
[postgres@pgdba01 data]$ psql -c "ALTER SYSTEM SET ssl = on;"
ALTER SYSTEM
[postgres@pgdba01 data]$ pg_ctl reload
server signaled
[postgres@pgdba01 data]$ psql -c "SHOW ssl;"
ssl
-----
on
Note: PostgreSQL refuses to start SSL if server.key is readable by group or others. It must be owned by postgres with permission 600.
Step 2: Allow SSL, reject non-SSL. Use this pg_hba.conf and reload:
local all postgres peer
local all all scram-sha-256
hostssl orcl teja 192.168.2.201/32 scram-sha-256
hostnossl orcl all 192.168.2.201/32 reject
Step 3: Test both ways. The sslmode setting on the client decides whether SSL is used.
[postgres@pgdba01 ~]$ pg_ctl reload
server signaled
# With SSL: allowed
[postgres@pgdba01 ~]$ psql "host=192.168.2.201 dbname=orcl user=teja sslmode=require"
Password for user teja:
orcl=> SELECT ssl, version, cipher FROM pg_stat_ssl WHERE pid = pg_backend_pid();
ssl | version | cipher
-----+---------+------------------------
t | TLSv1.3 | TLS_AES_256_GCM_SHA384
(1 row)
orcl=> exit
# Without SSL: rejected
[postgres@pgdba01 ~]$ psql "host=192.168.2.201 dbname=orcl user=teja sslmode=disable"
psql: error: connection to server at "192.168.2.201", port 5432 failed: FATAL: pg_hba.conf rejects connection for host "192.168.2.201", user "teja", database "orcl", no encryption
Result: encrypted connections are accepted and plain connections are refused. The words no encryption at the end of the message tell you the client did not use SSL.
Tips:
1. The default sslmode=prefer tries SSL first, so a normal psql -h ... also matches hostssl lines.
2. If you have only hostssl lines and no hostnossl line, a non-SSL client gets no pg_hba.conf entry ... no encryption. That also blocks it, without an explicit reject rule.
3. sslmode=require encrypts but does not check the server certificate. For production, use a CA-signed certificate and connect with sslmode=verify-full.
11. LDAP Authentication
With ldap, PostgreSQL does not check its own password. It sends the user name and password to an LDAP or Active Directory server and accepts the login if LDAP does. There are two modes.
Note: This lab has no LDAP server. The lines below are a template. Replace ldap.example.com and the dc=... values with your own directory.
Mode 1: Simple bind. PostgreSQL builds the DN as prefix + username + suffix and tries to log in with it:
local all postgres peer
local all all scram-sha-256
hostssl orcl teja 192.168.2.0/24 ldap ldapserver=ldap.example.com ldapprefix="uid=" ldapsuffix=",ou=people,dc=example,dc=com"
For user teja this binds as uid=teja,ou=people,dc=example,dc=com.
Mode 2: Search + bind. PostgreSQL first searches the directory for the user, then binds with the DN it found. Use this when users are spread over several OUs, or for Active Directory:
hostssl orcl all 192.168.2.0/24 ldap ldapserver=ldap.example.com ldapscheme=ldaps ldapbasedn="dc=example,dc=com" ldapbinddn="cn=svc-pg,dc=example,dc=com" ldapbindpasswd="secret" ldapsearchattribute=uid
Common LDAP options:
| Option | Meaning |
|---|---|
ldapserver, ldapport | LDAP server name and port (default 389, or 636 for ldaps) |
ldapscheme=ldaps | Encrypt the connection to LDAP with LDAPS |
ldaptls=1 | Encrypt with STARTTLS on the normal LDAP port |
ldapprefix, ldapsuffix | Simple bind: text added before and after the user name |
ldapbasedn | Search + bind: where to start searching |
ldapsearchattribute | Search + bind: attribute that holds the user name (uid, or sAMAccountName in Active Directory) |
ldapbinddn, ldapbindpasswd | Service account used for the search. If omitted, the search is anonymous. |
Test with the LDAP password of the user. The PostgreSQL password is not used:
[postgres@pgdba01 ~]$ psql "host=192.168.2.201 dbname=orcl user=teja sslmode=require"
Password for user teja: <---- type the LDAP password
If the LDAP password is wrong, the client sees only a short message. The server log has more detail about the LDAP error:
psql: error: connection to server at "192.168.2.201", port 5432 failed: FATAL: LDAP authentication failed for user "teja"
Tip: LDAP only checks the password. The role must already exist in PostgreSQL, for example CREATE ROLE teja LOGIN;. It does not need a PostgreSQL password.
Warning: The client sends the password to PostgreSQL in clear text, and PostgreSQL passes it on to LDAP. So always use hostssl (section 10) and ldapscheme=ldaps or ldaptls=1. Also, ldapbindpasswd is stored as plain text in pg_hba.conf, so keep the file owned by postgres with permission 600.
12. Verify Rules and Logs
Check what PostgreSQL sees in the file. The error column shows syntax problems, so you can check a file before you reload it:
postgres=# SELECT line_number, type, database, user_name, address, auth_method, error FROM pg_hba_file_rules;
line_number | type | database | user_name | address | auth_method | error
-------------+-------+----------+------------+---------------+---------------+-------
4 | local | {all} | {postgres} | | peer |
7 | local | {all} | {all} | | scram-sha-256 |
10 | host | {orcl} | {teja} | 192.168.2.201 | scram-sha-256 |
11 | host | {orcl} | {all} | 192.168.2.201 | reject |
(4 rows)
postgres=# SELECT line_number, error FROM pg_hba_file_rules WHERE error IS NOT NULL;
(0 rows)
Every rejected connection is written to the server log (the log folder is set by log_directory in postgresql.conf; the file name contains the weekday):
[postgres@pgdba01 ~]$ tail -f /pglogs/pgsql-17/logs/postgresql-Fri.log
... LOG: received SIGHUP, reloading configuration files
... FATAL: pg_hba.conf rejects connection for host "[local]", user "teja", database "orcl", no encryption
... LOG: received SIGHUP, reloading configuration files
... FATAL: pg_hba.conf rejects connection for host "192.168.2.201", user "postgres", database "orcl", no encryption
Tip: received SIGHUP, reloading configuration files in the log means your pg_ctl reload worked. If a reload finds a syntax error in pg_hba.conf, PostgreSQL keeps the old rules and writes the error to the log.
13. Common Errors
| Error message | Meaning and fix |
|---|---|
no pg_hba.conf entry for host ... | No line matched. Add a rule for this host, user, database and SSL type. |
pg_hba.conf rejects connection ... | A reject line matched first. Check the order of lines. |
password authentication failed for user ... | Wrong password, or the role has no password set. |
Peer authentication failed for user ... | The OS user name is different from the role name on a peer line. |
LDAP authentication failed for user ... | Wrong LDAP password or wrong DN settings. See the server log for details. |
Connection refused | Not a pg_hba.conf problem. Check that the server is running, listen_addresses, and the firewall (port 5432). |
Tip: The end of the message shows no encryption or SSL encryption. This tells you which type of line (hostssl or hostnossl) PostgreSQL tried to match.
14. Best Practices
- Keep one working admin rule (
local all postgres peer) at the top. - Put specific rules before general ones.
- Use
scram-sha-256. Avoidmd5, and never usetrustonhostlines. - Use small networks (
192.168.2.0/24), not0.0.0.0/0. - Use
hostsslfor every remote connection, and always for LDAP. - Back up the file, and check
pg_hba_file_rulesbefore you reload.
15. Restore the Original File
When you finish the exercises, put the backup from section 4 back and reload:
[postgres@pgdba01 ~]$ cp $PGDATA/pg_hba.conf.bkp $PGDATA/pg_hba.conf
[postgres@pgdba01 ~]$ pg_ctl reload
server signaled
If you want to switch SSL off again, run ALTER SYSTEM RESET ssl; and reload.
















