Role hierarchy
By granting roles to other roles, you can build a hierarchy of roles, where roles lower in the hierarchy have a narrow range of privileges, while roles higher in the hierarchy are granted combinations of roles and their privileges. When you organize roles hierarchically, any privileges that you add to lower-level roles are automatically propagated to the roles above them.
Creating hierarchical roles
The following example creates two roles, assigns them privileges, then assigns both roles to another role.
-
Create table
applog
:=> CREATE TABLE applog (id int, sourceID VARCHAR(32), data TIMESTAMP, event VARCHAR(256));
-
Create the
logreader
role and grant it read-only privileges on tableapplog
:=> CREATE ROLE logreader; CREATE ROLE => GRANT SELECT ON applog TO logreader; GRANT PRIVILEGE
-
Create the
logwriter
role and grant it write privileges on tableapplog
:=> CREATE ROLE logwriter; CREATE ROLE => GRANT INSERT, UPDATE ON applog to logwriter; GRANT PRIVILEGE
-
Create the
logadmin
role and grant it DELETE privilege on tableapplog
:=> CREATE ROLE logadmin; CREATE ROLE => GRANT DELETE ON applog to logadmin; GRANT PRIVILEGE
-
Grant the
logreader
andlogwriter
roles to rolelogadmin
:=> GRANT logreader, logwriter TO logadmin;
-
Create user
bob
and grant him thelogadmin
role:=> CREATE USER bob; CREATE USER => GRANT logadmin TO bob; GRANT PRIVILEGE
-
Modify user
bob
's account so hislogadmin
role is automatically enabled on login:=> ALTER USER bob DEFAULT ROLE logadmin; ALTER USER => \c - bob You are now connected as user "bob". => SHOW ENABLED_ROLES; name | setting ---------------+---------- enabled roles | logadmin (1 row)
Enabling hierarchical roles
Only roles that are explicitly granted to a user can be enabled for that user. In the previous example, roles logreader
or logwriter
cannot be enabled for bob
. They can only be enabled indirectly, by enabling logadmin
.
Hierarchical role grants and WITH ADMIN OPTION
If one or more roles are granted to another role using WITH ADMIN OPTION
, then users who are granted the 'higher' role inherit administrative access to the subordinate roles.
For example, you might modify the earlier grants of roles logreader
and logwriter
to logadmin
as follows:
=> GRANT logreader, logwriter TO logadmin WITH ADMIN OPTION;
NOTICE 4617: Role "logreader" was already granted to role "logadmin"
NOTICE 4617: Role "logwriter" was already granted to role "logadmin"
GRANT ROLE
User bob
, through his logadmin
role, is now authorized to grant its two subordinate roles to other users—in this case, role logreader
to user Alice
:
=> \c - bob;
You are now connected as user "bob".
=> GRANT logreader TO Alice;
GRANT ROLE
=> \c - alice;
You are now connected as user "alice".
=> show available_roles;
name | setting
-----------------+-----------
available roles | logreader
(1 row)
Note
Because the grant of the logadmin
role to bob
did not include WITH ADMIN OPTION
, he cannot grant that role to alice
:
=> \c - bob;
You are now connected as user "bob".
=> GRANT logadmin TO alice;
ROLLBACK 4925: The role "logadmin" cannot be granted to "alice"