How-to

Create a PostgreSQL User and Give It a Home

A PostgreSQL user is a role that may log in. Creating one is quick; giving it the right database, schema and authentication rule is what makes it work.

Which PostgreSQL version will I have?

From the distribution's own repositories: PostgreSQL 17 on Debian 13, 16 on Ubuntu 24.04, 15 on Debian 12 and a choice of module streams on Rocky Linux and AlmaLinux 9. The official PostgreSQL repository offers newer releases for all of them. Everything below works from version 14 on, and the notes call out where version 15 changed defaults.

How do I create a user from the shell?

sudo -u postgres createuser --pwprompt --no-createdb --no-createrole ledger

createuser is a wrapper around SQL that runs as the postgres superuser account. --pwprompt asks for a password; the flags make the defaults explicit, so the role cannot create databases or other roles.

What is the SQL form?

sudo -u postgres psql
CREATE ROLE ledger LOGIN PASSWORD 'r8T-long-generated-value' CONNECTION LIMIT 40;
CREATE DATABASE ledger OWNER ledger;

CREATE USER is the same as CREATE ROLE ... LOGIN. Since PostgreSQL 14, passwords are stored as scram-sha-256 hashes by default, which is the right choice; check with SHOW password_encryption;. To avoid the password landing in your shell history or the server log, use \password ledger inside psql instead of typing it into the statement.

Why can the new user not create tables?

Because PostgreSQL 15 removed the old default that let every user create objects in the public schema. On a database the role owns, it may still create tables there, since a database owner controls its schemas. In a shared database, give each application its own schema:

\c ledger
CREATE SCHEMA app AUTHORIZATION ledger;
ALTER ROLE ledger IN DATABASE ledger SET search_path = app, public;

Now tables created by ledger land in app without qualifying names.

How do I add a read-only user?

Grant usage on the schema, select on existing tables, and default privileges so future tables are covered too:

CREATE ROLE ledger_ro LOGIN PASSWORD 'another-generated-value';
GRANT CONNECT ON DATABASE ledger TO ledger_ro;
GRANT USAGE ON SCHEMA app TO ledger_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA app TO ledger_ro;
ALTER DEFAULT PRIVILEGES FOR ROLE ledger IN SCHEMA app GRANT SELECT ON TABLES TO ledger_ro;

PostgreSQL 14 and later also include the built-in role pg_read_all_data, which reads everything in every database; it suits backup or audit accounts, not applications.

Where is the login actually allowed?

In pg_hba.conf, the host-based authentication file, which PostgreSQL checks from top to bottom. Find it with SHOW hba_file;.

LineAllows
local all postgres peerThe postgres system account over the socket, no password
local ledger ledger scram-sha-256ledger over the socket with its password
host ledger ledger 127.0.0.1/32 scram-sha-256ledger over TCP from this machine
hostssl ledger ledger_ro 198.51.100.12/32 scram-sha-256ledger_ro from one remote machine, TLS only

After editing, reload with SELECT pg_reload_conf(); or systemctl reload postgresql. Errors such as "no pg_hba.conf entry for host" mean no line matched; "password authentication failed" means a line matched and the password was wrong.

How do remote clients connect?

Set listen_addresses = 'localhost,203.0.113.25' in postgresql.conf and restart, add a hostssl line for the client's address, and open port 5432 in the firewall only for that address. Since machines talk over the public internet, with no private network on offer, insist on TLS: Debian and Ubuntu packages enable it with a self-signed certificate by default; replace it with a proper certificate if clients verify the server. A tunnel through SSH, ssh -L 5433:127.0.0.1:5432, is a good alternative for occasional admin access.

How do I check the new user works?

psql "host=127.0.0.1 dbname=ledger user=ledger" -c 'select current_user, current_schema()'

It should print ledger and app. Removing roles later is covered in Delete a PostgreSQL user; sizing a PostgreSQL machine in Database servers.

Keep Reading

Delete a PostgreSQL user

Remove a PostgreSQL role cleanly: end its sessions, hand its objects over, drop leftover privileges in every database, then drop the role.

Open

Database servers

PostgreSQL, MongoDB, ClickHouse and other engines on machines picked by working set, write rate and core count.

Open

MySQL users and privileges

Create MySQL 8 accounts with caching_sha2 passwords, least-privilege grants, roles, lockout rules and dynamic privileges for admin tasks.

Open

Debian

Debian 13 or 12 for servers that should change as little as possible between upgrades, on any machine we sell.

Open

FAQ

Quick Answers

Run sudo -u postgres createuser --pwprompt name, or CREATE ROLE name LOGIN PASSWORD in psql.

CREATE USER is CREATE ROLE with LOGIN included by default.

PostgreSQL 15 removed the default CREATE right on public. Make the user the database owner or give it its own schema.

scram-sha-256, the default since PostgreSQL 14.

No line in pg_hba.conf matches that user, database and address. Add one and reload the configuration.