How to Manage Users in PostgreSQL

Galaxy Glossary

How do I manage users and privileges in PostgreSQL?

Create, alter, drop, and privilege roles (users) to control database access.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Why does PostgreSQL use roles instead of standalone users?

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.

How do I create a login role?

Syntax

CREATE ROLE role_name [LOGIN] [PASSWORD 'secret'] [IN ROLE parent_role] [VALID UNTIL 'YYYY-MM-DD'];

Example

CREATE ROLE analyst LOGIN PASSWORD 'Strong_42' VALID UNTIL '2025-01-01';

How do I grant privileges to a user?

Syntax

GRANT privilege_list ON object_type object_name TO role_name [WITH GRANT OPTION];

Example

GRANT SELECT, INSERT ON TABLE sales.orders TO analyst;

How can I alter or reset user settings?

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;

How do I remove a user safely?

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;

What are best practices for secure user management?

Grant the least privilege needed, avoid creating superusers, rotate passwords periodically, and use role hierarchies (e.g., readonly, writer) instead of ad-hoc grants.

Why How to Manage Users in PostgreSQL is important

How to Manage Users in PostgreSQL Example Usage


-- Full workflow
CREATE ROLE analyst LOGIN PASSWORD 'Strong_42';
GRANT CONNECT ON DATABASE reporting TO analyst;
GRANT USAGE ON SCHEMA public TO analyst;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO analyst;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO analyst;

How to Manage Users in PostgreSQL Syntax


-- Create a user (role with LOGIN)
CREATE ROLE role_name LOGIN PASSWORD 'secret' [VALID UNTIL 'YYYY-MM-DD'];

-- Grant object privileges
GRANT { SELECT | INSERT | UPDATE | DELETE | ALL } ON { TABLE | DATABASE | SCHEMA | SEQUENCE } object TO role_name;

-- Alter role attributes
ALTER ROLE role_name [SET param = value | RESET param | PASSWORD 'newpass' | VALID UNTIL 'date'];

-- Drop role
REASSIGN OWNED BY role_name TO new_owner;
DROP ROLE role_name;

Common Mistakes

Frequently Asked Questions (FAQs)

Does PostgreSQL differentiate between users and roles?

No. Every user is a role with the LOGIN attribute set to true.

How can I force a user to change their password?

Use ALTER ROLE username PASSWORD NULL; and set the authentication method to md5 or scram-sha-256. The next login prompts a new password.

Can I audit who granted what?

Enable log_statement = 'ddl' or install the pgaudit extension to capture grant and role-management statements.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.