SET_SESSION_AUTHORIZATION(7pg16)
π SET_SESSION_AUTHORIZATION β bestie edition β¨
okay so real talk, this is SET_SESSION_AUTHORIZATION from the PostgreSQL 16.15 Documentation β yes the whole manual page, and no I'm not skipping anything, we're doing this properly π«Ά
π NAME
SET_SESSION_AUTHORIZATION β sets the session user identifier and the current user identifier of the current session. both of them. at once. slay π
β¨ SYNOPSIS
SET [ SESSION | LOCAL ] SESSION AUTHORIZATION user_name
SET [ SESSION | LOCAL ] SESSION AUTHORIZATION DEFAULT
RESET SESSION AUTHORIZATION
that's it, that's the whole vibe check πΈ
π DESCRIPTION
So this command sets the session user identifier and the current user identifier of your current SQL session to be user_name. and here's the cute part: the user name can be written as either an identifier or a string literal. either one, your choice, main character energy π
Using this, it's literally possible β for example β to temporarily become an unprivileged user and then later switch back to being a superuser. swap the fit, swap it back. iconic π¦
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 it 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 the one that's relevant for permission checking. iykyk π₯
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. no sneaky upgrades, bestie π
The SESSION and LOCAL modifiers act the same as for the regular SET command. same energy, different fit.
The DEFAULT and RESET forms reset the session and current user identifiers to be the originally authenticated user name. and the good news: these forms can be executed by any user. everyone eats π
β NOTES
SET SESSION AUTHORIZATIONcannot be used within aSECURITY DEFINERfunction. like, at all. not me finding this out the hard way π
π EXAMPLES
first, a little vibe check on who we even are:
SELECT SESSION_USER, CURRENT_USER;
session_user | current_user
--------------+--------------
peter | peter
then we switch it up:
SET SESSION AUTHORIZATION 'paul';
and check again:
SELECT SESSION_USER, CURRENT_USER;
session_user | current_user
--------------+--------------
paul | paul
peter β‘οΈ paul. the transformation. we love to see it π
π COMPATIBILITY
The SQL standard allows some other expressions to appear in place of the literal user_name, but honestly those options are not important in practice. lowkey irrelevant π§
PostgreSQL allows identifier syntax ("username"), which SQL does not. and SQL does not allow this command during a transaction β PostgreSQL does not make this restriction because there is no reason to. we're built different, periodt π
The SESSION and LOCAL modifiers are a PostgreSQL extension, as is the RESET syntax. extensions on extensions on extensions β¨
Also, the privileges necessary to execute this command are left implementation-defined by the standard. so, like, it's giving "you figure it out" π€·ββοΈ
π¦ SEE ALSO
SET ROLE(SET_ROLE(7)) β the other identity-crisis command, go read it too π«Ά
and that's the whole page, bestie! this lil teachable moment came to you courtesy of the PostgreSQL 16.15 docs, dated 2026 β mwah, go forth and SET SESSION AUTHORIZATION 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