Postgres roles and permissions for an app user and a read-only user
A Postgres role is a login or a group that owns objects and holds privileges, so you create a limited role per app with GRANT instead of letting the app use the superuser.
- Updated Nov 27, 2026
- Markdown
Create an app role and a read-only role with the minimum grants, and inspect the result with \dp.
- A running Postgres you can reach as a superuser
Roles decide who can do what in Postgres
- The owner role creates the tables, so it owns them.
- The owner runs GRANT SELECT, INSERT, UPDATE ON the tables TO the app role.
- The owner gives the read-only role only SELECT, so it cannot change data.
- The app connects as its own role and can read and write only what it was granted.
- The read-only role can run SELECT, and an INSERT is refused with permission denied.
Static diagram
- Owner rolecreate tablesDatabase
- Owner roleGRANTApp role
- Owner roleGRANT SELECTRead-only role
- App roleapp connectsDatabase
- Read-only roleread onlyDatabase
Give every app its own Postgres role that owns only its own database, and never let an app log in as the postgres superuser. With CREATE ROLE, GRANT and ALTER DEFAULT PRIVILEGES you decide exactly which databases, schemas and tables each login can read or change, so a leaked password or a bad query has a small blast radius.
How it works#
In Postgres there is one concept: the role. A role can own objects, hold privileges, and optionally log in. A role with the LOGIN attribute is what other systems call a user. A role without it works as a group: you grant privileges to the group and add login roles to it. CREATE USER is the same as CREATE ROLE ... LOGIN.
Access to a table is checked in layers. A role needs CONNECT on the database, USAGE on the schema that contains the table, and then the privilege on the table itself, such as SELECT or INSERT. Missing any layer gives permission denied. Roles are cluster-wide, but privileges are per database. The role PUBLIC is a pseudo-role meaning everyone, and some defaults are granted to it, which is why you sometimes revoke from PUBLIC.
The owner of an object can do everything with it and can grant to others. A superuser skips all checks, so an app running as superuser can drop other databases and read every table. That is why the app should not be one.
Step 1: Create the app role and database#
Connect as the superuser with sudo (see install Postgres and connect with psql):
CREATE ROLE app LOGIN PASSWORD 'change-me';
CREATE DATABASE shop OWNER app;
REVOKE CONNECT ON DATABASE shop FROM PUBLIC;
GRANT CONNECT ON DATABASE shop TO app;Replace change-me with a long random password and keep it as a secret in an environment variable (see environment variables and secrets), not in the repo. Because app owns the database, it can create tables and run migrations there. It cannot touch other databases. The default CONNECT privilege on a new database goes to PUBLIC, so the REVOKE line prevents other roles from connecting to shop.
Step 2: Handle the public schema#
Every new database has a schema named public. Before PostgreSQL 15, any role could create objects in it by default. PostgreSQL 15 changed this: CREATE on public is no longer granted to all roles, and the database owner owns the schema. Servers upgraded from older versions keep their old grants, so do not assume. Check from shop:
\c shop
\dn+ publicIf the access list shows =UC/... for PUBLIC, everyone can create objects there. Remove it:
REVOKE CREATE ON SCHEMA public FROM PUBLIC;This does not affect app, because the owner keeps its rights. Running it on an already restricted schema does no harm.
Step 3: Add a read-only role#
A read-only role suits dashboards, reporting tools and people who need to look at data. Run these in the target database, as the owner or a superuser:
CREATE ROLE reporting LOGIN PASSWORD 'change-me-too';
GRANT CONNECT ON DATABASE shop TO reporting;
GRANT USAGE ON SCHEMA public TO reporting;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO reporting;
ALTER DEFAULT PRIVILEGES FOR ROLE app IN SCHEMA public
GRANT SELECT ON TABLES TO reporting;The GRANT SELECT ON ALL TABLES line covers tables that exist now. ALTER DEFAULT PRIVILEGES FOR ROLE app covers tables that app creates later, for example in a migration. Without it, the first new table gives permission denied for table ... to the reporting role. Add GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public only if the role must read sequence values.
Step 4: Grant write access to a separate role#
If you want migrations and runtime to use different roles, keep app as the owner for migrations and create a runtime role with limited rights:
CREATE ROLE app_rw LOGIN PASSWORD 'change-me-three';
GRANT CONNECT ON DATABASE shop TO app_rw;
GRANT USAGE ON SCHEMA public TO app_rw;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_rw;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO app_rw;
ALTER DEFAULT PRIVILEGES FOR ROLE app IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_rw;
ALTER DEFAULT PRIVILEGES FOR ROLE app IN SCHEMA public
GRANT USAGE, SELECT ON SEQUENCES TO app_rw;The runtime role can read and write rows but cannot drop or alter tables. Sequences matter because an INSERT into a table with a serial or identity column needs USAGE on its sequence.
Check it worked#
\du -- roles and attributes
\l -- databases, owners and access privileges
\dp -- table privileges in the current databaseThen prove it by logging in as the limited role and trying a write:
psql -h 127.0.0.1 -U reporting -d shop -c "INSERT INTO items(name) VALUES ('x');"The expected result is ERROR: permission denied for table items. A SELECT should succeed.
Edge cases and gotchas#
- Default privileges are per creator.
ALTER DEFAULT PRIVILEGES FOR ROLE appapplies only to objects thatappcreates. Tables created by the superuser are not covered. - Passwords in SQL end up in logs. A statement with
PASSWORD '...'can appear in server logs and in~/.psql_history. In psql use\password appto type it without echo. - Dropping a role fails while it owns objects. Use
REASSIGN OWNED BY old TO new;andDROP OWNED BY old;first, thenDROP ROLE old;. Check twice, becauseDROP OWNEDremoves objects. - Grants on the database do not reach tables.
GRANT ALL ON DATABASEgives connect, create and temp rights, not table access. - Superuser is not a feature flag for convenience. Do not give
SUPERUSER,CREATEDBorCREATEROLEto an app role unless the app genuinely creates databases. - Docker image defaults. The
POSTGRES_USERyou set becomes a superuser. Create a second, limited role for the app rather than using that one.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
ERROR: permission denied for table items | No table privilege | GRANT SELECT ON items TO reporting; |
ERROR: permission denied for schema public | No USAGE on the schema | GRANT USAGE ON SCHEMA public TO reporting; |
FATAL: permission denied for database "shop" | No CONNECT privilege | GRANT CONNECT ON DATABASE shop TO reporting; |
ERROR: permission denied for sequence items_id_seq | Insert without sequence privilege | GRANT USAGE, SELECT ON SEQUENCE items_id_seq TO app_rw; |
ERROR: permission denied to create role | Current role lacks CREATEROLE | Run as a superuser |
ERROR: role "app" already exists | Role name taken | Use ALTER ROLE app PASSWORD '...' or pick another name |
ERROR: role "old" cannot be dropped because some objects depend on it | Role still owns objects | REASSIGN OWNED BY old TO app; then DROP OWNED BY old; |
| New table not readable by the read-only role | Default privileges missing or set for another creator | Re-run ALTER DEFAULT PRIVILEGES FOR ROLE <creator> ... |
Quick reference#
CREATE ROLE app LOGIN PASSWORD 'change-me';
CREATE DATABASE shop OWNER app;
REVOKE CONNECT ON DATABASE shop FROM PUBLIC;
GRANT CONNECT ON DATABASE shop TO reporting;
GRANT USAGE ON SCHEMA public TO reporting;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO reporting;
ALTER DEFAULT PRIVILEGES FOR ROLE app IN SCHEMA public
GRANT SELECT ON TABLES TO reporting;
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
\du \l \dp \dn+Related lessons#
- Install Postgres and connect with psql: the session you run these statements in.
- Database connection strings explained: hand the new role to your app safely.
- Environment variables and secrets: where the role password should live.
- Users, groups and sudo: the same least-privilege idea at the operating system level.
- Postgres backup and restore with pg_dump: roles live outside a single-database dump, so save them separately.
- Database connection pooling and limits: per-role connection limits and pools.
- Manage secrets for self-hosted Docker apps: keeping database passwords out of images.
Next#
Continue with database connection strings explained to put the new role into a URL your app can use.
Try it: Create an app role and a database it owns
Run as the postgres superuser. Create a login role, a database it owns, and inspect it.
Want a real machine? Free browser labs: Killercoda
Check what you learned
Frequently asked questions
What is the difference between a role and a user in PostgreSQL?
There is no difference in storage. CREATE USER is CREATE ROLE with LOGIN already set, so a user is a role that can log in.
Should my app connect as the postgres superuser?
No. A superuser bypasses every permission check, so a SQL injection or a bad migration can do anything. Give the app a role that owns only its own database.
How do I create a read-only user in Postgres?
Create a login role, grant CONNECT on the database, USAGE on the schema and SELECT on the tables, then use ALTER DEFAULT PRIVILEGES so new tables are covered too.
Why can my new role still create tables in the public schema?
Before PostgreSQL 15, every role had CREATE on the public schema by default. Revoke it explicitly if your server is older or was upgraded from an older version.
Sources
Keep going: guides and tutorials
- Self-Hosted Postgres in Docker: Connect It to Your App Run Postgres in Docker as its own resource with its own volume, connect your app with one command, and read the reachability badge before a timeout hits.
- Manage Secrets Self-Hosted: Env Vars and Master Key Rotation Set env vars and encrypted secrets on a self-hosted server, apply changes safely, and rotate the master key with no downtime without locking yourself out.