Sub topic 2
Role attributes: LOGIN, SUPERUSER, CREATEDB, CREATEROLE, and more
This chapter assumes you can create roles with CREATE ROLE, that you know a role can stand for a user or a group, and that you can connect with psql. Here you will learn the attributes that decide what a role can do at the server level, apart from what it can do to tables and schemas.
Attributes versus privileges
A role has two kinds of capabilities:
- Privileges such as
SELECTorUSAGEapply to a specific object. You manage them withGRANTandREVOKE. Later chapters cover them. - Attributes apply to the role itself. They control logging in, creating databases, creating other roles, bypassing checks, and similar server-wide abilities. You set them with
CREATE ROLEandALTER ROLE.
PostgreSQL stores attributes as columns in the pg_authid catalog. The view pg_roles exposes the same data without the password hash.
Defaults
A plain CREATE ROLE name; is deliberately restrictive.
| Attribute | Default | Meaning |
|---|---|---|
| `LOGIN` | `NOLOGIN` | Whether the role can start a session |
| `SUPERUSER` | `NOSUPERUSER` | Skips almost all permission checks |
| `CREATEDB` | `NOCREATEDB` | May create databases |
| `CREATEROLE` | `NOCREATEROLE` | May create and manage other roles |
| `REPLICATION` | `NOREPLICATION` | May start streaming replication connections |
| `BYPASSRLS` | `NOBYPASSRLS` | Ignores row-level security policies |
| `INHERIT` | `INHERIT` | Whether the role automatically uses privileges of roles it belongs to |
| `CONNECTION LIMIT` | `-1` | Maximum concurrent sessions (`-1` means unlimited) |
CREATE USER is the same command with LOGIN as the default. CREATE GROUP is an old alias for CREATE ROLE. Using CREATE ROLE with explicit attributes makes your intent clear.
LOGIN
LOGIN is the attribute that separates a "user" from a "group". Only a role with LOGIN can be named in a connection request.
CREATE ROLE app_readonly NOLOGIN;
CREATE ROLE alice LOGIN PASSWORD 'change-me';Having LOGIN is necessary but not sufficient. The server's pg_hba.conf file must also have a rule that allows the connection, and the authentication method in that rule must succeed. The attribute and the host-based rule are checked independently.
You can change it later:
ALTER ROLE alice NOLOGIN; -- existing sessions continue; new ones are refused
ALTER ROLE alice LOGIN;NOLOGIN does not end sessions that are already open. To end them, use pg_terminate_backend() on the session's process ID.
Passwords, expiry, and connection limits
The password options go on the role too.
CREATE ROLE deployer LOGIN
PASSWORD 'change-me'
VALID UNTIL '2027-01-01'
CONNECTION LIMIT 5;PASSWORDstores a hash, not the plain text. Which hash depends on thepassword_encryptionsetting, which isscram-sha-256in current versions.VALID UNTILsets a time after which password authentication fails. It does not affect other methods such as certificates or peer authentication.CONNECTION LIMITcaps concurrent sessions for that role. It does not apply to superusers.
SUPERUSER
A superuser skips every permission check except the right to log in. It can read any table, drop any object, and change server-wide files through features like COPY ... PROGRAM. That last ability means a superuser can run commands as the operating system user that runs PostgreSQL.
CREATE ROLE dba LOGIN SUPERUSER PASSWORD 'change-me';Only a superuser can create another superuser. Treat the attribute as a break-glass tool:
- Keep the number of superusers small, ideally the bootstrap
postgresrole plus one or two named administrators. - Never connect an application as a superuser. A SQL injection bug then becomes full control of the server.
- Prefer a narrower tool when one exists, such as the predefined roles covered in a later chapter.
CREATEDB, REPLICATION, and BYPASSRLS
These three are narrow and easy to reason about.
CREATE ROLE ci_runner LOGIN CREATEDB PASSWORD 'change-me';
CREATE ROLE replicator LOGIN REPLICATION PASSWORD 'change-me';
CREATE ROLE etl LOGIN BYPASSRLS PASSWORD 'change-me';CREATEDBlets a role runCREATE DATABASE. The role becomes the owner of databases it creates. A continuous-integration runner that creates throwaway databases is a typical use.REPLICATIONlets a role open a replication connection, used by standbys and backup tools such aspg_basebackup. The role also needs a matchingreplicationline inpg_hba.conf.BYPASSRLSexempts the role from row-level security policies. Grant it rarely, because it removes a safety net.
CREATEROLE
CREATEROLE lets a role run CREATE ROLE, ALTER ROLE, DROP ROLE, and GRANT or REVOKE membership. Its meaning changed in PostgreSQL 16, so check your server version.
- PostgreSQL 15 and earlier. A
CREATEROLErole could change or grant almost any non-superuser role. That includes handing outpg_read_all_data-style privileges or taking over other roles' membership. In practice it was close to superuser, so treat it that way. - PostgreSQL 16 and later. The creator of a role automatically gets
ADMIN OPTIONon it. ACREATEROLErole can manage only roles it hasADMIN OPTIONon. It can never alter a superuser. It can give a new role only attributes it holds itself, so it cannot hand outCREATEDBunless it hasCREATEDB.
-- PostgreSQL 16 and later
CREATE ROLE team_admin LOGIN CREATEROLE PASSWORD 'change-me';
-- When team_admin later runs CREATE ROLE bob LOGIN;
-- it receives ADMIN OPTION on bob and can manage only roles like bob.The 16 model fits delegation well. A team lead can manage the team's roles without touching anyone else's.
INHERIT
INHERIT controls whether a member role automatically uses the privileges of the roles it belongs to. The next chapters cover membership in detail, but the interaction with attributes matters here.
Attributes are not inherited
Privileges on objects flow through membership. The attributes LOGIN, SUPERUSER, CREATEDB, CREATEROLE, REPLICATION, and BYPASSRLS do not. A member must switch to the role with SET ROLE to use them. The example below shows this.
CREATE ROLE db_admin NOLOGIN CREATEDB;
CREATE ROLE bob LOGIN PASSWORD 'change-me';
GRANT db_admin TO bob;Now connect as bob:
CREATE DATABASE scratch;
-- ERROR: permission denied to create database
SET ROLE db_admin;
CREATE DATABASE scratch;
-- CREATE DATABASE
RESET ROLE;This is a safety feature. Membership in a group cannot silently make someone a superuser.
Changing and inspecting attributes
ALTER ROLE changes any attribute. Prefix NO to turn one off.
ALTER ROLE ci_runner NOCREATEDB;
ALTER ROLE deployer CONNECTION LIMIT 10;
ALTER ROLE alice VALID UNTIL 'infinity';ALTER ROLE ... SET stores a per-role default for a configuration parameter, which applies at the next login:
ALTER ROLE alice SET statement_timeout = '30s';To list attributes in psql, run \du. To query them, use pg_roles:
SELECT rolname, rolcanlogin, rolsuper, rolcreatedb, rolcreaterole,
rolreplication, rolbypassrls, rolconnlimit, rolvaliduntil
FROM pg_roles
WHERE rolname NOT LIKE 'pg\_%'
ORDER BY rolname;The NOT LIKE filter hides the predefined roles, whose names start with pg_. You cannot create roles with that prefix yourself.
Common errors
| Message | Likely cause |
|---|---|
| `role "x" is not permitted to log in` | The role has `NOLOGIN` |
| `permission denied to create role` | The current role lacks `CREATEROLE` (or is not a superuser) |
| `permission denied to create database` | `CREATEDB` is held only by a group the role belongs to. Use `SET ROLE` |
| `too many connections for role "x"` | The role reached its `CONNECTION LIMIT` |
| `password authentication failed` | Wrong password, a `VALID UNTIL` date in the past, or a role with no password under a password rule |
Design guidance
- Create group roles as
NOLOGINand give them no attributes. - Create login roles with only the attributes they need.
- Reserve
SUPERUSERfor named human administrators. - Where you can, set
VALID UNTILon credentials for temporary access.
Exercise
You run GRANT db_admin TO bob; where db_admin has CREATEDB. Bob runs CREATE DATABASE scratch; and gets "permission denied to create database". Why, and what are two ways to fix it?
Show answer
Role attributes such as CREATEDB are never inherited through membership. Fix 1: Bob runs SET ROLE db_admin; before creating the database. Fix 2: run ALTER ROLE bob CREATEDB; so Bob holds the attribute directly.
Exercise
A reporting application connects as a role that was created with CREATE ROLE report LOGIN SUPERUSER. Name the risk and write the command that removes it.
Show answer
A flaw such as SQL injection in the application would give an attacker full control of the server, including OS-level access through features like COPY ... PROGRAM. Run ALTER ROLE report NOSUPERUSER; and then grant it only the privileges it needs.