CREATE ROLE
The CREATE ROLE statement adds a new role. Roles are cluster-wide: one role exists across every database on the server. A role can own objects, hold privileges granted with GRANT, contain other roles as members, and — if it has the LOGIN attribute — connect to the server as a database user.
CREATE USER is the same statement, except the new role gets LOGIN by default; CREATE ROLE defaults to NOLOGIN.
Examples
Create a role. By default it cannot log in — a plain role is a bundle of privileges to grant to others:
CREATE ROLE doc_analyst;Create a role that can connect, authenticating with a password:
CREATE ROLE doc_app LOGIN PASSWORD 'app_secret';CREATE USER implies LOGIN:
CREATE USER doc_lead PASSWORD 'lead_secret';Give a role the CREATEDB and CREATEROLE attributes, allowing it to create databases and to manage other roles (the optional WITH keyword is noise, as in PostgreSQL):
CREATE ROLE doc_admins WITH CREATEDB CREATEROLE;Set an expiry on the password. After the timestamp passes, password authentication for the role fails:
CREATE ROLE doc_svc LOGIN PASSWORD 'svc_secret' VALID UNTIL '2099-12-31 00:00:00+00';Make the new role an immediate member of an existing role, inheriting its privileges:
CREATE ROLE doc_temp IN ROLE doc_admins;Contradictory attributes are rejected:
CREATE ROLE doc_bad LOGIN NOLOGIN;error db error: ERROR: conflicting or redundant optionsRole names are unique across the server:
CREATE ROLE doc_analyst;error db error: ERROR: role "doc_analyst" already existsNotes
SUPERUSERroles bypass all permission checks. Only a superuser can create another superuser.CONNECTION LIMITis accepted and stored (visible inpg_roles.rolconnlimit) but is not currently enforced at connect time.REPLICATION,BYPASSRLS, andSYSIDare accepted for PostgreSQL compatibility and have no effect.
See also
- ALTER ROLE — change a role's attributes or password
- DROP ROLE — remove roles
- GRANT — grant privileges or role membership
- SET ROLE — switch the current role within a session