$ run as a normal user# run as root (or with sudo)
The quick answer
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.
Step by step
- Connect as the admin with peer login (no password, the Linux user
postgres):
- 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.
- PostgreSQL 15+: ordinary users can no longer create tables in the
publicschema. Two clean ways:
- give the app its own schema (as above), or
- make the app the owner of the database — then it owns
publictoo (since 15 the database owner ownspublic).
- A read-only role for reports — rights on the existing tables and on tables made later:
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.)
- Allow the network login in
pg_hba.conf— only from the app's network, only for its database, withscram-sha-256:
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:
- Limit the damage of a bad query or a leaked password:
ALTER ROLE shop CONNECTION LIMIT 50;
ALTER ROLE report SET statement_timeout = '60s';Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
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 failed | Wrong password, or the role has no password yet. | \password ROLE; check the method is scram-sha-256. |
permission denied for schema public | PostgreSQL 15+ change. | Own schema for the app, or make the app the database owner. |
| Report user can not read new tables | Default 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 servers | listen_addresses = 'localhost' or the firewall. | Set listen_addresses, restart; open 5432 only for the app network. |
Go further
- Command pages: psql.
- Next how-tos: Back up and restore PostgreSQL, See who is connected to a database, Open a port in the firewall, Find slow PostgreSQL queries.
- Full guide: Install and set up PostgreSQL. At work: PostgreSQL in daily use.
- Practise: Lab: a web app, a database and a cache with Compose.
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.