SET ROLE

Synopsis

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

Syntax

  1. set_role ::= SET [ SESSION | LOCAL ] ROLE { role_name | NONE }
  2. reset_role ::= RESET ROLE

set_role

SET ROLE - 图1

reset_role

SET ROLE - 图2

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.
  1. yugabyte=# select session_user, current_user;
  2. session_user | current_user
  3. --------------+--------------
  4. yugabyte | yugabyte
  5. (1 row)
  6. yugabyte=# set role john;
  7. SET
  8. yugabyte=# select session_user, current_user;
  9. session_user | current_user
  10. --------------+--------------
  11. yugabyte | john
  12. (1 row)
  • Changing to new role assumes the privileges available to that role.
  1. yugabyte=# select session_user, current_user;
  2. session_user | current_user
  3. --------------+--------------
  4. yugabyte | yugabyte
  5. (1 row)
  6. yugabyte=# create database db1;
  7. CREATE DATABASE
  8. yugabyte=# set role john;
  9. SET
  10. yugabyte=# select session_user, current_user;
  11. session_user | current_user
  12. --------------+--------------
  13. yugabyte | john
  14. (1 row)
  15. yugabyte=# create database db2;
  16. ERROR: permission denied to create database

See also