Sub topic 2
Role attributes: LOGIN, SUPERUSER, CREATEDB, CREATEROLE, and more
This chapter assumes you can connect to PostgreSQL and run basic SQL. You should also know that a PostgreSQL role can represent a person, an application, or a group.
A role attribute is a setting that controls what a role can do at the server level. Attributes are different from object privileges. For example, GRANT SELECT ON shop.orders gives access to one table. CREATEDB allows a role to create databases.
Creating roles
Use CREATE ROLE to create a role. A role cannot connect unless it has the LOGIN attribute. Roles without LOGIN are useful as group roles because other roles can become members of them.
CREATE ROLE app_owner NOLOGIN;
CREATE ROLE app_readonly NOLOGIN;
CREATE ROLE alice
LOGIN
PASSWORD 'change-me'
CONNECTION LIMIT 3
VALID UNTIL '2027-12-31';PASSWORD sets a password for password-based authentication. The actual authentication method still depends on pg_hba.conf. Use a secret manager or another protected method for production passwords.
CONNECTION LIMIT limits the number of simultaneous connections for that role. The value -1 means no per-role limit. The limit does not apply to superusers.
VALID UNTIL sets the password expiration time. It does not necessarily disable every possible authentication method after that time.
Important attributes
| Attribute | Meaning | Typical use |
|---|---|---|
| `LOGIN` | Allows the role to start a session | Human or application login |
| `SUPERUSER` | Bypasses most permission checks | Carefully protected administration |
| `CREATEDB` | Allows database creation | Database administration |
| `CREATEROLE` | Allows creating and changing many roles | Delegated role administration |
| `REPLICATION` | Allows replication connections and some replication commands | Replication service |
| `BYPASSRLS` | Bypasses row-level security policies | Trusted maintenance role |
| `INHERIT` | Uses privileges from member roles automatically | Common default for login roles |
A newly created role normally has NOSUPERUSER, NOCREATEDB, NOCREATEROLE, NOREPLICATION, NOBYPASSRLS, and INHERIT. Explicitly writing these choices can make security reviews easier.
Changing attributes
Use ALTER ROLE to change role attributes.
ALTER ROLE alice
NOSUPERUSER
NOCREATEDB
NOCREATEROLE
NOREPLICATION
NOBYPASSRLS
CONNECTION LIMIT 5;
ALTER ROLE app_owner
NOLOGIN;
ALTER ROLE alice VALID UNTIL 'infinity';Only a superuser can grant or remove SUPERUSER. A role with CREATEROLE can manage roles within limits, but it cannot grant itself superuser status. Keep CREATEROLE separate from ordinary application accounts.
REPLICATION is not a general data-reading privilege. It enables replication-related connections, while normal table access still follows PostgreSQL permissions. BYPASSRLS is especially sensitive because it defeats row-level security, which may otherwise restrict visible rows.
Ownership and attributes together
Every database object has an owner. The owner can normally alter or drop that object and can grant its privileges. A role does not need SUPERUSER to own tables, schemas, or functions.
A common design uses a NOLOGIN owner role and separate login roles:
This prevents a person or application from owning objects directly. It also lets you change membership without changing object ownership.
Exercise
Which attributes should a normal application login usually have, and which should it avoid?
Show answer
It usually needs LOGIN and may have a connection limit and password expiration. It should normally avoid SUPERUSER, CREATEDB, CREATEROLE, REPLICATION, and BYPASSRLS.