Sub topic 1
Authentication and authorization: what pg_hba.conf decides
Prerequisites
This chapter assumes you already have the following:
- Basic SQL and
psqlusage. - Familiarity with PostgreSQL roles and databases.
- Comfort editing configuration files on a Linux server.
- A basic idea of IP addresses and CIDR notation, such as
10.0.0.0/24.
You do not need to know SCRAM or the pg_hba.conf syntax yet. Later chapters cover both. This chapter sets up the mental model that the rest of the tutorial builds on.
Two questions, two mechanisms
PostgreSQL answers two separate questions when a client connects and runs a query.
- Authentication asks whether the client really is the role it claims to be, and whether this kind of connection is allowed at all.
- Authorization asks what that role may do once it is connected.
People often mix these up, because both can produce a "permission" style error. They are implemented in different places, they fail with different messages, and you fix them with different tools.
| Aspect | Authentication | Authorization |
|---|---|---|
| Question | Who are you, and may you connect this way? | What may you do? |
| Configured in | `pg_hba.conf`, `pg_ident.conf`, role passwords | `GRANT`, `REVOKE`, role attributes, ownership, row-level security |
| Happens | Once, at connection start | On every statement |
| Typical failure | `no pg_hba.conf entry for host ...` | `permission denied for table orders` |
| Inspected with | `pg_hba_file_rules`, server log | `\du`, `\dp`, `has_*_privilege()` functions |
This tutorial is about the first column. pg_hba.conf is the file that controls it. The name stands for "host-based authentication".
What pg_hba.conf decides
pg_hba.conf is a list of rules. Each rule describes a kind of connection attempt and says what to do with it. When a connection arrives, PostgreSQL looks at four facts about it:
- How it arrived: a Unix-domain socket, plain TCP, or TCP with TLS.
- The client's IP address, for TCP connections.
- The database name the client asked for.
- The role name the client asked for.
It finds the first rule that matches those facts. That rule names an authentication method, for example scram-sha-256, peer, or reject. The server then runs that method. If the method succeeds, authentication is done.
A rule can therefore decide three things:
- Whether a connection is rejected outright, before any password is checked.
- Which method the client must satisfy, such as a password, an operating system identity, or a client certificate.
- Whether TLS is required for that path, by using
hostsslinstead ofhost.
Here is a small file to make this concrete. You will learn to read every column in the next chapters. For now, read it as a list of sentences.
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all scram-sha-256
host all all 127.0.0.1/32 scram-sha-256
hostssl appdb app_user 10.0.0.0/24 scram-sha-256
host all all 0.0.0.0/0 rejectThe first line says that on the local socket, the postgres role is accepted if the operating system user is also postgres. The last line says that any other IPv4 connection over plain TCP is rejected. Note that pg_hba.conf never says which tables a role may read.
What pg_hba.conf does not decide
pg_hba.conf does not grant or remove privileges on tables, schemas, or functions. A rule that accepts app_user on database appdb says nothing about what app_user can do there. It also does not store passwords. Passwords live in the system catalog pg_authid and are covered in a later chapter. The file only chooses which method is used to check them.
Authorization is handled by the SQL privilege system, with these pieces:
- Role attributes such as
LOGIN,SUPERUSER,CREATEDB, andCONNECTION LIMIT. - Object privileges granted with
GRANTand removed withREVOKE. - Ownership of objects.
- Row-level security policies.
The boundary between the two is not perfectly clean, which is where most confusion comes from. Three checks happen at connection time but belong to authorization:
- The role must have the
LOGINattribute. - The role must not be past its
VALID UNTILdate. - The role must hold the
CONNECTprivilege on the target database.
By default, every role gets CONNECT on every new database through the pseudo-role PUBLIC. That default is why a new role can connect to any database as soon as pg_hba.conf lets it in.
The order of checks
A connection passes through these stages in order. A failure at any stage ends the attempt.
The first three failure paths are authentication problems and are fixed in pg_hba.conf or in the credentials. The last two are authorization problems and are fixed with ALTER ROLE or GRANT.
Seeing both layers in practice
The server log and the client message together tell you which layer rejected you. Here are the usual messages, from earliest stage to latest:
FATAL: no pg_hba.conf entry for host "10.0.0.5", user "app_user", database "appdb", no encryption. No rule matched this combination. The message echoes the four facts PostgreSQL used, so you can compare them with your rules.FATAL: password authentication failed for user "app_user". A rule matched and demanded a password, and the check failed. This text is also used when the role does not exist. The client is deliberately not told which, so that an attacker cannot probe for role names. The server log carries the detail.FATAL: role "app_user" is not permitted to log in. Authentication succeeded, but the role lacksLOGIN.FATAL: permission denied for database "appdb". Authentication succeeded, but the role lacksCONNECT.ERROR: permission denied for table orders. The session is open and an object privilege is missing.
You can check the authorization side with ordinary SQL. This example creates a role and a database, then tightens the default CONNECT privilege so that authorization, not only pg_hba.conf, limits who can enter.
CREATE ROLE app_user LOGIN;
CREATE ROLE report_user LOGIN;
CREATE DATABASE appdb;
REVOKE CONNECT ON DATABASE appdb FROM PUBLIC;
GRANT CONNECT ON DATABASE appdb TO app_user;
SELECT rolname, rolcanlogin, rolsuper, rolvaliduntil
FROM pg_roles
WHERE rolname IN ('app_user', 'report_user');
SELECT r.rolname,
has_database_privilege(r.rolname, 'appdb', 'CONNECT') AS can_connect
FROM pg_roles AS r
WHERE r.rolname IN ('app_user', 'report_user');The second query shows can_connect as true for app_user and false for report_user. Even if pg_hba.conf accepts report_user for appdb, the connection still ends with permission denied for database "appdb". Neither role has a password yet. Setting passwords is covered in the chapter on password storage.
Where the file lives and when it is read
The server reads pg_hba.conf at startup and again whenever it receives a reload signal. You can see the path with a query. This needs superuser rights or membership in pg_read_all_settings.
SHOW hba_file;On many source builds the file sits in the data directory. Debian and Ubuntu packages put it under /etc/postgresql/<version>/<cluster>/. Red Hat style packages keep it in the data directory under /var/lib/pgsql/<version>/data/.
Editing the file changes nothing until the server reloads it. Existing sessions are not affected by a reload, because authentication happens only once, when a session starts. The reload and validation steps get their own chapter.
A habit that prevents confusion
When a connection fails, first decide which layer failed, then look in the right place.
- Read the exact
FATALorERRORtext and note whether it namespg_hba.conf, a password,LOGIN,CONNECT, or an object. - If it points to authentication, compare the four facts in the message with your rules, top to bottom.
- If it points to authorization, query
pg_rolesand thehas_*_privilege()functions.
This split is the structure of the rest of the tutorial. The next chapters cover the first half of this process in detail, from rule syntax and ordering to SCRAM, TLS, and troubleshooting.
Exercise
A client connects as report_user to appdb and receives FATAL: permission denied for database "appdb". Should you edit pg_hba.conf to fix it? Explain which layer rejected the connection.
Show answer
No. Authentication already succeeded, because the server got far enough to check privileges. The failure comes from authorization: report_user lacks the CONNECT privilege on appdb, probably because it was revoked from PUBLIC and never granted to this role. Fix it with GRANT CONNECT ON DATABASE appdb TO report_user;. A pg_hba.conf problem would instead have produced no pg_hba.conf entry ... or password authentication failed.
Exercise
Name two things that pg_hba.conf decides and two things it does not decide.
Show answer
It decides whether a connection is rejected and which authentication method the client must satisfy. It can also require TLS by using hostssl. It does not decide which tables, schemas, or functions a role may use, and it does not store passwords, which live in pg_authid.