Sub topic 6
Group roles, membership, and inheritance
This chapter assumes you can create roles with CREATE ROLE, know the difference between LOGIN and NOLOGIN roles, and have used GRANT on tables and schemas. Here you will see how one role can hold the rights of another, and where that mechanism stops working.
A group is a role with members
PostgreSQL has no separate "group" object. A group role is an ordinary role, usually created with NOLOGIN, that other roles join. You add a member with GRANT role TO member:
CREATE ROLE app_readonly NOLOGIN;
CREATE ROLE app_writer NOLOGIN;
CREATE ROLE alice LOGIN PASSWORD 'change-me';
CREATE ROLE bob LOGIN PASSWORD 'change-me';
GRANT app_readonly TO alice;
GRANT app_writer TO bob;
GRANT app_readonly TO app_writer;The last line makes app_writer a member of app_readonly. Because membership chains, bob is a direct member of app_writer and an indirect member of app_readonly. You can then grant table privileges to the group once, and every member picks them up.
Role membership cannot form a cycle. If app_writer is a member of app_readonly, then GRANT app_writer TO app_readonly fails.
To remove a member, use REVOKE:
REVOKE app_readonly FROM alice;Two separate things: having rights and switching identity
Membership gives a member two distinct abilities.
- Inheriting: the member automatically uses the privileges of the group. When
alicerunsSELECT, PostgreSQL checksalice's own privileges and those of every role she inherits from. - Switching: the member can run
SET ROLE app_readonlyand act as that group role.
After SET ROLE, current_user changes to the group role, while session_user stays the login role. Privilege checks then use only the group role and the roles it inherits. Privileges that belong only to alice are not available until you run RESET ROLE.
-- connected as alice
SELECT session_user, current_user; -- alice, alice
SET ROLE app_readonly;
SELECT session_user, current_user; -- alice, app_readonly
RESET ROLE;Objects created after SET ROLE are owned by the role you switched to. This is a common way to create objects owned by a group such as app_owner without logging in as it.
What is never inherited
Inheritance covers privileges, such as SELECT on a table or USAGE on a schema. It also covers the right to act as an object's owner, so a member of an owner role can alter or drop that role's objects.
Role attributes are never inherited. If app_admin has CREATEDB and alice is a member of it, alice still cannot create databases. She must run SET ROLE app_admin first. The same applies to LOGIN, SUPERUSER, CREATEROLE, REPLICATION, and BYPASSRLS. This is why group roles should be NOLOGIN: a group never becomes a way to log in.
Controlling inheritance by PostgreSQL version
The version of PostgreSQL you run decides how inheritance is controlled.
| Aspect | PostgreSQL 15 and earlier | PostgreSQL 16 and later |
|---|---|---|
| Where inheritance is set | On the member role: `INHERIT` (default) or `NOINHERIT` | On each membership grant: `WITH INHERIT TRUE` or `FALSE` |
| Switching with `SET ROLE` | Always allowed for any member | Allowed only if the grant has `SET TRUE` |
| Meaning of `ALTER ROLE ... NOINHERIT` | Stops the role inheriting from all its groups | Sets the default only for grants made later |
PostgreSQL 16 syntax looks like this:
-- PostgreSQL 16 and later
GRANT app_readonly TO alice WITH INHERIT TRUE, SET FALSE;Here alice automatically has the group's privileges but cannot run SET ROLE app_readonly. This suits the common case where members should use group privileges but never act as the group.
On older versions you control inheritance on the member:
-- Older versions
ALTER ROLE alice NOINHERIT;
SET ROLE app_readonly;With NOINHERIT, alice gets permission denied for table orders until she runs SET ROLE app_readonly. Use this for administrative roles that should carry extra power only when the user asks for it.
Delegating with ADMIN OPTION
By default only a superuser, or a role allowed to administer the group, can add members to it. You can delegate that right:
GRANT app_readonly TO carol WITH ADMIN OPTION;carol can now add or remove members of app_readonly. Delegating this way gives the ability to manage membership only. It does not let carol change the group's own privileges.
In PostgreSQL 16 and later, a role that creates another role with CREATEROLE automatically gets ADMIN OPTION on it. A CREATEROLE user can then manage only the roles it created or was given admin rights on, not every role in the cluster.
Inspecting membership
The catalog pg_auth_members stores one row per membership grant. Join it to pg_roles to read names:
SELECT g.rolname AS group_role,
m.rolname AS member,
am.admin_option
FROM pg_auth_members am
JOIN pg_roles g ON g.oid = am.roleid
JOIN pg_roles m ON m.oid = am.member
ORDER BY group_role, member;On PostgreSQL 16 and later you can also select am.inherit_option and am.set_option.
To test the effective result for one role, use pg_has_role:
SELECT pg_has_role('alice', 'app_readonly', 'MEMBER') AS is_member,
pg_has_role('alice', 'app_readonly', 'USAGE') AS has_group_privileges;MEMBER is true for direct or indirect membership, and USAGE is true when the role actually inherits the group's privileges. If is_member is true and has_group_privileges is false, the user must run SET ROLE first, or the grant was made with INHERIT FALSE. This query is the fastest way to explain "she is in the group but still gets permission denied."
Common errors
- Granting to the login role instead of the group. If you run
GRANT SELECT ON ... TO aliceand later addbob, you must repeat the grant. Grant to the group and add members. - Creating objects as a member. If
aliceinherits write rights and creates a table,aliceowns it. Default privileges setFOR ROLE app_ownerwill not apply. Create objects afterSET ROLE app_owner, or asapp_ownerdirectly. - Expecting attributes to follow membership.
CREATEDBandLOGINon a group do nothing for its members. - Forgetting membership is not dropped with the grant. Removing a privilege from the group affects all members at once, so check
pg_auth_membersbefore changing a widely shared group. - Using
NOINHERITand forgettingSET ROLE. On old versions, aNOINHERITuser who sees "permission denied" usually just needsSET ROLE.
Exercise
On PostgreSQL 16, alice was granted appreadonly with WITH INHERIT FALSE, SET TRUE. She runs SELECT * FROM shop.orders and gets "permission denied". What does she need to do, and what does `pghasrole('alice', 'appreadonly', 'USAGE')` return before she does it?
Show answer
She must run SET ROLE app_readonly first, because the grant does not let her inherit the group's privileges. Before that, pg_has_role('alice', 'app_readonly', 'USAGE') returns false, while the MEMBER check returns true.
Exercise
appadmin has the CREATEDB attribute and bob is a member of appadmin with inheritance enabled. Can bob run CREATE DATABASE? Why or why not?
Show answer
Not directly. Role attributes such as CREATEDB are never inherited through membership. Bob must run SET ROLE app_admin (allowed only if his grant has the SET option, which is always allowed before PostgreSQL 16) and then run CREATE DATABASE as that role.