SET_ROLE(7pg17)
π SET_ROLE π
okay so bestie, welcome to SET_ROLE(7pg17) β that's the PostgreSQL 17 manual page, section 7, and today we're talking about literally becoming someone else for the rest of your session. identity crisis, but make it SQL β¨
β¨ NAME β¨
SET_ROLE β set the current user identifier of the current session π«Ά
Like, it changes who you are as far as the database is concerned. main character swap. π₯
πΈ SYNOPSIS πΈ
SET [ SESSION | LOCAL ] ROLE role_name
SET [ SESSION | LOCAL ] ROLE NONE
RESET ROLE
three little lines. iconic. π
π¦ DESCRIPTION π¦
So. This command sets the current user identifier of the current SQL session to be role_name. And yes, the role name can be written as either an identifier or a string literal β both are valid, we love flexibility. π
After SET ROLE, permissions checking for SQL commands is carried out as though the named role were the one that had logged in originally. you are, for all intents and purposes, that role now. ate. π
but wait β heads up, bestie π βSET ROLEandSET SESSION AUTHORIZATIONare exceptions. The permissions checks for those two continue to use the current session user and the initial session user (the authenticated user), respectively. so they don't play by the same rules. iykyk.
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. and if the session user is a superuser? any role can be selected. no cap. π
The SESSION and LOCAL modifiers act the same as for the regular SET command. β
Now the reset-ish forms, which are lowkey the best part:
SET ROLE NONEsets the current user identifier to the current session user identifier, as returned bysession_user.RESET ROLEsets the current user identifier to the connection-time setting specified by the command-line options,ALTER ROLE, orALTER DATABASE, if any such settings exist. Otherwise,RESET ROLEsets the current user identifier to the current session user identifier.
and the slay detail: these forms can be executed by any user. π«Ά
π NOTES π
okay this is the part where i hold your hand, because the privilege stuff is giving layers. π§
Using this command, it is possible to either add privileges or restrict one's privileges. Both! Choose your adventure!
- 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. - 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. - And if the role was granted
WITH INHERIT FALSE, SET FALSEβ then the privileges of that role cannot be exercised either with or withoutSET ROLE. π dead end, bestie, no workaround here.
Also important:SET ROLEhas effects comparable toSET SESSION AUTHORIZATION, but the privilege checks involved are quite different. AndSET 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. different vibes, different machinery. βοΈ
Two more caveats, and these matter fr fr:
SET ROLEdoes not process session variables as specified by the role'sALTER ROLEsettings; this only happens during login. (login-only behaviour, we don't do reruns π)SET ROLEcannot be used within aSECURITY DEFINERfunction.
π₯ EXAMPLES π₯
look at her go. π
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
see how session_user stayed peter but current_user became paul? that's the whole point. the session user is who logged in, the current user is who you're being right now. π¦β¨
π 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. (we're just chill like that π )
The SESSION and LOCAL modifiers are a PostgreSQL extension, as is the RESET syntax. π
π SEE ALSO π
SET SESSION AUTHORIZATION(SET_SESSION_AUTHORIZATION(7)) β the sibling command, definitely go read it. π«Ά
and that's the page, bestie! πΈ this was PostgreSQL 17.11, dated 2026 in the footer β mwah, go forth and roleplay responsibly ππ β¨
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-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