RBAC (role-based access control) in PostgreSQL limits what users can read or change by assigning roles only the privileges they need.
Role-based access control (RBAC) assigns database permissions to roles instead of individual users. A user inherits every privilege granted to its roles, making permission management simpler and safer.
Roles can log in (LOGIN) or simply group privileges (NOLOGIN). Privileges include CONNECT, USAGE, SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, and TRIGGER.Grant or revoke them at database, schema, table, or column scope.
Create application roles such as analytics_reader, order_manager, and admin. Then map human users or application services to those roles with GRANT commands.
CREATE ROLE analytics_reader NOLOGIN;
CREATE ROLE order_manager NOLOGIN;
CREATE ROLE admin NOLOGIN;
CREATE ROLE app_user LOGIN PASSWORD 'strong_pw';
GRANT analytics_reader, order_manager TO app_user;
Grant roles only the statements they need on each table.For example, allow analytics_reader to query but not mutate data, while order_manager can change order records.
GRANT SELECT ON Customers, Orders, Products, OrderItems TO analytics_reader;
GRANT SELECT, INSERT, UPDATE ON Orders, OrderItems TO order_manager;
Default privileges ensure new tables inherit the correct grants automatically.Always set them immediately after creating a schema.
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO analytics_reader;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT, INSERT, UPDATE ON TABLES TO order_manager;
Enable RLS and attach policies so even if a role has SELECT, it only sees authorized rows.Use current_user or custom session variables to filter data.
ALTER TABLE Orders ENABLE ROW LEVEL SECURITY;
CREATE POLICY customer_isolation ON Orders
FOR SELECT USING (customer_id = current_setting('app.current_customer')::int);
Grant privileges to roles, never to users. Use NOLOGIN roles for grouping. Keep roles minimal—read, write, admin tiers. Document every grant in version-controlled migration files. Review privileges on every release.
Yes.Grant one role to another to create inheritance chains, but keep them shallow for clarity.
Enable log_connections and log_statement = 'ddl' to track role usage and privilege changes. Pair with pgAudit for detailed logs.
Privilege checks are lightweight. Only complex row-level security policies may add measurable overhead. Index filter columns to minimize impact.
.
No. PostgreSQL prevents circular grants to avoid infinite inheritance loops.
Privileges are additive. If any role grants a permission, the user receives it, so revoke from all roles to remove access.