CREATE_ROLE(7pg13)
π
CREATE_ROLE(7) β PostgreSQL 13.23 Documentation, bestie edition π
β¨ NAME β¨
CREATE_ROLE β define a new database role
πΈ SYNOPSIS πΈ
CREATE ROLE name [ [ WITH ] option [ ... ] ]
where option can be:
SUPERUSER | NOSUPERUSER
| CREATEDB | NOCREATEDB
| CREATEROLE | NOCREATEROLE
| INHERIT | NOINHERIT
| LOGIN | NOLOGIN
| REPLICATION | NOREPLICATION
| BYPASSRLS | NOBYPASSRLS
| CONNECTION LIMIT connlimit
| [ ENCRYPTED ] PASSWORD 'password' | PASSWORD NULL
| VALID UNTIL 'timestamp'
| IN ROLE role_name [, ...]
| IN GROUP role_name [, ...]
| ROLE role_name [, ...]
| ADMIN role_name [, ...]
| USER role_name [, ...]
| SYSID uid
π DESCRIPTION π
Okay so CREATE ROLE adds a brand-new role to a PostgreSQL database cluster. A role is basically an entity that can own database objects and have database privileges β and honestly? A role can be a βuserβ, a βgroupβ, or both, depending on how you use it. Main character energy, truly.
Refer to Chapter 21 and Chapter 20 for the lore on managing users and authentication, bestie.
You must have CREATEROLE privilege or be a database superuser to use this command. No cap.
Note that roles are defined at the database cluster level, and so are valid in all databases in the cluster. π«
π PARAMETERS π
nameβ the name of the new role. Thatβs it, thatβs the name. πSUPERUSER/NOSUPERUSERβ these clauses determine whether the new role is a βsuperuserβ, who can override all access restrictions within the database. Superuser status is dangerous and should be used only when really needed. You must yourself be a superuser to create a new superuser. If not specified,NOSUPERUSERis the default. πCREATEDB/NOCREATEDBβ these clauses define a roleβs ability to create databases. IfCREATEDBis specified, the role being defined will be allowed to create new databases. SpecifyingNOCREATEDBwill deny a role the ability to create databases. If not specified,NOCREATEDBis the default. πCREATEROLE/NOCREATEROLEβ these clauses determine whether a role will be permitted to create, alter, drop, comment on, change the security label for, and grant or revoke membership in other roles. See role creation for more details about what capabilities are conferred by this privilege. If not specified,NOCREATEROLEis the default. πINHERIT/NOINHERITβ these clauses determine whether a role βinheritsβ the privileges of roles it is a member of. A role with theINHERITattribute can automatically use whatever database privileges have been granted to all roles it is directly or indirectly a member of. WithoutINHERIT, membership in another role only grants the ability toSET ROLEto that other role; the privileges of the other role are only available after having done so. If not specified,INHERITis the default. π§LOGIN/NOLOGINβ these clauses determine whether a role is allowed to log in; that is, whether the role can be given as the initial session authorization name during client connection. A role having theLOGINattribute can be thought of as a user. Roles without this attribute are useful for managing database privileges, but are not users in the usual sense of the word. If not specified,NOLOGINis the default, except whenCREATE ROLEis invoked through its alternative spellingCREATE USER(CREATE_USER(7)). π¦REPLICATION/NOREPLICATIONβ these clauses determine whether a role is a replication role. A role must have this attribute (or be a superuser) in order to be able to connect to the server in replication mode (physical or logical replication) and in order to be able to create or drop replication slots. A role having theREPLICATIONattribute is a very highly privileged role, and should only be used on roles actually used for replication. If not specified,NOREPLICATIONis the default. You must be a superuser to create a new role having theREPLICATIONattribute. π₯BYPASSRLS/NOBYPASSRLSβ these clauses determine whether a role bypasses every row-level security (RLS) policy.NOBYPASSRLSis the default. You must be a superuser to create a new role having theBYPASSRLSattribute. π
Note that pg_dump will set row_security to OFF by default, to ensure all contents of a table are dumped out. If the user running pg_dump does not have appropriate permissions, an error will be returned. However, superusers and the owner of the table being dumped always bypass RLS.
CONNECTION LIMIT connlimitβ if role can log in, this specifies how many concurrent connections the role can make.-1(the default) means no limit. Note that only normal connections are counted towards this limit. Neither prepared transactions nor background worker connections are counted towards this limit. π’[ ENCRYPTED ] PASSWORD 'password'/PASSWORD NULLβ sets the roleβs password. (A password is only of use for roles having theLOGINattribute, but you can nonetheless define one for roles without it.) If you do not plan to use password authentication you can omit this option. If no password is specified, the password will be set to null and password authentication will always fail for that user. A null password can optionally be written explicitly asPASSWORD NULL. π
Note: Specifying an empty string will also set the password to null, but that was not the case before PostgreSQL version 10. In earlier versions, an empty string could be used, or not, depending on the authentication method and the exact version, and libpq would refuse to use it in any case. To avoid the ambiguity, specifying an empty string should be avoided. π
The password is always stored encrypted in the system catalogs. The ENCRYPTED keyword has no effect, but is accepted for backwards compatibility. The method of encryption is determined by the configuration parameter password_encryption. If the presented password string is already in MD5-encrypted or SCRAM-encrypted format, then it is stored as-is regardless of password_encryption (since the system cannot decrypt the specified encrypted password string, to encrypt it in a different format). This allows reloading of encrypted passwords during dump/restore. π
VALID UNTIL 'timestamp'β theVALID UNTILclause 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β theIN ROLEclause 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 separateGRANTcommand to do that.) π«ΆIN GROUP role_nameβIN GROUPis an obsolete spelling ofIN ROLE. Vintage, but make it fashion. πROLE role_nameβ theROLEclause 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β theADMINclause is likeROLE, but the named roles are added to the new roleWITH ADMIN OPTION, giving them the right to grant membership in this role to others. πUSER role_nameβ theUSERclause is an obsolete spelling of theROLEclause. π¦SYSID uidβ theSYSIDclause is ignored, but is accepted for backwards compatibility. Itβs giving legacy icon behavior. π
π¦ NOTES π¦
- Use
ALTER ROLE(ALTER_ROLE(7)) to change the attributes of a role, andDROP ROLE(DROP_ROLE(7)) to remove a role. All the attributes specified byCREATE ROLEcan be modified by laterALTER ROLEcommands. β¨ - The preferred way to add and remove members of roles that are being used as groups is to use
GRANT(7)andREVOKE(7). π«Ά - The
VALID UNTILclause defines an expiration time for a password only, not for the role per se. In particular, the expiration time is not enforced when logging in using a non-password-based authentication method. β° - The
INHERITattribute governs inheritance of grantable privileges (that is, access privileges for database objects and role memberships). It does not apply to the special role attributes set byCREATE ROLEandALTER ROLE. For example, being a member of a role withCREATEDBprivilege does not immediately grant the ability to create databases, even ifINHERITis set; it would be necessary to become that role viaSET ROLE(SET_ROLE(7)) before creating a database. π§ - The
INHERITattribute is the default for reasons of backwards compatibility: in prior releases of PostgreSQL, users always had access to all privileges of groups they were members of. However,NOINHERITprovides a closer match to the semantics specified in the SQL standard. π - Be careful with the
CREATEROLEprivilege. There is no concept of inheritance for the privileges of aCREATEROLE-role. That means that even if a role does not have a certain privilege but is allowed to create other roles, it can easily create another role with different privileges than its own (except for creating roles with superuser privileges). For example, if the role βuserβ has theCREATEROLEprivilege but not theCREATEDBprivilege, nonetheless it can create a new role with theCREATEDBprivilege. Therefore, regard roles that have theCREATEROLEprivilege as almost-superuser-roles. π¨ - PostgreSQL includes a program
createuser(1)that has the same functionality asCREATE ROLE(in fact, it calls this command) but can be run from the command shell. π» - The
CONNECTION LIMIToption is only enforced approximately; if two new sessions start at about the same time when just one connection βslotβ remains for the role, it is possible that both will fail. Also, the limit is never enforced for superusers. ποΈ - Caution must be exercised when specifying an unencrypted password with this command. The password will be transmitted to the server in cleartext, and it might also be logged in the clientβs command history or the server log. The command
createuser(1), however, transmits the password encrypted. Also,psql(1)contains a command\passwordthat can be used to safely change the password later. π₯π
β¨ EXAMPLES β¨
Okay bestie, this is the part where it all clicks, fr fr. Watch and learn π
So say you wanna make a role that can actually log in, but you're not about to hand it a password (bold move, but okay, we respect it):
CREATE ROLE jonathan LOGIN;
Now what if you do want a password? Say less:
CREATE USER davide WITH PASSWORD 'jw8s0F4';
And just so we're clear: CREATE USER is literally the same as CREATE ROLE except that it implies LOGIN. That's the whole difference. That's it. That's the tweet. π¦
Next vibe: a password with an expiry date. This one is valid until the end of 2004 β and the second one single second ticks over into 2005, that password is no longer valid. Like, gone. Expired. Bye. π
CREATE ROLE miriam WITH LOGIN PASSWORD 'jw8s0F4' VALID UNTIL '2005-01-01';
And last but never least, the boss-energy role that can create databases and manage roles β main character energy, honestly π
CREATE ROLE admin WITH CREATEDB CREATEROLE;
π COMPATIBILITY π
So here's the tea on standards, bestie π« The CREATE ROLE statement is in the SQL standard β but the standard is out here being minimal, because it only requires this syntax:
CREATE ROLE name [ WITH ADMIN role_name ]
Everything else? Yeah β multiple initial administrators, and all the other options of CREATE ROLE, are PostgreSQL extensions. Homegrown. Special. Ours. β¨
Also, the SQL standard defines the concepts of users and roles, but it treats them as distinct concepts and just leaves all the commands defining users to be specified by each database implementation. Basically the standard said "not my problem, figure it out" π
In PostgreSQL we chose to unify users and roles into a single kind of entity. Slay, honestly. The consequence though? Roles therefore have many more optional attributes than they do in the standard β more knobs, more options, more to love π
And if you're wondering which one is "correct" per the standard: the behavior specified by the SQL standard is most closely approximated by giving users the NOINHERIT attribute, while roles are given the INHERIT attribute. That's the matchup. Remember it. π
π¦ SEE ALSO π¦
The friend group, the supporting cast, the ones you should also go read:
SET ROLE(SET_ROLE(7))ALTER ROLE(ALTER_ROLE(7))DROP ROLE(DROP_ROLE(7))GRANT(7)REVOKE(7)createuser(1)
And that's the whole page, bestie β you ate that π«Ά This little masterpiece comes to you courtesy of PostgreSQL 13.23, dated 2025, and it lives at the iconic address CREATE ROLE(7). Go forth and make roles responsibly, mwah ππΈ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms