Sub topic 1
Authentication and authorization: what pg_hba.conf decides
PostgreSQL uses two related security questions:
- Authentication asks, “Who is this client?”
- Authorization asks, “What may this authenticated role do?”
The pg_hba.conf file controls the first question and whether a connection may reach the database server. It does not grant table privileges, choose schemas, or decide whether a role may run DELETE. Those decisions come from PostgreSQL roles, database ownership, and privileges.
Prerequisites
This chapter assumes that you know basic SQL and psql, understand PostgreSQL roles and databases, can edit a Linux configuration file, and have a basic understanding of IP addresses and CIDR notation.
What pg_hba.conf controls
pg_hba.conf is PostgreSQL's host-based authentication configuration file. Each record describes a class of incoming connections and the authentication method required for that class.
A client connection has several properties that PostgreSQL compares with the records:
- Connection type, such as a Unix-domain socket or TCP/IP.
- Requested database.
- PostgreSQL user, also called a role.
- Client IP address for network connections.
- Authentication method, such as
peerorscram-sha-256.
A matching record can allow authentication, require a particular proof of identity, or reject the connection. After authentication succeeds, normal PostgreSQL authorization rules determine what the role can do.
For example, this rule requires SCRAM password authentication for TCP connections from the local machine:
host all all 127.0.0.1/32 scram-sha-256The rule does not mean every authenticated user can access every database. It only says that a matching connection must pass SCRAM authentication before PostgreSQL accepts it.
Authentication is not authorization
Suppose app_user successfully connects to the appdb database. The successful connection proves only that the client satisfied the selected authentication rule. It does not prove that app_user can read the orders table.
You might separately grant access with SQL:
GRANT CONNECT ON DATABASE appdb TO app_user;
GRANT USAGE ON SCHEMA public TO app_user;
GRANT SELECT ON TABLE public.orders TO app_user;The first grant concerns database authorization. The second and third grants concern object authorization. These grants work only after pg_hba.conf permits the connection and the role authenticates successfully.
A useful mental model
Analyze a failed client connection in this order:
- Is the PostgreSQL server listening on the address and port the client used?
- Does a
pg_hba.confrecord match the connection type, database, user, and address? - Does the selected authentication method succeed?
- Does the role have permission to connect to the database?
- Does the role have permission for the requested schema, table, or operation?
This order prevents a common mistake: troubleshooting a missing SELECT privilege when the client has not passed host-based authentication yet.
Exercise
A client from 192.0.2.15 authenticates successfully as app_user but receives permission denied for table orders. Which security decision failed?
Show answer
Authentication succeeded. Authorization for the table failed, so inspect the role's schema and table privileges rather than pg_hba.conf.