4 SET ROLE — set the current user identifier of the current session
8 SET [ SESSION | LOCAL ] ROLE role_name
9 SET [ SESSION | LOCAL ] ROLE NONE
14 This command sets the current user identifier of the current SQL
15 session to be role_name. The role name can be written as either an
16 identifier or a string literal. After SET ROLE, permissions checking
17 for SQL commands is carried out as though the named role were the one
18 that had logged in originally. Note that SET ROLE and SET SESSION
19 AUTHORIZATION are exceptions; permissions checks for those continue to
20 use the current session user and the initial session user (the
21 authenticated user), respectively.
23 The current session user must have the SET option for the specified
24 role_name, either directly or indirectly via a chain of memberships
25 with the SET option. (If the session user is a superuser, any role can
28 The SESSION and LOCAL modifiers act the same as for the regular SET
31 SET ROLE NONE sets the current user identifier to the current session
32 user identifier, as returned by session_user. RESET ROLE sets the
33 current user identifier to the connection-time setting specified by the
34 command-line options, ALTER ROLE, or ALTER DATABASE, if any such
35 settings exist. Otherwise, RESET ROLE sets the current user identifier
36 to the current session user identifier. These forms can be executed by
41 Using this command, it is possible to either add privileges or restrict
42 one's privileges. If the session user role has been granted memberships
43 WITH INHERIT TRUE, it automatically has all the privileges of every
44 such role. In this case, SET ROLE effectively drops all the privileges
45 except for those which the target role directly possesses or inherits.
46 On the other hand, if the session user role has been granted
47 memberships WITH INHERIT FALSE, the privileges of the granted roles
48 can't be accessed by default. However, if the role was granted WITH SET
49 TRUE, the session user can use SET ROLE to drop the privileges assigned
50 directly to the session user and instead acquire the privileges
51 available to the named role. If the role was granted WITH INHERIT
52 FALSE, SET FALSE then the privileges of that role cannot be exercised
53 either with or without SET ROLE.
55 SET ROLE has effects comparable to SET SESSION AUTHORIZATION, but the
56 privilege checks involved are quite different. Also, SET SESSION
57 AUTHORIZATION determines which roles are allowable for later SET ROLE
58 commands, whereas changing roles with SET ROLE does not change the set
59 of roles allowed to a later SET ROLE.
61 SET ROLE does not process session variables as specified by the role's
62 ALTER ROLE settings; this only happens during login.
64 SET ROLE cannot be used within a SECURITY DEFINER function.
68 SELECT SESSION_USER, CURRENT_USER;
70 session_user | current_user
71 --------------+--------------
76 SELECT SESSION_USER, CURRENT_USER;
78 session_user | current_user
79 --------------+--------------
84 PostgreSQL allows identifier syntax ("rolename"), while the SQL
85 standard requires the role name to be written as a string literal. SQL
86 does not allow this command during a transaction; PostgreSQL does not
87 make this restriction because there is no reason to. The SESSION and
88 LOCAL modifiers are a PostgreSQL extension, as is the RESET syntax.
92 SET SESSION AUTHORIZATION