CREATE ROLE statement creates SQL roles, which are groups containing any number of roles and users as members. You can assign privileges to roles, and all members of the role (regardless of whether they are direct or indirect members) will inherit the role’s privileges.
You can use the keywords ROLE and USER interchangeably. CREATE USER is equivalent to CREATE ROLE, with one exception: CREATE ROLE sets the NOLOGIN role option, which prevents the new role from being used to log in to the database. You can use CREATE ROLE and specify the LOGIN role option to achieve the same result as CREATE USER.
Considerations
- After creating a role, you must grant it privileges to databases and tables
- All privileges of a role are inherited by all of its members
- Users and roles can be members of roles
- Role options of a role are not inherited by any of its members
- There is no limit to the number of members in a role
- Membership loops are not allowed (direct or indirect)
Required privileges
Unless a role is a member of theadmin role, additional privileges are required to manage other roles:
- To create other roles, a role must have the
CREATEROLErole option - To add the
LOGINcapability for other roles so that they can log in as users, a role must also have theCREATELOGINrole option - To grant or revoke membership to a role for additional roles, a member of the role must be set as a role admin for that role
Syntax
Parameters
Create a new role only if a role of the same name does not already exist. If one does exist, do not return an error.
The name of the role to create.Role names:
- Are case-insensitive
- Must start with either a letter or underscore
- Must contain only letters, numbers, periods, or underscores
- Must be between 1 and 63 characters
- Cannot be
none - Cannot start with
pg_orcrdb_internal - User and role names share the same namespace and must be unique
Apply a role option to the role.Available role options:
CREATEROLE/NOCREATEROLE- Allow/disallow the role to create, alter, and drop other rolesCREATEDB/NOCREATEDB- Allow/disallow the role to create databasesCREATELOGIN/NOCREATELOGIN- Allow/disallow the role to manage authentication usingWITH PASSWORD,VALID UNTIL, andLOGIN/NOLOGINLOGIN/NOLOGIN- Allow/disallow the role to log in with one of the client authentication methodsCONTROLCHANGEFEED/NOCONTROLCHANGEFEED- Allow/disallow the role to runCREATE CHANGEFEEDon tables they haveSELECTprivileges onMODIFYCLUSTERSETTING/NOMODIFYCLUSTERSETTING- Allow/disallow the role to modify cluster settings withSET CLUSTER SETTINGPASSWORD password/PASSWORD NULL- Set the role’s password for password authenticationVALID UNTIL timestamp- Set the expiration time for a role’s passwordVIEWACTIVITY/NOVIEWACTIVITY- Allow/disallow the role to see other users’ queries and sessionsVIEWACTIVITYREDACTED/NOVIEWACTIVITYREDACTED- Allow/disallow the role to see other users’ queries and sessions, but with masked constantsVIEWCLUSTERSETTING/NOVIEWCLUSTERSETTING- Allow/disallow the role to view cluster settingsCANCELQUERY/NOCANCELQUERY- Allow/disallow the role to cancel queriesVIEWCLUSTERMETADATA/NOVIEWCLUSTERMETADATA- Allow/disallow the role to view cluster metadata
Examples
Create a role
Create a role that can log in to the database
Prevent a role from using password authentication
The following statement prevents the role from using password authentication and mandates certificate-based client authentication:Create a role that can create other roles
The following example allows the role to create other users and manage authentication methods for them:Create a role that can create and rename databases
The following example allows the role to create or rename databases:See also
- ALTER ROLE
- DROP ROLE
- SHOW ROLES
- GRANT
- REVOKE
- CREATE USER