Roles Basics
8 examples for PostgreSQL roles - 6 basic and 2 intermediate. In PostgreSQL, roles are the universal principal: login users and group-like permission bundles are the same object type.
Search across all documentation pages
8 examples for PostgreSQL roles - 6 basic and 2 intermediate. In PostgreSQL, roles are the universal principal: login users and group-like permission bundles are the same object type.
-- Connect as a role that can CREATE ROLE (typically superuser or CREATEROLE)
\duCREATE ROLE app_api LOGIN PASSWORD 'from-vault-rotate-quarterly';
ALTER ROLE app_api CONNECTION LIMIT 50;LOGIN allows password or cert authentication.CONNECTION LIMIT caps runaway pool misconfiguration per role.CREATE ROLE app_readers NOLOGIN;
CREATE ROLE app_writers NOLOGIN;
GRANT app_readers TO app_api;
GRANT app_writers TO app_migrator;GRANT group TO user.SET ROLE lets a login assume a group temporarily.app_api inherits app_readers only in read-only deploy profiles if needed.Related: GRANT/REVOKE Patterns - object privileges
CREATE ROLE tenant_admin NOLOGIN;
CREATE ROLE support_agent LOGIN INHERIT PASSWORD 'vault';
GRANT tenant_admin TO support_agent;
-- support_agent automatically has tenant_admin privileges when INHERIT true (default)INHERIT (default): granted roles' privileges apply automatically.NOINHERIT: must SET ROLE tenant_admin to activate privileges.NOINHERIT for break-glass roles audited on activation.\du+
SELECT r.rolname, m.rolname AS member_of
FROM pg_roles r
LEFT JOIN pg_auth_members am ON am.member = r.oid
LEFT JOIN pg_roles m ON m.oid = am.roleid
WHERE r.rolname = 'app_api';\du+ shows attributes: Superuser, Create role, Create DB, Replication.pg_roles includes roles you cannot LOGIN as.ALTER ROLE app_api SET statement_timeout = '30s';
ALTER ROLE app_api SET search_path = app, public;
ALTER ROLE app_migrator CREATEDB; -- rarely - prefer explicit database grantssearch_path on app roles prevents schema hijack surprises.CREATEDB/CREATEROLE on application login roles.-- Reassign owned objects first
REASSIGN OWNED BY old_app TO app_owner;
DROP OWNED BY old_app;
DROP ROLE old_app;DROP OWNED removes privileges; review before running on production.CREATE ROLE app_owner NOLOGIN;
CREATE ROLE app_staging LOGIN PASSWORD 'vault';
CREATE ROLE app_prod LOGIN PASSWORD 'vault';
GRANT app_owner TO app_staging, app_migrator;
-- app_prod inherits narrower grants applied separatelyapp_migrator; runtime uses app_api without DDL rights.Related: Default Privileges - migrations create objects as owner
SET ROLE for Audited ElevationCREATE ROLE ddl_runner NOLOGIN;
GRANT ddl_runner TO deploy_user WITH ADMIN OPTION FALSE;
-- In migration session only
SET ROLE ddl_runner;
CREATE TABLE audit.sample (id int);
RESET ROLE;SET ROLE records in logs when log_line_prefix includes user and session user.NOINHERIT on deploy_user for strict elevation.Stack versions: This page was written for PostgreSQL 18.4 (stable 18, maintenance 17), pgvector 0.8+, PgBouncer 1.x, Patroni 3.x, and PostGIS 3.5+.
Reviewed by Chris St. John·Last updated Jul 19, 2026