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 LOGIN is a user; a role without it is a group you grant membership in. NOINHERIT, CREATEDB, CREATEROLE and SUPERUSER are 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 PUBLIC no longer has CREATE on the public schema. Revoke CONNECT from PUBLIC on sensitive databases.
  • Authentication: pg_hba.conf lists type database user address method rules, first match wins. Use scram-sha-256 (the default password hash since PostgreSQL 14); avoid trust. Add TLS (ssl = on, hostssl rules), and enterprise SSO via ldap, gss or certificates.
  • Row-level security (RLS): ALTER TABLE ... ENABLE ROW LEVEL SECURITY plus CREATE POLICY limits which rows a role can see or change; the table owner bypasses it unless FORCE ROW LEVEL SECURITY.
  • Function privileges: SECURITY DEFINER functions run with the owner’s rights, so set search_path explicitly (stored-routines).
  • Built-in roles such as pg_read_all_data, pg_monitor give 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 b

Future 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 pgaudit for audit trails and log_connections/log_disconnections as needed.
  • Restrict listen_addresses and the firewall; never expose 5432 to the internet.
  • Changing a role attribute or pg_hba.conf needs only a reload (pg_reload_conf()); check pg_hba_file_rules for errors before reloading.
  • Encryption at rest is not built in; use disk, volume or cloud-provider encryption.

Practice questions

  1. Why does GRANT SELECT ON ALL TABLES not cover tables created tomorrow?
  2. How does RLS interact with a table owner?
  3. Order these pg_hba.conf lines to allow SCRAM from the app subnet and reject everything else.

References

stored-routines · pg-backup-pitr · pg-connection-pooling