CREATE ROLE

Synopsis

CREATE ROLE adds a new role to a YugabyteDB database cluster. A role is an entity that can own database objects and have database privileges.A role can be a user or a group, depending on how it is used. A role with atttribute LOGIN can be considered as a “user”.You must have CREATEROLE privilege or be a database superuser to use this command.

Note that roles are defined at the YSQL cluster level, and so are valid in all databases in the cluster.

You can use GRANT/REVOKE commands to set/remove permissions for roles.

Syntax

  1. create_role ::= CREATE ROLE role_name
  2. [ [ WITH ] role_option [ , ... ] ]
  3. role_option ::= SUPERUSER
  4. | NOSUPERUSER
  5. | CREATEDB
  6. | NOCREATEDB
  7. | CREATEROLE
  8. | NOCREATEROLE
  9. | INHERIT
  10. | NOINHERIT
  11. | LOGIN
  12. | NOLOGIN
  13. | CONNECTION LIMIT connlimit
  14. | [ ENCRYPTED ] PASSWORD ' password '
  15. | PASSWORD NULL
  16. | VALID UNTIL ' timestamp '
  17. | IN ROLE role_name [ , ... ]
  18. | IN GROUP role_name [ , ... ]
  19. | ROLE role_name [ , ... ]
  20. | ADMIN role_name [ , ... ]
  21. | USER role_name [ , ... ]
  22. | SYSID uid

create_role

CREATE ROLE - 图1

role_option

CREATE ROLE - 图2

Where

  • role_name is the name of the new role.
  • SUPERUSER, NOSUPERUSER determine whether the new role is a “superuser” or not. Superusers can override all access restrictions and should be used with care.Only roles with SUPERUSER privilege can create other SUPERUSER roles. If not specified, NOSUPERUSER is the default.
  • CREATEDB, NOCREATEDB determine whether the new role can create a database or not. Default is NOCREATEDB.
  • CREATEROLE, NOCREATEROLE determine whether the new role can create other roles or not. Default is NOCREATEROLE.
  • INHERIT, NOINHERIT determine whether the new role inherits privileges of the roles that it is a member of.Without INHERIT, membership in another role only grants the ability to SET ROLE to that other role. The privileges of the other role are only available after having done so.If not specified, INHERIT is the default.
  • LOGIN, NOLOGIN determine whether the new role is allowed to login or not. Only roles with login privilege can be used during client connection.A role with LOGIN can be thought of as a user. If not specified, NOLOGIN is the default. Note that if CREATE USER statement is used instead of CREATE ROLE, then default is LOGIN.
  • CONNECTION LIMIT specifies how many concurrent connections the role can make. Default is -1 which means unlimited. This only applies to roles that can login.
  • [ENCRYPTED] PASSWORD sets the password for the new role. This only applies to roles that can login.If no password is specified, the password will be set to null and password authentication will always fail for that user.Note that password is always stored encrypted in system catalogs and the optional keyword ENCRYPTED is only present for compatibility with Postgres.
  • VALID UNTIL sets a date and time after which the role’s password is no longer valid. If this clause is omitted the password will be valid for all time.
  • IN ROLE role_name, IN GROUP role_name lists one or more existing roles to which the new role will be immediately added as a new member. (Note that there is no option to add the new role as an administrator; use a separate GRANT command to do that.)
  • ROLE role_name, USER role_name lists one or more existing roles which are automatically added as members of the new role. (This in effect makes the new role a “group”.)
  • ADMIN role_name is similar to ROLE role_name, but the named roles are added to the new role WITH ADMIN OPTION, giving them the right to grant membership in this role to others.
  • SYSID uid is ignored and present for compatibility with Postgres.

Examples

  • Create a role that can login.
  1. yugabyte=# CREATE ROLE John LOGIN;
  • Create a role that can login and has a password.
  1. yugabyte=# CREATE ROLE Jane LOGIN PASSWORD 'password';
  • Create a role that can manage databases and roles.
  1. yugabyte=# CREATE ROLE SysAdmin CREATEDB CREATEROLE;

See also