Skip to main content
⚡ OpenDevDocs
PostgreSQL
databasepostgresqlGuideBeginner

Creating Users & Roles in PostgreSQL

Manage PostgreSQL roles, CREATE USER, password hashing, SUPERUSER privileges, and role attributes.

Verified: 2026-09-30
Tested on:postgres 16.x
Platforms:
linuxmacoswindows
#postgresql#roles#users#security#permissions

Roles vs Users in PostgreSQL

In PostgreSQL, a Role can function as both a user account (with login capabilities) or a user group. CREATE USER is simply an alias for CREATE ROLE ... WITH LOGIN.


1. Creating a Standard Application User

-- Create an application role with a secure password
CREATE ROLE app_user WITH LOGIN PASSWORD 'SuperSecretPass123!';

-- Prevent user from creating databases or superuser privileges
ALTER ROLE app_user NOCREATEDB NOCREATEROLE NOSUPERUSER;

2. Common Role Attributes

AttributeMeaning
LOGINAllows the role to establish database connections (a user account).
SUPERUSERBypasses all security and permission checks (grant with extreme caution).
CREATEDBAllows the role to create new databases.
CREATEROLEAllows the role to create or modify other roles.
PASSWORD '<str>'Sets an encrypted password (stored using SCRAM-SHA-256).
VALID UNTIL '<ts>'Sets an expiration timestamp for temporary accounts.

3. Modifying and Deleting Roles

-- Change an existing user's password
ALTER ROLE app_user WITH PASSWORD 'NewUltraSecretPass456!';

-- Rename a role
ALTER ROLE old_user_name RENAME TO new_user_name;

-- Delete a role (must reassign or drop owned objects first)
REASSIGN OWNED BY old_user TO postgres;
DROP OWNED BY old_user;
DROP ROLE old_user;

Best Practice: Least Privilege Principle

Never connect your production web application (Node.js/Next.js/Express) using the default postgres superuser account. Create a dedicated application role (app_backend) and grant permissions only to specific tables and schemas.

On this page