Guide
How to create a user in PostgreSQL, grant access and list users
Run CREATE USER alice WITH PASSWORD 'long-random-password'; as a superuser or a role with CREATEROLE. Then give the user access: GRANT CONNECT ON DATABASE appdb TO alice;, GRANT USAGE ON SCHEMA public TO alice; and the table privileges it needs. List users with \du in psql or SELECT rolname FROM pg_roles; anywhere.
CREATE USER vs CREATE ROLE
PostgreSQL has one kind of account, the role. A role that can log in is what other databases call a user; a role that can't is a group you grant privileges to and then add users to. CREATE USER is exactly CREATE ROLE with LOGIN switched on:
CREATE USER alice WITH PASSWORD 'long-random-password';
-- is the same as
CREATE ROLE alice WITH LOGIN PASSWORD 'long-random-password';
-- a group role: no LOGIN
CREATE ROLE app_readwrite;Forget LOGIN on CREATE ROLE and the user gets FATAL: role "alice" is not permitted to log in; fix it with ALTER ROLE alice LOGIN;. You need superuser or CREATEROLE to create roles, and only a superuser can create another superuser.
Options: password, expiry, limits
CREATE ROLE alice WITH
LOGIN
PASSWORD 'long-random-password'
VALID UNTIL '2027-06-30'
CONNECTION LIMIT 10;
CREATE ROLE deployer LOGIN PASSWORD '…' CREATEDB; -- may create databases
CREATE ROLE bob LOGIN PASSWORD '…' IN ROLE app_readwrite; -- member of a group from the start- PASSWORD is stored hashed. Since PostgreSQL 14 the default
password_encryptionisscram-sha-256. MD5 is deprecated: PostgreSQL 18 warns when you set an MD5 password, and a future release will remove it.PASSWORD NULL, or no password at all, means password login always fails. - VALID UNTIL expires the password, not the role. After that time the user gets the same
password authentication failederror as for a wrong password, and login methods that don't use the password, such as certificates, ignore it. - CONNECTION LIMIT caps concurrent sessions;
-1, the default, means no limit. It is approximate and never applies to superusers. - CREATEDB, CREATEROLE, SUPERUSER, REPLICATION and BYPASSRLS are powers to give sparingly.
A password typed into CREATE ROLE travels to the server in clear text and can land in your ~/.psql_history and the server log. To avoid that, create the role without one and set it with psql's \password, which hashes it on your machine first:
CREATE ROLE alice LOGIN;
\password aliceFrom a shell, createuser does the same job and also sends the password already hashed:
createuser -h db.example.com -U postgres --pwprompt --connection-limit=10 alice
createuser --echo --no-login app_readwrite # a group role; --echo shows the SQLThere is no CREATE ROLE IF NOT EXISTS. In a migration, wrap it:
DO $$
BEGIN
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'alice') THEN
CREATE ROLE alice LOGIN;
END IF;
END
$$;Grant the user access
A new role can log in but can't see your tables. Access is granted at three levels: the database, the schema and the objects inside it. A common pattern is to grant to a group role and add users to the group, so the next person needs one line:
-- the group
CREATE ROLE app_readwrite;
GRANT CONNECT ON DATABASE appdb TO app_readwrite;
\c appdb
GRANT USAGE ON SCHEMA public TO app_readwrite;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_readwrite;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO app_readwrite;
-- tables that app_owner creates later
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_readwrite;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA public
GRANT USAGE, SELECT ON SEQUENCES TO app_readwrite;
-- the users
GRANT app_readwrite TO alice, bob;USAGEon sequences lets inserts useserialand identity columns.- Run the schema and table grants while connected to that database: grants are per database.
ON ALL TABLESonly covers tables that exist now;ALTER DEFAULT PRIVILEGES FOR ROLEcovers future ones, and must name the role that will create them.- To let a user create tables,
GRANT CREATE ON SCHEMA public TO alice;. Since PostgreSQL 15 ordinary users can't create objects inpublicby default; only the database owner can.
For a user that may only read, follow the read-only user recipe; it covers pg_read_all_data and the default-privileges gotcha in detail. For a user that owns a whole database, create the database with OWNER (see creating a database).
Change a user: ALTER ROLE
ALTER ROLE alice WITH PASSWORD 'new-long-random-password'; -- or \password alice
ALTER ROLE alice VALID UNTIL 'infinity'; -- remove the expiry
ALTER ROLE alice NOLOGIN; -- disable without dropping
ALTER ROLE alice CONNECTION LIMIT 5;
ALTER ROLE alice CREATEDB;
ALTER ROLE alice RENAME TO alice_old;
-- per-user settings, applied at each new login
ALTER ROLE alice SET search_path = shop, public;
ALTER ROLE alice SET statement_timeout = '30s';
ALTER ROLE alice IN DATABASE appdb SET work_mem = '64MB';
REVOKE app_readwrite FROM alice; -- leave a groupRenaming a role clears its password if the password is stored as MD5, because MD5 uses the role name as salt; SCRAM passwords survive a rename.
List users in PostgreSQL
In psql, \du (or \dg, the same command) lists roles and their attributes; \du+ adds comments, and \du a* filters by name:
appdb=# \du
List of roles
Role name | Attributes
---------------+------------------------------------------------------------
alice | 10 connections +
| Password valid until 2027-06-30 00:00:00+00
app_owner | Create DB
app_readwrite | Cannot login
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS
appdb=# \drg
List of role grants
Role name | Member of | Options | Grantor
-----------+---------------+--------------+----------
alice | app_readwrite | INHERIT, SET | postgresSince PostgreSQL 16, group memberships are no longer a "Member of" column of \du but their own command, \drg, shown second above. Outside psql, query the catalog:
-- every role, without the built-in pg_* ones
SELECT rolname, rolcanlogin, rolsuper, rolcreatedb, rolcreaterole,
rolconnlimit, rolvaliduntil
FROM pg_roles
WHERE rolname !~ '^pg_'
ORDER BY rolname;
-- only roles that can log in (password column masked)
SELECT usename, usesuper, usecreatedb, valuntil FROM pg_user ORDER BY usename;
-- who is in which group
SELECT g.rolname AS group_role, m.rolname AS member
FROM pg_auth_members am
JOIN pg_roles g ON g.oid = am.roleid
JOIN pg_roles m ON m.oid = am.member
WHERE g.rolname !~ '^pg_'
ORDER BY 1, 2;
-- who you are
SELECT current_user, session_user;pg_roles is readable by everyone. pg_authid, which holds the password hashes, is for superusers only.
Drop a user safely
DROP ROLE refuses while anything still refers to the role:
ERROR: role "alice" cannot be dropped because some objects depend on it
DETAIL: privileges for database appdb
privileges for schema public
owner of table public.alice_notesHand its objects to another role, remove its grants, then drop it. REASSIGN OWNED and DROP OWNED work on the database you are connected to, so repeat them in each database where the role owns objects or has privileges:
\c appdb
REASSIGN OWNED BY alice TO app_owner; -- tables etc. alice owns now belong to app_owner
DROP OWNED BY alice; -- removes alice's remaining privileges (and anything still owned)
-- repeat \c / REASSIGN / DROP OWNED for every other database, then:
DROP ROLE alice;Order matters: without REASSIGN OWNED first, DROP OWNED deletes the tables the role owns. DROP ROLE also doesn't end sessions that are already open; to lock someone out now, ALTER ROLE alice NOLOGIN; and then SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE usename = 'alice';.
Amazon RDS, Aurora and Supabase
RDS and Aurora PostgreSQL. The master user is not a superuser; it belongs to rds_superuser and has CREATEROLE and CREATEDB, so everything above works except creating superusers. AWS recommends creating separate users for applications rather than using the master user. To let a user sign in with IAM instead of a password, GRANT rds_iam TO alice;; from then on that user must log in with an IAM token, and its password no longer works (see connecting to RDS PostgreSQL).
Supabase. Run the same SQL in the SQL editor or psql. When the new user connects through Supabase's shared pooler, its user name gets the project reference appended, alice.abcdefghijklmnop; direct connections use plain alice. Tables in public often have row-level security on, so a new role sees no rows until a policy allows it.
Questions
What is the difference between CREATE USER and CREATE ROLE in PostgreSQL?
CREATE USER is CREATE ROLE with LOGIN implied. CREATE ROLE without LOGIN makes a group role that can hold privileges but can't sign in.
How do I list all users in PostgreSQL?
Type \du in psql, or run SELECT rolname FROM pg_roles WHERE rolcanlogin; in any client. pg_user lists only roles that can log in.
How do I change a user's password in PostgreSQL?
Use \password username in psql, which hashes the password before sending it, or ALTER ROLE username WITH PASSWORD 'new-password';
Why does my new user get permission denied for table?
Creating a user grants nothing on your tables. Grant CONNECT on the database, USAGE on the schema and SELECT (or more) on the tables, and use ALTER DEFAULT PRIVILEGES for tables created later.
How do I make a PostgreSQL user a superuser?
As a superuser, run ALTER ROLE username SUPERUSER; On managed services such as RDS or Supabase there is no true superuser; the closest is membership in the provider's admin role, such as rds_superuser.
How do I delete a user that owns objects?
In each database, run REASSIGN OWNED BY user TO new_owner; and DROP OWNED BY user;, then DROP ROLE user;
Updated 2026-11-25