# 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.

URL: https://levelrail.com/learn/databases/postgres-roles-and-permissions/
Updated: 2026-11-27

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](https://levelrail.com/learn/glossary/#sudo)) (see [install Postgres and connect with psql](https://levelrail.com/learn/databases/install-postgres-and-connect-with-psql/))):

```sql
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](https://levelrail.com/learn/glossary/#secret)) in an environment variable (see [environment variables and secrets](https://levelrail.com/learn/deploy-operate/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`:

```sql
\c shop
\dn+ public
```

If the access list shows `=UC/...` for `PUBLIC`, everyone can create objects there. Remove it:

```sql
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:

```sql
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:

```sql
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

```sql
\du            -- roles and attributes
\l             -- databases, owners and access privileges
\dp            -- table privileges in the current database
```

Then prove it by logging in as the limited role and trying a write:

```bash
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 app` applies only to objects that `app` creates. 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 app` to type it without echo.
- **Dropping a role fails while it owns objects.** Use `REASSIGN OWNED BY old TO new;` and `DROP OWNED BY old;` first, then `DROP ROLE old;`. Check twice, because `DROP OWNED` removes objects.
- **Grants on the database do not reach tables.** `GRANT ALL ON DATABASE` gives connect, create and temp rights, not table access.
- **Superuser is not a feature flag for convenience.** Do not give `SUPERUSER`, `CREATEDB` or `CREATEROLE` to an app role unless the app genuinely creates databases.
- **Docker image defaults.** The `POSTGRES_USER` you 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

```sql
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](https://levelrail.com/learn/databases/install-postgres-and-connect-with-psql/)): the session you run these statements in.
- [Database connection strings explained](https://levelrail.com/learn/databases/database-connection-strings-explained/)): hand the new role to your app safely.
- [Environment variables and secrets](https://levelrail.com/learn/deploy-operate/environment-variables-and-secrets/)): where the role password should live.
- [Users, groups and sudo](https://levelrail.com/learn/linux/users-groups-and-sudo/)): the same least-privilege idea at the operating system level.
- [Postgres backup and restore with pg_dump](https://levelrail.com/learn/databases/postgres-backup-and-restore-with-pg-dump/)): roles live outside a single-database dump, so save them separately.
- [Database connection pooling and limits](https://levelrail.com/learn/databases/database-connection-pooling-and-limits/)): per-role connection limits and pools.
- [Manage secrets for self-hosted Docker apps](https://levelrail.com/blog/manage-secrets-self-hosted-docker/)): keeping database passwords out of images.

## Next

Continue with [database connection strings explained](https://levelrail.com/learn/databases/database-connection-strings-explained/)) to put the new role into a URL your app can use.

## 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

- [PostgreSQL documentation, database roles](https://www.postgresql.org/docs/current/user-manag.html)
- [PostgreSQL documentation, GRANT](https://www.postgresql.org/docs/current/sql-grant.html)
