Appearance
CREATE ROLE
Create a database role (or user) and define attributes for the role.
Users and roles are virtually the same thing. You can use a CREATE ROLE statement to create a new role that takes on the privileges granted to an existing role. In that sense, users may become a member of a role and accrue privileges from that role.
CREATE USER syntax is accepted; however, CREATE USER and CREATE ROLE are synonymous, except for one important difference in behavior. When you use CREATE USER, the LOGIN attribute is assumed by default. However, NOLOGIN is the default when you use CREATE ROLE.
Only users and roles that have CREATE ROLE privilege can create other roles and users. This privilege can be inherited or granted:
- Inherited when the
CREATEROLEoption is used in the CREATE ROLE statement - Granted explicitly with a GRANT CREATE ROLE ON SYSTEM statement
Note the difference between attributes assigned to roles when they are created and grantable privileges that may be subsequently given to roles or accrued via role membership. With the exception of CREATEDB/NOCREATEDB and CREATEROLE/NOCREATEROLE, the options defined in the CREATE ROLE statement are attributes, not privileges.
CREATE ROLE name [ [ WITH ] option [ ... ] ]
where option can be:
SUPERUSER | NOSUPERUSER
| CREATEDB | NOCREATEDB
| CREATEROLE | NOCREATEROLE
| INHERIT | NOINHERIT
| LOGIN | NOLOGIN
| CONNECTION LIMIT connlimit
| [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password'
| VALID UNTIL 'timestamp'
| IN ROLE role_name [, ...]
| ROLE role_name [, ...]
| ADMIN role_name [, ...]
| USER role_name [, ...]- name
Specify the role or user name, using a quoted or unquoted identifier. See SQL Identifiers.
- SUPERUSER | NOSUPERUSER
Whether the role can override all access restrictions for the database. Only a superuser can create a new superuser.
NOSUPERUSERis the default. Assign this attribute only when it is really needed. You cannot grantSUPERUSERprivilege.- CREATEDB | NOCREATEDB
Whether the role can create databases.
NOCREATEDBis the default. Ability to create databases is inheritable when users belong to a role that has that privilege.- CREATEROLE | NOCREATEROLE
Whether the role can create new roles (execute CREATE ROLE). A role with this attribute can also alter and drop roles.
NOCREATEROLEis the default. Ability to create roles is inheritable when users belong to a role that has that privilege.- INHERIT | NOINHERIT
Whether a role inherits the privileges of roles that it is a member of.
NOINHERITmeans that membership in another role only grants the ability toSET ROLEto that other role. The privileges of the other role become available only after doing this.INHERITis the default behavior.INHERITrefers to inheritance of grantable privileges (system-level privileges, access to database objects, and memberships in roles,CREATEDB,CREATEROLE). A role with theINHERITattribute can automatically use whatever privileges have been granted to all roles it is directly or indirectly a member of.- LOGIN
Whether a role is allowed to log in (in other words, used as the initial session authorization name for a client connection). Think of a role that has
LOGINattribute as a user. Roles that cannot log in are useful for managing database privileges.NOLOGINis the default, except when the alternativeCREATE USERsyntax is used.CREATE USERcreates a user (role) with defaultLOGINcapability.- CONNECTION LIMIT limit
For a role with the
LOGINattribute, the concurrent connection limit for the role. The default is-1(no limit).- ENCRYPTED | UNENCRYPTED PASSWORD 'password'
For a role with the
LOGINattribute, define a password for password authentication. If no password is specified, the password is set to null and authentication always fails for this user.PASSWORD NULLalso sets the password to null.You can store the password
ENCRYPTEDin the system catalogs. The default encryption behavior depends on the setting of thepassword_encryptionconfiguration parameter.) If the password string is already MD5-encrypted, it is stored encrypted as is, regardless of theENCRYPTEDorUNENCRYPTEDoption.CAUTION:
If you create and update unencrypted passwords with CREATE ROLE and ALTER ROLE commands, passwords are transmitted in clear text and may appear in log messages. A more secure alternative is the
ybsql \passwordcommand.- VALID UNTIL 'timestamp'
Expiration date and time for the password. If you do not set a date and time, the password remains valid for all time.
- IN ROLE role_name
One or more existing roles that this role will become a member of. You cannot add the new role as an administrator; use the GRANT command.
- ROLE role_name
One or more existing roles that will become members of this role. (The new role effectively becomes a "group.")
USER role_nameis alternative syntax for theROLEclause.- ADMIN role_name
Like the ROLE clause, except that the named roles become members of the new role
WITH ADMIN OPTION, which means they can grant membership in this role to others.
Examples
Create a role with password authentication and the ability to create other roles and create databases.
premdb=# create role ybuser1 createdb createrole login
encrypted password '!@#$%900' valid until '2016-12-31 23:59:59';
CREATE ROLECreate a set of LOGIN users with the INHERIT option.
premdb=# create role alex login inherit;
CREATE ROLE
premdb=# create role vicky login inherit;
CREATE ROLE
premdb=# create role henry login inherit;
CREATE ROLE
premdb=# create role niklas login inherit;
CREATE ROLE
premdb=# \du
List of roles
Role name | Attributes | Member of
------------------------+-----------------------------------+------------------------
alex | | {}
henry | | {}
niklas | | {}
vicky | | {}
...Create a role (or group) to which these four users belong:
premdb=# create role allqa nologin createdb role alex, vicky, henry, niklas;
CREATE ROLE
premdb=# \du
List of roles
Role name | Attributes | Member of
-------------+------------------------------------------------------------+-----------
alex | | {allqa}
allqa | Create DB, Cannot login | {}
henry | | {allqa}
niklas | | {allqa}
vicky | | {allqa}
...Note that the CREATEDB attribute is inherited, so the allqa members can run as themselves and create databases. (They do not need to use SET ROLE to run as allqa.)
premdb=# premdb=# \c premdb alex
You are now connected to database "premdb" as user "alex".
premdb=> create database alexdb;
CREATE DATABASEParent topic:SQL Commands