CREATE_ROLE(7pg16)
π¦ CREATE_ROLE(7pg16) π¦
okay bestie, this is the PostgreSQL 16.15 Documentation page for the CREATE_ROLE(7) manual page β literally the glow-up guide for making a new database role. No cap, it's giving admin-core π
.
π 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 new role to a PostgreSQL database cluster. A role is an entity that can own database objects and have database privileges; a role can be considered a βuserβ, a βgroupβ, or both depending on how it is used. Refer to Chapter 22 and Chapter 21 for information about managing users and authentication. You must have CREATEROLE privilege or be a database superuser to use this command.
Also important, bestie: roles are defined at the database cluster level, and so are valid in all databases in the cluster. cluster-wide queen behavior π
During role creation it is possible to immediately assign the newly created role to be a member of an existing role, and also assign existing roles to be members of the newly created role. The rules for which initial role membership options are enabled are described below in the IN ROLE, ROLE, and ADMIN clauses. The GRANT(7) command has fine-grained option control during membership creation, and the ability to modify these options after the new role is created.
π¦ PARAMETERS π¦
nameβ the name of the new role. name it something cute but usable, bestie π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. Only superuser roles or roles withCREATEDBcan specifyCREATEDB.CREATEROLE,NOCREATEROLEβ these clauses determine whether a role will be permitted to create, alter, drop, comment on, and change the security label for other roles. See role creation for more details about what capabilities are conferred by this privilege. If not specified,NOCREATEROLEis the default.INHERIT,NOINHERITβ this affects the membership inheritance status when this role is added as a member of another role, both in this and future commands. Specifically, it controls the inheritance status of memberships added with this command using theIN ROLEclause, and in later commands using theROLEclause. It is also used as the default inheritance status when adding this role as a member using theGRANTcommand. If not specified,INHERITis the default.
In PostgreSQL versions before 16, inheritance was a role-level attribute that controlled all runtime membership checks for that role. lore drop β¨
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.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. Only superuser roles or roles withREPLICATIONcan specifyREPLICATION.BYPASSRLS,NOBYPASSRLSβ these clauses determine whether a role bypasses every row-level security (RLS) policy.NOBYPASSRLSis the default. Only superuser roles or roles withBYPASSRLScan specifyBYPASSRLS.
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. <butterfly>dump-safe vibes π¦</butterfly>
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. pls don't do the ambiguous thing π
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 causes the new role to be automatically added as a member of the specified existing roles. The new membership will have theSEToption enabled and theADMINoption disabled. TheINHERIToption will be enabled unless theNOINHERIToption is specified.IN GROUP role_nameβIN GROUPis an obsolete spelling ofIN ROLE. vintage spelling, lowkey cute πROLE role_nameβ theROLEclause causes one or more specified existing roles to be automatically added as members, with theSEToption enabled. This in effect makes the new role a βgroupβ. Roles named in this clause with the role-levelINHERITattribute will have theINHERIToption enabled in the new membership. New memberships will have theADMINoption disabled.ADMIN role_nameβ theADMINclause has the same effect asROLE, but the named roles are added as members of the new role withADMINenabled, giving them the right to grant membership in the new 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 support π
π NOTES π
- Use
ALTER ROLEto change the attributes of a role, andDROP ROLEto 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
GRANTandREVOKE. - 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 role attributes defined here are non-inheritable, i.e., being a member of a role with, e.g.,
CREATEDBwill not allow the member to create new databases even if the membership grant has theINHERIToption. Of course, if the membership grant has theSEToption the member role would be able toSET ROLEto the createdb role and then create a new database. - The membership grants created by the
IN ROLE,ROLE, andADMINclauses have the role executing this command as the grantor. - 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. - 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. approximately is doing a lot of work there π
β οΈ Caution: 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 commandcreateuser(1), however, transmits the password encrypted. Also,psql(1)contains a command\passwordthat can be used to safely change the password later. do not send your password into the group chat bestie π
β¨ EXAMPLES β¨
Okay bestie, real talk β this is the part where it all clicks. Let's build some roles π
Wanna make a role that can log in but has no password? Iconic, no notes:
CREATE ROLE jonathan LOGIN;
Now one with a password, slay π
CREATE USER davide WITH PASSWORD 'jw8s0F4';
Just so we're clear:CREATE USERis the same asCREATE ROLEexcept that it impliesLOGIN. That's the whole difference, fr fr.
Here's a fun one β a password that's only valid until the end of 2004. The way this works: after one second has ticked in 2005, the password is no longer valid. Expired. Gone. π
CREATE ROLE miriam WITH LOGIN PASSWORD 'jw8s0F4' VALID UNTIL '2005-01-01';
And finally, the girlboss of the group chat β a role that can create databases and manage roles:
CREATE ROLE admin WITH CREATEDB CREATEROLE;
π COMPATIBILITY π
So CREATE ROLE is in the SQL standard, no cap. But lowkey the standard only requires this tiny little syntax:
CREATE ROLE name [ WITH ADMIN role_name ]
Everything else? Extra. Multiple initial administrators, plus all the other options of CREATE ROLE β those are all PostgreSQL extensions, bestie. We added them π
The SQL standard also defines users and roles as concepts, but it treats them as distinct concepts and justβ¦ leaves all the commands that define users up to each database implementation to figure out π. In PostgreSQL we chose to unify users and roles into a single kind of entity. So our roles end up having way more optional attributes than they do in the standard. That's just how we roll β¨
If you want the behavior the SQL standard specifies, the closest vibe is:
- SQL-standard users β PostgreSQL roles with the
NOINHERIToption - SQL-standard roles β PostgreSQL roles with the
INHERIToption
π¦ SEE ALSO π¦
Go read these next, iykyk:
SET ROLE(SET_ROLE(7))ALTER ROLE(ALTER_ROLE(7))DROP ROLE(DROP_ROLE(7))GRANT(7)REVOKE(7)createuser(1)createrole_self_grant
That's the whole page, you made it π«Ά This was PostgreSQL 16.15, dated 2026, and it's filed under CREATE ROLE(7) β go forth and grant responsibly, mwah π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-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