What is a role in PostgreSQL?

BeginnerConceptPostgreSQL
Answer

Short answer

In PostgreSQL, users and groups are both "roles". A role with the LOGIN attribute can connect (a user); a role without it acts like a group that others can be members of. Roles own objects and receive privileges with GRANT.

Examples

CREATE ROLE shop_app WITH LOGIN PASSWORD 'Str0ng-Pass!';    -- a user
CREATE ROLE readonly;                                        -- a group (no LOGIN)
GRANT CONNECT ON DATABASE shop TO readonly;
GRANT USAGE ON SCHEMA public TO readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;
GRANT readonly TO analyst1, analyst2;                        -- membership
\du

Useful attributes

Attribute Meaning
LOGIN Can connect
SUPERUSER Bypasses all checks — only for admins
CREATEDB Can create databases
CREATEROLE Can create other roles
CONNECTION LIMIT n Max concurrent connections for this role
VALID UNTIL '2027-01-01' Password expiry

Best practice

Group privileges into roles (readonly, readwrite, migrator) and grant those roles to users — easier to manage and audit.

What to say in the interview

"Postgres has only roles; LOGIN makes a role a user, and roles without LOGIN act as groups. I create group roles like readonly and readwrite with the privileges and grant them to login roles, never giving apps SUPERUSER."

All Databases (MySQL, PostgreSQL, MongoDB) questions Roadmap: PostgreSQL basics

More on PostgreSQL