Create, alter, drop, and privilege roles (users) to control database access.
PostgreSQL abstracts authentication and authorization into roles. A role can own objects, receive privileges, and optionally log in. Treating users as login-enabled roles lets you grant the same rights to many users or service accounts with less duplication.
CREATE ROLE role_name [LOGIN] [PASSWORD 'secret'] [IN ROLE parent_role] [VALID UNTIL 'YYYY-MM-DD'];
CREATE ROLE analyst LOGIN PASSWORD 'Strong_42' VALID UNTIL '2025-01-01';
GRANT privilege_list ON object_type object_name TO role_name [WITH GRANT OPTION];
GRANT SELECT, INSERT ON TABLE sales.orders TO analyst;
Use ALTER ROLE
to change passwords, connection limits, timeouts, and more. For example: ALTER ROLE analyst CONNECTION LIMIT 10;
or ALTER ROLE analyst RESET ALL;
First transfer ownership and revoke privileges: REASSIGN OWNED BY analyst TO admin; REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM analyst;
Then drop the role: DROP ROLE analyst;
Grant the least privilege needed, avoid creating superusers, rotate passwords periodically, and use role hierarchies (e.g., readonly, writer) instead of ad-hoc grants.
No. Every user is a role with the LOGIN attribute set to true.
Use ALTER ROLE username PASSWORD NULL;
and set the authentication method to md5
or scram-sha-256
. The next login prompts a new password.
Enable log_statement = 'ddl'
or install the pgaudit
extension to capture grant and role-management statements.