Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 57Data · PostgreSQL

How to create a PostgreSQL user and database

Every app should get its own database and its own login role — never the postgres superuser. Often you also need a read-only user for reports or a BI tool. This how-to creates both with the least rights, sets the password without leaving it in logs or history, allows the network login in pg_hba.conf, and explains the change in PostgreSQL 15+ that surprises many people (no more CREATE on the public schema).

  • 5 Lessons
  • 3 Commands

$ run as a normal user# run as root (or with sudo)

57.1

The quick answer

sudo -u postgres psql
psql
CREATE ROLE shop LOGIN;
\password shop
CREATE DATABASE shop OWNER shop;
\c shop
CREATE SCHEMA app AUTHORIZATION shop;
ALTER ROLE shop SET search_path = app;
\q

\password asks for the password twice and sends only its hash — it does not appear in the server log or in your history.

57.2

Step by step

  1. Connect as the admin with peer login (no password, the Linux user postgres):
sudo -u postgres psql
  1. Create the app role and its database (quick answer). The owner of the database and of the schema can create tables there; other roles can not.
  2. PostgreSQL 15+: ordinary users can no longer create tables in the public schema. Two clean ways:
  • give the app its own schema (as above), or
  • make the app the owner of the database — then it owns public too (since 15 the database owner owns public).
  1. A read-only role for reports — rights on the existing tables and on tables made later:
psql
CREATE ROLE report LOGIN;
\password report
\c shop
GRANT CONNECT ON DATABASE shop TO report;
GRANT USAGE ON SCHEMA app TO report;
GRANT SELECT ON ALL TABLES IN SCHEMA app TO report;
ALTER DEFAULT PRIVILEGES FOR ROLE shop IN SCHEMA app GRANT SELECT ON TABLES TO report;

ALTER DEFAULT PRIVILEGES … FOR ROLE shop = new tables created by shop are readable for report too. (PostgreSQL 14+ also has the built-in role pg_read_all_data — it reads every database, often too much.)

  1. Allow the network login in pg_hba.conf — only from the app's network, only for its database, with scram-sha-256:
/var/lib/pgsql/data/pg_hba.conf
host    shop    shop      10.0.1.0/24     scram-sha-256
host    shop    report    10.0.5.10/32    scram-sha-256

(On Ubuntu: /etc/postgresql/16/main/pg_hba.conf.) Rules are read top to bottom; the first match wins. Then reload:

sudo -u postgres psql -c 'SELECT pg_reload_conf();'
  1. Limit the damage of a bad query or a leaked password:
psql
ALTER ROLE shop CONNECTION LIMIT 50;
ALTER ROLE report SET statement_timeout = '60s';

57.3

Check that it worked

psql -h 10.0.2.21 -U shop -d shop -c 'SELECT current_user, current_schema;'
psql -h 10.0.2.21 -U report -d shop -c 'CREATE TABLE x (id int);'
ERROR:  permission denied for schema app

The app user works, the report user can read but not write.

Commands in this lesson

57.4

Common problems

ProblemCauseFix
no pg_hba.conf entry for host …No matching line for that IP/database/user.Add the host line; SELECT pg_reload_conf();.
password authentication failedWrong password, or the role has no password yet.\password ROLE; check the method is scram-sha-256.
permission denied for schema publicPostgreSQL 15+ change.Own schema for the app, or make the app the database owner.
Report user can not read new tablesDefault privileges are missing (or set for the wrong creator role).ALTER DEFAULT PRIVILEGES FOR ROLE CREATOR ….
Peer authentication failed for user "shop"Local socket login uses peer.Use -h 127.0.0.1 (TCP), or add a local rule with scram.
Connection refused from other serverslisten_addresses = 'localhost' or the firewall.Set listen_addresses, restart; open 5432 only for the app network.

Commands in this lesson

57.5

Go further

1Why use \password instead of CREATE ROLE … PASSWORD 'secret'?

The plain password does not end up in the server log or in your psql history.

2Why can a new user not create tables in public on PostgreSQL 15+?

Since 15 only the database owner may create in public; give the app its own schema or make it the owner.

Commands in this lesson

All commands in this chapter