Overview
PostgreSQL uses roles for both users and groups. Access is controlled at three layers: network and authentication (pg_hba.conf), privileges on objects (GRANT), and optionally row-level security. Least privilege is the goal: applications should not connect as a superuser or as the table owner.
Core concepts
- A role with
LOGINis a user; a role without it is a group you grant membership in.NOINHERIT,CREATEDB,CREATEROLEandSUPERUSERare attributes; superuser bypasses all checks. - Privileges:
SELECT,INSERT,UPDATE,DELETE,USAGE(schemas, sequences),EXECUTE(functions),CONNECT. The owner of an object can do everything with it. - Default privileges (
ALTER DEFAULT PRIVILEGES) apply to future objects, which is how you avoid granting on every new table. - PUBLIC: the pseudo-role every role belongs to. Since PostgreSQL 15
PUBLICno longer hasCREATEon thepublicschema. RevokeCONNECTfromPUBLICon sensitive databases. - Authentication:
pg_hba.confliststype database user address methodrules, first match wins. Usescram-sha-256(the default password hash since PostgreSQL 14); avoidtrust. Add TLS (ssl = on,hostsslrules), and enterprise SSO vialdap,gssor certificates. - Row-level security (RLS):
ALTER TABLE ... ENABLE ROW LEVEL SECURITYplusCREATE POLICYlimits which rows a role can see or change; the table owner bypasses it unlessFORCE ROW LEVEL SECURITY. - Function privileges:
SECURITY DEFINERfunctions run with the owner’s rights, so setsearch_pathexplicitly (stored-routines). - Built-in roles such as
pg_read_all_data,pg_monitorgive broad but scoped access.
Worked example
Verified on PostgreSQL 17, a read-only role:
CREATE ROLE app_ro NOLOGIN;
GRANT USAGE ON SCHEMA public TO app_ro;
GRANT SELECT ON t TO app_ro;
CREATE ROLE alice LOGIN PASSWORD '...' IN ROLE app_ro; -- inherits app_ro
SET ROLE app_ro;
SELECT count(*) FROM t; -- works: 2
SELECT 1 FROM b LIMIT 1; -- ERROR: permission denied for table bFuture tables should be readable too:
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO app_ro;RLS for a multi-tenant table:
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON invoices
USING (tenant_id = current_setting('app.tenant_id')::int);Pitfalls and production notes
- Do not run applications as the owner. Separate roles: an owner for migrations, a read-write app role, a read-only role for analytics.
- Do not put passwords in migrations or repositories; use a secret manager, and rotate credentials. Prefer certificates or IAM-style short-lived tokens where available.
- Enable
pgauditfor audit trails andlog_connections/log_disconnectionsas needed. - Restrict
listen_addressesand the firewall; never expose 5432 to the internet. - Changing a role attribute or
pg_hba.confneeds only a reload (pg_reload_conf()); checkpg_hba_file_rulesfor errors before reloading. - Encryption at rest is not built in; use disk, volume or cloud-provider encryption.
Practice questions
- Why does
GRANT SELECT ON ALL TABLESnot cover tables created tomorrow? - How does RLS interact with a table owner?
- Order these
pg_hba.conflines to allow SCRAM from the app subnet and reject everything else.
References
- PostgreSQL manual, Database Roles and Client Authentication: https://www.postgresql.org/docs/current/user-manag.html
- PostgreSQL manual, Row Security Policies: https://www.postgresql.org/docs/current/ddl-rowsecurity.html