Sub topic 1
Roles: one concept for users and groups
Prerequisites
This chapter assumes you can run basic SQL (CREATE TABLE, SELECT, INSERT) and connect to PostgreSQL with psql or another client. It also assumes you know that a server (called a cluster) holds databases, and a database holds schemas and tables. To run the examples, connect as a superuser such as postgres, or as a role that has the CREATEROLE attribute.
One concept instead of two
Many database systems have separate objects for users and groups. PostgreSQL has one object, the role. A role can act as a user, as a group, or as both, depending on how you set it up:
- A role that can log in behaves like a user. You connect to the server as that role.
- A role that other roles belong to behaves like a group. You grant privileges to it once, and its members use them.
Because both are the same kind of object, any role can have members, and any role can be a member of another. The rest of this tutorial depends on this model, so it is worth getting clear now.
Creating roles
The statement is CREATE ROLE. By default, a new role cannot log in (it has the NOLOGIN attribute). You add LOGIN to make it usable as a user.
CREATE ROLE app_owner NOLOGIN;
CREATE ROLE app_readonly NOLOGIN;
CREATE ROLE app_writer NOLOGIN;
CREATE ROLE alice LOGIN PASSWORD 'change-me';
GRANT app_readonly TO alice;The first three roles are group roles. They exist to hold privileges and cannot be used to connect. alice is a login role. The last statement makes alice a member of app_readonly.
PostgreSQL also accepts two older spellings:
| Statement | Equivalent to |
|---|---|
| `CREATE USER alice;` | `CREATE ROLE alice LOGIN;` |
| `CREATE GROUP staff;` | `CREATE ROLE staff;` |
The only difference between CREATE USER and CREATE ROLE is the default for LOGIN. CREATE GROUP is kept for compatibility, so prefer CREATE ROLE in new scripts. Always write LOGIN or NOLOGIN explicitly so a script says what it means.
Roles belong to the cluster, not the database
Roles are defined once for the whole cluster. If you create alice while connected to database appdb, she also exists in every other database on that server. The role list is stored in a shared system catalog.
This has two consequences:
- You cannot have two different roles named
alicein two databases of the same cluster. - Existing as a role does not give access. Whether
alicecan connect to a given database, and what she can do there, is controlled by privileges, which later chapters cover.
When a cluster is created with initdb, one role already exists: the bootstrap superuser. It is named after the operating system user that ran initdb, which is usually postgres. PostgreSQL also ships roles whose names start with pg_. Those are predefined roles, and a later chapter covers them. The pg_ prefix is reserved, so you cannot create your own roles with it.
How membership works
Membership is created with GRANT role TO member and removed with REVOKE role FROM member. Membership can be nested: if alice is in app_readonly, and app_readonly is in reporting_team, then alice can be affected by privileges granted to both.
The diagram below shows the layout from the example.
By default, a member automatically inherits the privileges of the roles it belongs to. The details of inheritance, including the differences between PostgreSQL 16 and earlier versions, are in the chapter on group roles.
Looking at the roles you have
In psql, the \du command lists roles and their attributes. The same information is available through SQL in the pg_roles view:
SELECT rolname, rolcanlogin, rolsuper
FROM pg_roles
WHERE rolname NOT LIKE 'pg\_%'
ORDER BY rolname;The filter hides the predefined pg_ roles so you see only the ones that matter to your setup. The \_ escapes the underscore, which is otherwise a single-character wildcard in LIKE.
To see who belongs to a group role, join the membership catalog to pg_roles twice:
SELECT g.rolname AS group_role, m.rolname AS member
FROM pg_auth_members am
JOIN pg_roles g ON g.oid = am.roleid
JOIN pg_roles m ON m.oid = am.member
WHERE g.rolname = 'app_readonly';After the earlier example, this returns one row: app_readonly with member alice.
pg_roles is a readable view over the real catalog, pg_authid. Use the view. The underlying catalog contains password hashes and only superusers can read it.
Which role am I?
Two session values matter when you work with roles:
session_useris the role you authenticated as when you connected.current_useris the role whose privileges are checked right now. It is normally the same assession_user.
SET ROLE changes current_user without reconnecting, provided your session user is allowed to switch to that role. RESET ROLE puts it back.
SELECT session_user, current_user;
SET ROLE app_owner;
SELECT session_user, current_user;
RESET ROLE;Run as a superuser, the second query shows your login name for session_user and app_owner for current_user. Later chapters use SET ROLE app_owner to create objects as the owner role, so the owner is the intended one and not whoever happened to be connected.
Naming and quoting
Role names follow the same rules as other SQL identifiers. Unquoted names are folded to lower case, so CREATE ROLE Alice creates a role called alice. A quoted name keeps its case exactly:
CREATE ROLE "Bob" LOGIN;After this, you must write "Bob" with quotes every time. An unquoted Bob refers to bob, which does not exist. Pick lower-case names with underscores and you avoid the problem.
Some roles are named after people (alice) and some after function (app_readonly). A useful habit is to name group roles after what they allow, and to keep personal login roles as members of those groups. That way, when a person leaves or changes teams, you change their membership and leave the privileges alone.
Common errors
| Message | Likely cause |
|---|---|
| `role "alice" already exists` | Roles are cluster-wide, so another database's setup script already created it. |
| `role "alice" does not exist` | A typo, a case-folding surprise with quoted names, or the role was created on a different cluster. |
| `permission denied to create role` | Your role has neither `SUPERUSER` nor `CREATEROLE`. |
| `role "app_readonly" is not permitted to log in` | You tried to connect as a `NOLOGIN` role. Connect as a member and use `SET ROLE` if needed. |
| `role "x" cannot be dropped because some objects depend on it` | The role owns objects or holds privileges. See the chapter on dropping roles. |
Summary of the model
- A role is a cluster-wide identity. It can be a user, a group, or both.
LOGINdecides whether you can connect as the role.CREATE ROLEdefaults toNOLOGIN, andCREATE USERdefaults toLOGIN.GRANT group_role TO member_rolecreates membership.session_useris who connected, andcurrent_useris whose privileges apply now.- Roles do not hold any access until privileges are granted to them or to a group they belong to.
Exercise
You run CREATE ROLE reports; and then try to connect to the server with psql -U reports. The connection is refused with a message that the role is not permitted to log in. What is the cause, and which single statement fixes it?
Show answer
CREATE ROLE creates a role with NOLOGIN by default, so reports cannot connect. ALTER ROLE reports LOGIN; fixes it. You would normally also set a password, for example ALTER ROLE reports LOGIN PASSWORD 'change-me';, if the server uses password authentication.
Exercise
You are connected as alice, and a superuser has granted her membership in app_owner. You run SET ROLE app_owner; and then SELECT session_user, current_user;. What does the query return, and how do you return to your normal privileges?
Show answer
It returns alice for session_user and app_owner for current_user, because SET ROLE changes only the role whose privileges are checked. Run RESET ROLE; to return current_user to alice. (This works only if the membership allows SET ROLE. On PostgreSQL 16 and later that depends on the membership's SET option, which defaults to true.)