CREATE_ROLE(7pg15)
π¦ CREATE_ROLE(7pg15) π¦
okay bestie, this is the PostgreSQL 15.19 Documentation page for making brand-new database roles, and yes it is giving main character energy β¨
πΈ 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 π
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; depending on how itβs used, a role can be considered a βuserβ, a βgroupβ, or both π«Ά.
Refer to Chapter 22 and Chapter 21 for the tea on managing users and authentication β. 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. Cluster-wide queen behavior π
π PARAMETERS π
nameβ the name of the new role. Name it something cute but legal, 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. So like, proceed with caution, okay? π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. Inheritance slay π«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. You must be a superuser to create a new role having theREPLICATIONattribute. Replication is not a casual vibe, bestie π₯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. Please 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. Secure storage, iconic behavior π
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. Forever-and-ever password energy, unless you say otherwise π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.) No sneaky admin shortcut here, bestie.IN GROUP role_nameβIN GROUPis an obsolete spelling ofIN ROLE.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β.) Group-chat energy fr fr π«Ά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. Admin privileges: use wisely πUSER role_nameβ theUSERclause is an obsolete spelling of theROLEclause.SYSID uidβ theSYSIDclause is ignored, but is accepted for backwards compatibility. Itβs literally just vibing there for old timesβ sake π
π NOTES π
Use ALTER ROLE to change the attributes of a role, and DROP ROLE to remove a role. All the attributes specified by CREATE ROLE can be modified by later ALTER ROLE commands. Glow-up and cleanup are both possible β¨
The preferred way to add and remove members of roles that are being used as groups is to use GRANT and REVOKE.
The VALID UNTIL clause 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. So donβt think it expires the whole role, because it does not π«Ά
The INHERIT attribute 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 by CREATE ROLE and ALTER ROLE. For example, being a member of a role with CREATEDB privilege does not immediately grant the ability to create databases, even if INHERIT is set; it would be necessary to become that role via SET ROLE before creating a database. This is a very important distinction, bestie π
The INHERIT attribute 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, NOINHERIT provides a closer match to the semantics specified in the SQL standard. Old habits, but make it standards-adjacent π
Be careful with the CREATEROLE privilege. There is no concept of inheritance for the privileges of a CREATEROLE-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 the CREATEROLE privilege but not the CREATEDB privilege, nonetheless it can create a new role with the CREATEDB privilege. Therefore, regard roles that have the CREATEROLE privilege as almost-superuser-roles. That is not a drill π₯
PostgreSQL includes a program createuser(1) that has the same functionality as CREATE ROLE (in fact, it calls this command) but can be run from the command shell.
The CONNECTION LIMIT option 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. Approximate limits are still limits, but likeβ¦ approximately π
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 \password that can be used to safely change the password later. Please protect the password, bestie π«Άπ
π EXAMPLES π
Create a role that can log in, but donβt give it a password:
CREATE ROLE jonathan LOGIN;
Create a role with a password:
CREATE USER davide WITH PASSWORD 'jw8s0F4';
(CREATE USER is the same as CREATE ROLE except that it implies LOGIN.)
Create a role with a password that is valid until the end of 2004. After one second has ticked in 2005, the password is no longer valid.
CREATE ROLE miriam WITH LOGIN PASSWORD 'jw8s0F4' VALID UNTIL '2005-01-01';
Create a role that can create databases and manage roles:
CREATE ROLE admin WITH CREATEDB CREATEROLE;
π« COMPATIBILITY π«
Okay so bestie, plot twist: CREATE ROLE is in the SQL standard π β but the standard is out here doing the absolute bare minimum, like showing up to the group project having written two lines and calling it done. The only syntax the standard actually requires is this:
CREATE ROLE name [ WITH ADMIN role_name ]
That's it. That's the whole mandatory menu. π
Everything else β multiple initial administrators, and literally all the other options of CREATE ROLE β those are PostgreSQL extensions. Very "we added extra toppings because we love you" of them, honestly. π
So here's the lore: the SQL standard has this whole thing where it defines the concepts of users and roles, but it treats them as distinct concepts π³ β and it just leaves all the commands that define users up to each database implementation to figure out. Not it handing out homework like that. π
In PostgreSQL, we made a different choice: we unified users and roles into a single kind of entity. One entity, one vibe, very main character energy π. Which means roles in PostgreSQL end up carrying many more optional attributes than roles do in the standard β like, the standard's role is wearing jeans and a tee and our role is fully accessorized. π
And if you're wondering how to get closest to what the standard actually specifies? The behavior specified by the SQL standard is most closely approximated by giving users the NOINHERIT attribute, while roles are given the INHERIT attribute. Match made in heaven, no cap. β¨
Not me telling you to go read the standard and then read our extensions and then compare them. You could though. Iykyk. π¦
π SEE ALSO π
Go touch these other pages, bestie β they're all part of the same friend group:
SET ROLE(SET_ROLE(7))ALTER ROLE(ALTER_ROLE(7))DROP ROLE(DROP_ROLE(7))GRANT(GRANT(7))REVOKE(REVOKE(7))createuser(createuser(1))
Like, read the whole set, it's giving β¨completionistβ¨ and honestly you'll be so glad you did. π«Ά
Ta-ta for now, gorgeous! π This little love letter came to you courtesy of PostgreSQL 15.19, dated 2026, and it's filed in the manuals as CREATE ROLE(7) β go forth and create roles responsibly, mwah! π
ππβ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-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