1SET ROLE(7) PostgreSQL 13.4 Documentation SET ROLE(7)
2
3
4
6 SET_ROLE - set the current user identifier of the current session
7
9 SET [ SESSION | LOCAL ] ROLE role_name
10 SET [ SESSION | LOCAL ] ROLE NONE
11 RESET ROLE
12
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.
19
20 The specified role_name must be a role that the current session user is
21 a member of. (If the session user is a superuser, any role can be
22 selected.)
23
24 The SESSION and LOCAL modifiers act the same as for the regular SET(7)
25 command.
26
27 SET ROLE NONE sets the current user identifier to the current session
28 user identifier, as returned by session_user. RESET ROLE sets the
29 current user identifier to the connection-time setting specified by the
30 command-line options, ALTER ROLE, or ALTER DATABASE, if any such
31 settings exist. Otherwise, RESET ROLE sets the current user identifier
32 to the current session user identifier. These forms can be executed by
33 any user.
34
36 Using this command, it is possible to either add privileges or restrict
37 one's privileges. If the session user role has the INHERIT attribute,
38 then it automatically has all the privileges of every role that it
39 could SET ROLE to; in this case SET ROLE effectively drops all the
40 privileges assigned directly to the session user and to the other roles
41 it is a member of, leaving only the privileges available to the named
42 role. On the other hand, if the session user role has the NOINHERIT
43 attribute, SET ROLE drops the privileges assigned directly to the
44 session user and instead acquires the privileges available to the named
45 role.
46
47 In particular, when a superuser chooses to SET ROLE to a non-superuser
48 role, they lose their superuser privileges.
49
50 SET ROLE has effects comparable to SET SESSION AUTHORIZATION
51 (SET_SESSION_AUTHORIZATION(7)), but the privilege checks involved are
52 quite different. Also, SET SESSION AUTHORIZATION determines which roles
53 are allowable for later SET ROLE commands, whereas changing roles with
54 SET ROLE does not change the set of roles allowed to a later SET ROLE.
55
56 SET ROLE does not process session variables as specified by the role's
57 ALTER ROLE (ALTER_ROLE(7)) settings; this only happens during login.
58
59 SET ROLE cannot be used within a SECURITY DEFINER function.
60
62 SELECT SESSION_USER, CURRENT_USER;
63
64 session_user | current_user
65 --------------+--------------
66 peter | peter
67
68 SET ROLE 'paul';
69
70 SELECT SESSION_USER, CURRENT_USER;
71
72 session_user | current_user
73 --------------+--------------
74 peter | paul
75
77 PostgreSQL allows identifier syntax ("rolename"), while the SQL
78 standard requires the role name to be written as a string literal. SQL
79 does not allow this command during a transaction; PostgreSQL does not
80 make this restriction because there is no reason to. The SESSION and
81 LOCAL modifiers are a PostgreSQL extension, as is the RESET syntax.
82
84 SET SESSION AUTHORIZATION (SET_SESSION_AUTHORIZATION(7))
85
86
87
88PostgreSQL 13.4 2021 SET ROLE(7)