Sub topic 1
Roles: one concept for users and groups
Prerequisites
Assume you know basic SQL statements such as CREATE TABLE, SELECT, INSERT, UPDATE, and DELETE. You should also know how to connect to PostgreSQL with psql or another client, and understand that a PostgreSQL server contains databases, schemas, and tables.
One role, two common uses
A PostgreSQL role is a named identity that can own database objects and receive privileges. PostgreSQL uses roles for both human users and groups. A role is not automatically a person or a group. Its attributes determine whether it can log in and what administrative actions it may perform.
A login role represents an identity that can authenticate:
CREATE ROLE alice LOGIN PASSWORD 'change-me';A non-login role usually represents a group of permissions:
CREATE ROLE app_readonly NOLOGIN;
CREATE ROLE app_writer NOLOGIN;The NOLOGIN attribute prevents direct connections with that role. It does not prevent the role from receiving privileges or from being granted to another role.
PostgreSQL roles exist at the cluster level. A role can connect to databases in the same PostgreSQL cluster if it has the required login and database privileges. Objects such as tables remain inside a particular database.
Adding a login role to a group role
Use GRANT to make one role a member of another role:
GRANT app_readonly TO alice;This statement means that alice is a member of app_readonly. If inheritance is enabled, alice can use privileges granted to app_readonly without switching roles.
A group role does not need a special group type. NOLOGIN is a convention that makes the role unsuitable for direct application or human logins.
The same login role can belong to several group roles. For example, a reporting analyst might belong to a read-only group and a monitoring group. Keep permissions on group roles whenever possible. This makes access easier to review and change.
Role attributes
Role attributes are settings stored on the role itself. Common attributes include:
| Attribute | Effect |
|---|---|
| `LOGIN` | Allows the role to authenticate |
| `NOLOGIN` | Prevents direct authentication |
| `SUPERUSER` | Bypasses most permission checks |
| `CREATEDB` | Allows the role to create databases |
| `CREATEROLE` | Allows the role to create or alter roles, within limits |
Use administrative attributes sparingly. Application roles normally need LOGIN but should not be SUPERUSER, CREATEDB, or CREATEROLE.
You can inspect roles with pg_roles:
SELECT rolname, rolcanlogin, rolsuper, rolcreatedb, rolcreaterole
FROM pg_roles
WHERE rolname NOT LIKE 'pg\_%'
ORDER BY rolname;A role's ability to log in is separate from its object privileges. LOGIN permits authentication, but it does not grant access to a table. Table, schema, and database privileges are assigned separately.
Exercise
Exercise
Why is app_readonly usually created with NOLOGIN instead of LOGIN?
Show answer
It is a group role that stores shared permissions. Individual login roles can become members of it, while nobody connects directly as the group.