The Short Answer
Open psql as the administrative role, then create the role, set its password, and create its database:
sudo -u postgres psql
CREATE ROLE app_user LOGIN;
\password app_user
CREATE DATABASE app OWNER app_user;
\password asks for the new password twice, hashes it in the client, and sends only the hash to the server, so the password appears in clear text neither in your history files nor in the server log. The role owns the app database and can create tables in it. If PostgreSQL is not installed yet, see how to install PostgreSQL.
Roles, Users, and Groups
PostgreSQL has only roles. A role with the LOGIN attribute acts as a user; a role without it acts as a group that other roles join. CREATE USER is the same statement as CREATE ROLE with LOGIN added:
CREATE USER app_user; -- can log in
CREATE ROLE app_user LOGIN; -- the same
CREATE ROLE readers; -- a group, cannot log in
Role names are plain identifiers. Write them in lowercase without quotes, because PostgreSQL folds unquoted names to lowercase.
From the Shell with createuser
sudo -u postgres createuser --pwprompt app_user
sudo -u postgres createdb --owner=app_user app
--pwprompt asks for the password interactively. Avoid CREATE ROLE … PASSWORD '…': typed with -c it stays in the shell history, and typed inside psql it lands in psql's history file and can appear in the server log.
Grant Privileges
An owner can do everything in its own database. For a second role that should only read, grant the rights step by step:
CREATE ROLE report_user LOGIN;
\password report_user
GRANT CONNECT ON DATABASE app TO report_user;
\c app
GRANT USAGE ON SCHEMA public TO report_user;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO report_user;
ALTER DEFAULT PRIVILEGES FOR ROLE app_user IN SCHEMA public
GRANT SELECT ON TABLES TO report_user;
The GRANT SELECT line covers tables that exist now. The last statement covers tables that app_user creates later. Since PostgreSQL 15, ordinary roles can no longer create objects in the public schema of a database they do not own, which is one more reason to make the application's role the owner.
Change a Role with ALTER ROLE
| Task | Statement |
|---|---|
| New password | \password app_user |
| Allow new databases | ALTER ROLE app_user CREATEDB; |
| Block logins | ALTER ROLE app_user NOLOGIN; |
| Limit connections | ALTER ROLE app_user CONNECTION LIMIT 20; |
| Rename | ALTER ROLE app_user RENAME TO web; |
Do not give an application role SUPERUSER. A superuser bypasses every permission check and can read files on the server as the postgres system user.
List Roles with \du
\du
The output shows each role with its attributes, such as Superuser, Create DB, or Cannot login. The same data in SQL:
SELECT rolname, rolcanlogin, rolsuper FROM pg_roles ORDER BY rolname;
Names that start with pg_ are built-in roles. For more inspection commands, see the psql commands cheat sheet.
Passwords and Authentication
Since PostgreSQL 14, new passwords are hashed with scram-sha-256 by default; on version 13, the default on AlmaLinux and Rocky Linux 9, set password_encryption = 'scram-sha-256' first. A password only matters for connections that pg_hba.conf sends through password authentication. On Ubuntu and Debian that includes TCP connections to 127.0.0.1; on AlmaLinux and Rocky Linux the initial file uses ident for them, so change those lines to scram-sha-256 and reload. Local socket logins as postgres use peer authentication instead. Opening the server to another machine is a separate step, covered in how to allow remote connections to PostgreSQL.
Remove a Role
A role that still owns objects cannot be dropped. Take a backup of the database first, then hand its objects to another role and remove it:
REASSIGN OWNED BY report_user TO app_user;
DROP OWNED BY report_user;
DROP ROLE report_user;
Run the first two lines in every database where the role owns something or has privileges. After REASSIGN OWNED, DROP OWNED only removes the role's remaining privileges; without the first line, it deletes every object the role owns.
If you also work with MySQL, the matching guide is how to list users in MySQL.