SET_ROLE(7pg14)

πŸ“¦ version 14.24, in opensuse-leap 16.0

πŸ’… SET_ROLE β€” bestie's guide to becoming someone else (legally)

okay so this is the SET ROLE page from the PostgreSQL 14.24 docs, section 7 of the manual, and it's literally about ~swapping who you are~ inside your SQL session. iconic concept, zero cap. let's get into it 🫢


✨ NAME ✨

SET_ROLE β€” sets the current user identifier of the current session. yes, that's the whole vibe. you're changing who the session thinks you are.


πŸ¦‹ SYNOPSIS πŸ¦‹

SET [ SESSION | LOCAL ] ROLE role_name
SET [ SESSION | LOCAL ] ROLE NONE
RESET ROLE

three ways to do it, pick your fighter πŸ’–


🌸 DESCRIPTION 🌸

so this command sets the current user identifier of the current SQL session to be role_name. and the role name? you can write it as either an identifier or a string literal β€” both slay, both valid. after SET ROLE, permissions checking for SQL commands gets carried out as though the named role were the one that had logged in originally. so like… the database genuinely believes you're them now. main character swap πŸ”₯

the role_name you specify must be a role that the current session user is a member of. (and if the session user is a superuser, any role can be selected β€” superusers get the VIP list πŸ‘‘)

the SESSION and LOCAL modifiers act the exact same as they do for the regular SET command. no special treatment, they're just here doing their thing.

SET ROLE NONE sets the current user identifier to the current session user identifier, as returned by session_user. and RESET ROLE? it sets the current user identifier to the connection-time setting specified by the command-line options, ALTER ROLE, or ALTER DATABASE, if any such settings exist. otherwise, RESET ROLE sets the current user identifier to the current session user identifier. both of these forms can be executed by any user β€” no membership required, everyone's invited πŸŽ€


⭐ NOTES ⭐

ngl this part is where it gets juicy. using this command, it is possible to either add privileges or restrict your privileges. so it's a glow-up or a downgrade, depending on how you play it 😭

  • if the session user role has the INHERIT attribute, then it automatically has all the privileges of every role that it could SET ROLE to. in this case, SET ROLE effectively drops all the privileges assigned directly to the session user and to the other roles it is a member of, leaving only the privileges available to the named role. so you're not stacking privileges, you're switching them. lowkey a trade.
  • on the other hand, if the session user role has the NOINHERIT attribute, SET ROLE drops the privileges assigned directly to the session user and instead acquires the privileges available to the named role. same energy, different mechanics. πŸ’…
real talk though: in particular, when a superuser chooses to SET ROLE to a non-superuser role, they lose their superuser privileges. not a drill. your crown? gone. the moment you're in that role you're just a regular person again πŸ’€

SET ROLE has effects comparable to SET SESSION AUTHORIZATION, but the privilege checks involved are quite different. also, SET SESSION AUTHORIZATION determines which roles are allowable for later SET ROLE commands, whereas changing roles with SET ROLE does not change the set of roles allowed to a later SET ROLE. they're cousins, not twins 🧚

SET ROLE does not process session variables as specified by the role's ALTER ROLE settings; this only happens during login. so don't expect the full costume change, bestie β€” that's a login-time thing only.

⚠️ and one big one: SET ROLE cannot be used within a SECURITY DEFINER function. periodt. 🚫

πŸŽ€ EXAMPLES πŸŽ€

here's peter being peter, then peter becoming paul. character development:

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 stays peter but current_user flips to paul?? that's the whole point of the command in one screenshot. ate. 🌈


πŸ’œ COMPATIBILITY πŸ’œ

PostgreSQL allows identifier syntax ("rolename"), while the SQL standard requires the role name to be written as a string literal. so we're just being flexible, we're not being messy.

SQL does not allow this command during a transaction; PostgreSQL does not make this restriction because there is no reason to. genuinely no reason. so we simply don't ✨

the SESSION and LOCAL modifiers are a PostgreSQL extension, as is the RESET syntax. we did that. you're welcome 🎁


πŸ‘€ SEE ALSO πŸ‘€


k that's the whole page, you're officially caught up πŸ’– big kiss from the PostgreSQL 14.24 docs, dated 2026 β€” this is SET ROLE(7), signing off, mwah πŸŽ€βœ¨πŸ‘‘

πŸ“¦ where this page comes from