SET_ROLE(7pg16)
hey bestie, welcome to SET ROLE(7) from the PostgreSQL 16.15 Documentation π this is the page where your SQL session gets to switch up its current user identifier, and honestly? iconic. β¨
πΈ NAME πΈ
SET_ROLE β set the current user identifier of the current session. that's the whole vibe: your session's current user identifier gets changed. π
β¨ SYNOPSIS β¨
SET [ SESSION | LOCAL ] ROLE role_name
SET [ SESSION | LOCAL ] ROLE NONE
RESET ROLE
π DESCRIPTION π
okay so this command sets the current user identifier of the current SQL session to be role_name. you can write the role name as either an identifier or a string literal β both are valid, no cap. after SET ROLE, permissions checking for SQL commands is carried out as though the named role were the one that had logged in originally. it's giving "you are now wearing this role's permissions fit." π
the current session user must have the SET option for the specified role_name, either directly or indirectly via a chain of memberships with the SET option. (if the session user is a superuser, any role can be selected.) so yeah, there's a whole permission check happening behind the scenes. π
the SESSION and LOCAL modifiers act the same as for the regular SET command. π«Ά
SET ROLE NONE sets the current user identifier to the current session user identifier, as returned by session_user. RESET ROLE sets the current user identifier to the connection-time setting specified by the command-line options, ALTER ROLE, or ALTER DATABASE, if any such settings exist. otherwise, RESET ROLE sets the current user identifier to the current session user identifier. these forms can be executed by any user. π
π§ NOTES π§
- using this command, it is possible to either add privileges or restrict one's privileges. both directions, bestie β it can go either way. π’
- if the session user role has been granted memberships
WITH INHERIT TRUE, it automatically has all the privileges of every such role. in this case,SET ROLEeffectively drops all the privileges except for those which the target role directly possesses or inherits. so yes, it can be a privilege glow-down. π - on the other hand, if the session user role has been granted memberships
WITH INHERIT FALSE, the privileges of the granted roles can't be accessed by default. however, if the role was grantedWITH SET TRUE, the session user can useSET ROLEto drop the privileges assigned directly to the session user and instead acquire the privileges available to the named role. very "swap the fit, swap the access." π - if the role was granted
WITH INHERIT FALSE, SET FALSEthen the privileges of that role cannot be exercised either with or withoutSET ROLE. that role's privileges are just not exercisable in that situation, periodt. π« - note that when a superuser chooses to
SET ROLEto a non-superuser role, they lose their superuser privileges. yes, literally. superuser status does not come along for the ride. π SET ROLEhas effects comparable toSET SESSION AUTHORIZATION, but the privilege checks involved are quite different. also,SET SESSION AUTHORIZATIONdetermines which roles are allowable for laterSET ROLEcommands, whereas changing roles withSET ROLEdoes not change the set of roles allowed to a laterSET ROLE. iykyk. π§SET ROLEdoes not process session variables as specified by the role'sALTER ROLEsettings; this only happens during login. so no, it's not doing that login-time session variable thing. πSET ROLEcannot be used within aSECURITY DEFINERfunction. that's a hard no, bestie. β οΈ
π EXAMPLES π
peep the before/after, it's lowkey satisfying:
SELECT SESSION_USER, CURRENT_USER;
session_user | current_user
--------------+--------------
peter | peter
SET ROLE 'paul';
SELECT SESSION_USER, CURRENT_USER;
session_user | current_user
--------------+--------------
peter | paul
π¦ COMPATIBILITY π¦
PostgreSQL allows identifier syntax ("rolename"), while the SQL standard requires the role name to be written as a string literal. SQL does not allow this command during a transaction; PostgreSQL does not make this restriction because there is no reason to. the SESSION and LOCAL modifiers are a PostgreSQL extension, as is the RESET syntax. slay for extensions, honestly. π¦
π SEE ALSO π
SET SESSION AUTHORIZATION(SET_SESSION_AUTHORIZATION(7))
okay that's the whole page β brought to you by PostgreSQL 16.15 and dated 2026, and it's the SET ROLE(7) manual page. byeee, go switch roles responsibly π π«Ά
π¦ where this page comes from
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms