SET ROLE

Synopsis

Use the SET ROLE statement to set the current user of the current session to be the specified user.

Syntax

set_role ::= SET [ SESSION | LOCAL ] ROLE { role_name | NONE }

reset_role ::= RESET ROLE

set_role

SETSESSIONLOCALROLErole_nameNONE

reset_role

RESETROLE

Semantics

The specified role_name must be a role that the current session user is a member of. Superusers can set to any role. Once the role is set to role_name, any further SQL commands will use the privileges available to that role.

To reset the role back to current user, RESET ROLE or SET ROLE NONE can be used.

Examples

  • Change to new role John.
yugabyte=# select session_user, current_user;
 session_user | current_user
--------------+--------------
 yugabyte     | yugabyte
(1 row)
yugabyte=# set role john;
SET
yugabyte=# select session_user, current_user;
 session_user | current_user
--------------+--------------
 yugabyte     | john
(1 row)
  • Changing to new role assumes the privileges available to that role.
yugabyte=# select session_user, current_user;
 session_user | current_user
--------------+--------------
 yugabyte     | yugabyte
(1 row)
yugabyte=# create database db1;
CREATE DATABASE
yugabyte=# set role john;
SET
yugabyte=# select session_user, current_user;
 session_user | current_user
--------------+--------------
 yugabyte     | john
(1 row)
yugabyte=# create database db2;
ERROR:  permission denied to create database

See also