1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>SET ROLE</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-set-constraints.html" title="SET CONSTRAINTS" /><link rel="next" href="sql-set-session-authorization.html" title="SET SESSION AUTHORIZATION" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">SET ROLE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-set-constraints.html" title="SET CONSTRAINTS">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-set-session-authorization.html" title="SET SESSION AUTHORIZATION">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-SET-ROLE"><div class="titlepage"></div><a id="id-1.9.3.176.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">SET ROLE</span></h2><p>SET ROLE — set the current user identifier of the current session</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
3 SET [ SESSION | LOCAL ] ROLE <em class="replaceable"><code>role_name</code></em>
4 SET [ SESSION | LOCAL ] ROLE NONE
6 </pre></div><div class="refsect1" id="id-1.9.3.176.5"><h2>Description</h2><p>
7 This command sets the current user
8 identifier of the current SQL session to be <em class="replaceable"><code>role_name</code></em>. The role name can be
9 written as either an identifier or a string literal.
10 After <code class="command">SET ROLE</code>, permissions checking for SQL commands
11 is carried out as though the named role were the one that had logged
12 in originally. Note that <code class="command">SET ROLE</code> and
13 <code class="command">SET SESSION AUTHORIZATION</code> are exceptions; permissions
14 checks for those continue to use the current session user and the initial
15 session user (the <em class="firstterm">authenticated user</em>), respectively.
17 The current session user must have the <code class="literal">SET</code> option for the
18 specified <em class="replaceable"><code>role_name</code></em>, either
19 directly or indirectly via a chain of memberships with the
20 <code class="literal">SET</code> option.
21 (If the session user is a superuser, any role can be selected.)
23 The <code class="literal">SESSION</code> and <code class="literal">LOCAL</code> modifiers act the same
24 as for the regular <a class="link" href="sql-set.html" title="SET"><code class="command">SET</code></a>
27 <code class="literal">SET ROLE NONE</code> sets the current user identifier to the
28 current session user identifier, as returned by
29 <code class="function">session_user</code>. <code class="literal">RESET ROLE</code> sets the
30 current user identifier to the connection-time setting specified by the
31 <a class="link" href="libpq-connect.html#LIBPQ-CONNECT-OPTIONS">command-line options</a>,
32 <a class="link" href="sql-alterrole.html" title="ALTER ROLE"><code class="command">ALTER ROLE</code></a>, or
33 <a class="link" href="sql-alterdatabase.html" title="ALTER DATABASE"><code class="command">ALTER DATABASE</code></a>,
34 if any such settings exist. Otherwise, <code class="literal">RESET ROLE</code> sets
35 the current user identifier to the current session user identifier. These
36 forms can be executed by any user.
37 </p></div><div class="refsect1" id="id-1.9.3.176.6"><h2>Notes</h2><p>
38 Using this command, it is possible to either add privileges or restrict
39 one's privileges. If the session user role has been granted memberships
40 <code class="literal">WITH INHERIT TRUE</code>, it automatically has all the
41 privileges of every such role. In this case, <code class="command">SET ROLE</code>
42 effectively drops all the privileges except for those which the target role
43 directly possesses or inherits. On the other hand, if the session user role
44 has been granted memberships <code class="literal">WITH INHERIT FALSE</code>, the
45 privileges of the granted roles can't be accessed by default. However, if
46 the role was granted <code class="literal">WITH SET TRUE</code>, the
47 session user can use <code class="command">SET ROLE</code> to drop the privileges
48 assigned directly to the session user and instead acquire the privileges
49 available to the named role. If the role was granted <code class="literal">WITH INHERIT
50 FALSE, SET FALSE</code> then the privileges of that role cannot be
51 exercised either with or without <code class="literal">SET ROLE</code>.
53 <code class="command">SET ROLE</code> has effects comparable to
54 <a class="link" href="sql-set-session-authorization.html" title="SET SESSION AUTHORIZATION"><code class="command">SET SESSION AUTHORIZATION</code></a>, but the privilege
55 checks involved are quite different. Also,
56 <code class="command">SET SESSION AUTHORIZATION</code> determines which roles are
57 allowable for later <code class="command">SET ROLE</code> commands, whereas changing
58 roles with <code class="command">SET ROLE</code> does not change the set of roles
59 allowed to a later <code class="command">SET ROLE</code>.
61 <code class="command">SET ROLE</code> does not process session variables as specified by
62 the role's <a class="link" href="sql-alterrole.html" title="ALTER ROLE"><code class="command">ALTER ROLE</code></a> settings; this only happens during
65 <code class="command">SET ROLE</code> cannot be used within a
66 <code class="literal">SECURITY DEFINER</code> function.
67 </p></div><div class="refsect1" id="id-1.9.3.176.7"><h2>Examples</h2><pre class="programlisting">
68 SELECT SESSION_USER, CURRENT_USER;
70 session_user | current_user
71 --------------+--------------
76 SELECT SESSION_USER, CURRENT_USER;
78 session_user | current_user
79 --------------+--------------
81 </pre></div><div class="refsect1" id="id-1.9.3.176.8"><h2>Compatibility</h2><p>
82 <span class="productname">PostgreSQL</span>
83 allows identifier syntax (<code class="literal">"<em class="replaceable"><code>rolename</code></em>"</code>), while
84 the SQL standard requires the role name to be written as a string
85 literal. SQL does not allow this command during a transaction;
86 <span class="productname">PostgreSQL</span> does not make this
87 restriction because there is no reason to.
88 The <code class="literal">SESSION</code> and <code class="literal">LOCAL</code> modifiers are a
89 <span class="productname">PostgreSQL</span> extension, as is the
90 <code class="literal">RESET</code> syntax.
91 </p></div><div class="refsect1" id="id-1.9.3.176.9"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-set-session-authorization.html" title="SET SESSION AUTHORIZATION"><span class="refentrytitle">SET SESSION AUTHORIZATION</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-set-constraints.html" title="SET CONSTRAINTS">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-set-session-authorization.html" title="SET SESSION AUTHORIZATION">Next</a></td></tr><tr><td width="40%" align="left" valign="top">SET CONSTRAINTS </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> SET SESSION AUTHORIZATION</td></tr></table></div></body></html>