SET_SESSION_AUTHORIZATION(7)

โœจ SET_SESSION_AUTHORIZATION โœจ

okay bestie so this is the PostgreSQL 18.6 Documentation page for SET_SESSION_AUTHORIZATION โ€” it's literally the command that sets the session user identifier and the current user identifier of the current session. two identifiers, one glow-up. ๐Ÿ’…


๐ŸŒธ NAME ๐ŸŒธ

SET_SESSION_AUTHORIZATION โ€” set the session user identifier and the current user identifier of the current session ๐Ÿซถ


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

SET [ SESSION | LOCAL ] SESSION AUTHORIZATION user_name
SET [ SESSION | LOCAL ] SESSION AUTHORIZATION DEFAULT
RESET SESSION AUTHORIZATION

๐Ÿ’– DESCRIPTION ๐Ÿ’–

So this command sets the session user identifier and the current user identifier of the current SQL session to be user_name. and like, the user name can be written as either an identifier or a string literal โ€” both are valid, pick your fighter. โœจ

Using this command, it's possible to, for example, temporarily become an unprivileged user and later switch back to being a superuser. it's giving "main character for a sec, then back to admin mode." ๐Ÿ‘‘

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 โ€” and it can also be changed by SET ROLE. the current user identifier is the one that's relevant for permission checking, so like, that's the one holding the clipboard at the door. ๐Ÿ“‹

here's the big rule though, no cap: 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 if you're not a superuser, you can only be yourself. iconic but also, like, restrictive. ๐Ÿ’€

The SESSION and LOCAL modifiers act the same as for the regular SET command. same vibe, different command. ๐Ÿฆ‹

The DEFAULT and RESET forms reset the session and current user identifiers to be the originally authenticated user name. and good news bestie โ€” these forms can be executed by any user. so everyone gets to come home. ๐Ÿ ๐Ÿ’œ


๐Ÿงš NOTES ๐Ÿงš

SET SESSION AUTHORIZATION cannot be used within a SECURITY DEFINER function. that's it, that's the note. not allowed, don't try it. ๐Ÿšซ


๐ŸŒˆ 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

look at peter becoming paul. a transformation arc. ๐Ÿ’ซ


โญ 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. fr fr, nobody's using them. ๐Ÿ™ˆ

PostgreSQL allows identifier syntax ("username"), which SQL does not. so we're being a little extra, but in a good way. โœจ

SQL does not allow this command during a transaction; PostgreSQL does not make this restriction because there is no reason to. we simply saw no reason and moved on. ๐Ÿ’…

The SESSION and LOCAL modifiers are a PostgreSQL extension, as is the RESET syntax. yes, we added those. you're welcome. ๐Ÿซถ

The privileges necessary to execute this command are left implementation-defined by the standard. so the standard justโ€ฆ didn't decide. wild. ๐Ÿ˜ญ


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

SET ROLE (SET_ROLE(7))


that's the whole page, bestie โ€” go forth and switch identities responsibly. and yeah, this was PostgreSQL 18.6, dated 2026. mwah. ๐Ÿ’–๐ŸŽ€๐Ÿฆ‹๐ŸŒธโœจ