SET_SESSION_AUTHORIZATION(7pg15)
π NAME π
hiii bestie π«Ά this is SET_SESSION_AUTHORIZATION, aka page 7pg15 in the PostgreSQL 15 docs, and it's literally about setting the session user identifier and the current user identifier of the current session. main character identity-switch energy, no cap π
β¨ SYNOPSIS β¨
SET [ SESSION | LOCAL ] SESSION AUTHORIZATION user_name
SET [ SESSION | LOCAL ] SESSION AUTHORIZATION DEFAULT
RESET SESSION AUTHORIZATION
πΈ DESCRIPTION πΈ
okay so this command sets the session user identifier and the current user identifier of the current SQL session to be user_name. the user name can be written as either an identifier or a string literal. using this command, it is possible, for example, to temporarily become an unprivileged user and later switch back to being a superuser. it's giving "brb gonna go be someone else for a sec" β¨
the session user identifier is initially set to be the (possibly authenticated) user name provided by the client. the current user identifier is normally equal to the session user identifier, but might change temporarily in the context of SECURITY DEFINER functions and similar mechanisms; it can also be changed by SET ROLE. the current user identifier is relevant for permission checking. like, it's the one the system side-eyes when deciding what you're allowed to do π
the session user identifier can be changed only if the initial session user (the authenticated user) had the superuser privilege. otherwise, the command is accepted only if it specifies the authenticated user name. so yeah, not everyone can just waltz into a new identity β it's giving VIP-only π₯
the SESSION and LOCAL modifiers act the same as for the regular SET command.
the DEFAULT and RESET forms reset the session and current user identifiers to be the originally authenticated user name. these forms can be executed by any user. so if you need to snap back to your original self, that's there for you πͺ
π NOTES π
π¨ important caveat, bestie:SET SESSION AUTHORIZATIONcannot be used within aSECURITY DEFINERfunction.
π¦ EXAMPLES π¦
SELECT SESSION_USER, CURRENT_USER;
session_user | current_user
--------------+--------------
peter | peter
SET SESSION AUTHORIZATION 'paul';
SELECT SESSION_USER, CURRENT_USER;
session_user | current_user
--------------+--------------
paul | paul
π COMPATIBILITY π
the SQL standard allows some other expressions to appear in place of the literal user_name, but these options are not important in practice. PostgreSQL allows identifier syntax ("username"), which SQL does not. 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. periodt π
the privileges necessary to execute this command are left implementation-defined by the standard.
π SEE ALSO π
SET ROLE(SET_ROLE(7)) β go read that next if you wanna keep the role-switching lore going, iykyk π§
π source/version: PostgreSQL 15.19 Β· date: 2026 Β· that's the page, bestie! mwah ππ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-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