SET_SESSION_AUTHORIZATION(7pg17)
π SET_SESSION_AUTHORIZATION (7pg17) β or, "how to swap your whole identity in SQL" π
okay bestie, so this is the page for SET_SESSION_AUTHORIZATION from the PostgreSQL 17.11 docs, and honestly? it's giving main character energy but for user identities. let's get into it β¨
π NAME π
SET_SESSION_AUTHORIZATION β sets the session user identifier and the current user identifier of the current session. that's it, that's the whole vibe check π«Ά
β¨ SYNOPSIS β¨
SET [ SESSION | LOCAL ] SESSION AUTHORIZATION user_name
SET [ SESSION | LOCAL ] SESSION AUTHORIZATION DEFAULT
RESET SESSION AUTHORIZATION
like, three whole ways to do the thing. pick your fighter π
πΈ DESCRIPTION πΈ
so this command sets the session user identifier and the current user identifier of the current SQL session to be user_name. and listen, user_name can be written as either an identifier or a string literal β either way slays. using this command it's possible, for example, to temporarily become an unprivileged user and later switch back to being a superuser. literally identity theft but consensual and SQL-sanctioned ππ
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 β also it can be changed by SET ROLE. and the current user identifier? that's the one that's actually relevant for permission checking, so don't sleep on it π
β οΈ important lore: the session user identifier can be changed only if the initial session user (the authenticated user) has the superuser privilege. otherwise, the command is accepted only if it specifies the authenticated user name. so like, no unauthorized glow-ups, bestie π
the SESSION and LOCAL modifiers act the same as for the regular SET command. same behavior, different context, iykyk π¦
and the DEFAULT and RESET forms? they reset the session and current user identifiers to be the originally authenticated user name. and these forms can be executed by any user. yes queen, everybody gets to come home π π
π NOTES π
SET SESSION AUTHORIZATION cannot be used within a SECURITY DEFINER function. periodt. no cap. π«
π₯ EXAMPLES π₯
okay so here's the whole glow-up in action:
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
the way peter simply becomes paul π iconic behavior, honestly π
π¦ 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. ngl, the standard is doing the most for no reason π
- 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. we're chill like that π
- the
SESSIONandLOCALmodifiers are a PostgreSQL extension, as is theRESETsyntax. extra, but make it fashion β¨
the privileges necessary to execute this command are left implementation-defined by the standard. so, like, the standard just handed it off and said "you figure it out" π
π SEE ALSO π
SET ROLE(SET_ROLE(7)) β your other fave identity-shifting icon, go read it next π«Ά
and that's the whole page, bestie! this adorable lil rewrite is brought to you by PostgreSQL 17.11, dated 2026 β same docs, same facts, way more sparkle ππβ¨π¦πΈ
π¦ 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