Sub topic 6
Group Roles, Membership, and Inheritance
This chapter assumes that you can connect to PostgreSQL and know basic CREATE TABLE, SELECT, INSERT, and UPDATE statements. It also assumes that you understand that roles, databases, schemas, and tables are separate objects.
Use Roles as Groups
A PostgreSQL role can represent a person, an application, or a group. A role with LOGIN can authenticate. A role with NOLOGIN is commonly used as a group role that collects privileges.
Grant privileges to the group role, then make login roles members of it:
CREATE ROLE app_readonly NOLOGIN;
CREATE ROLE app_writer NOLOGIN;
CREATE ROLE alice LOGIN PASSWORD 'change-me';
GRANT app_readonly TO alice;The GRANT app_readonly TO alice statement creates membership. It does not grant app_readonly ownership of anything, and it does not copy privileges into alice. When inheritance is enabled, Alice can use privileges granted to app_readonly.
Inheritance and `SET ROLE`
A member role can receive a group role's privileges automatically. This behavior is called inheritance. The login role's INHERIT attribute controls automatic inheritance in older PostgreSQL versions and remains relevant to role behavior.
A session also has a current_user. With inheritance, Alice can query a table without changing current_user:
SELECT current_user;
SELECT * FROM shop.orders;If Alice uses SET ROLE, PostgreSQL changes the active role for permission checks:
SET ROLE app_readonly;
SELECT current_user;
SELECT * FROM shop.orders;
RESET ROLE;After SET ROLE app_readonly, current_user is app_readonly, although the authenticated session began as Alice. RESET ROLE returns to the original login role.
PostgreSQL 16 and later can control inheritance and role switching separately for each membership:
GRANT app_readonly TO alice
WITH INHERIT TRUE, SET FALSE;This membership lets Alice inherit the privileges but prevents her from explicitly switching to app_readonly. If you need the switch, grant SET TRUE or omit the restrictive option according to your policy.
Nested Membership and Administration
Roles can be members of other roles. For example, app_writer could belong to app_readonly if writers should also receive read access. Keep such nesting shallow because long membership chains make audits harder.
Membership administration is itself permission-controlled. A role that grants membership generally needs the appropriate membership administration authority. Avoid giving broad role-management power to application accounts.
Check memberships and attributes with psql:
\duYou can also inspect role attributes through the catalog:
SELECT rolname, rolcanlogin, rolinherit, rolcreaterole
FROM pg_roles
WHERE rolname IN ('alice', 'app_readonly', 'app_writer');Design group roles around job needs. A read-only group should receive read privileges, while a writer group should receive only the additional write privileges it requires.
Exercise
Alice is a member of app_readonly, but the membership has INHERIT FALSE. What must she do to use that role's privileges, assuming SET is allowed?
Show answer
She must run SET ROLE app_readonly before accessing objects through that role, then use RESET ROLE when finished.