---
title: "Create Read-Only Users"
description: "Create a read-only PostgreSQL user on Managed PostgreSQL that can read existing and future tables and nothing else."
url: https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/create-read-only-users/
product: "PostgreSQL"
section: product
last_updated: 2026-11-10
---
> For AI agents: the documentation index is at https://community.exoscale.com/llms.txt. Every page is available as markdown at `<page URL>index.md` or with `Accept: text/markdown`.

# Create Read-Only Users

Build a PostgreSQL user that can read your data and nothing else, including the tables that do not exist yet.

## How the Pieces Fit Together

A robust read-only setup has three parts:

1. A `NOLOGIN` **group role** that carries the privileges. It is created in SQL and never used to
   connect.
2. A **login identity** that inherits the group role. Use a user created with
   `exo dbaas user create` when you want the platform to manage the password, or a SQL user
   when you prefer to manage it yourself.
3. `ALTER DEFAULT PRIVILEGES`, so that tables created *after* the grants are covered too. Without
   it, the user goes blind the next time a migration adds a table.

Run every SQL statement below as `avnadmin`, connected to the database you want to open up.
`avnadmin` holds `CREATEROLE`, so it can create and grant roles, but it is not a superuser: see
[Manage users and databases](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/manage-users-and-databases/index.md).

> [!IMPORTANT]
> Privileges in PostgreSQL are per database. Repeat the whole recipe in every database the user
> must read, including any database you create later.

## Create the Read-Only Group Role

```sql
CREATE ROLE readonly NOLOGIN;

GRANT CONNECT ON DATABASE defaultdb TO readonly;
GRANT USAGE ON SCHEMA public TO readonly;

GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO readonly;
```

`GRANT ... ON ALL TABLES` is a one-shot loop over the tables that exist right now. It is not a
standing rule, which is why the next step is not optional.

## Cover Future Tables with ALTER DEFAULT PRIVILEGES

```sql
ALTER DEFAULT PRIVILEGES FOR ROLE avnadmin IN SCHEMA public
  GRANT SELECT ON TABLES TO readonly;

ALTER DEFAULT PRIVILEGES FOR ROLE avnadmin IN SCHEMA public
  GRANT SELECT ON SEQUENCES TO readonly;
```

> [!WARNING]
> `ALTER DEFAULT PRIVILEGES` is scoped to the **creating role**, not to the schema. The statements
> above only affect objects created by `avnadmin`. If your migrations run as `app_owner` and your
> ETL job creates tables as `etl`, you must repeat both statements with
> `FOR ROLE app_owner` and `FOR ROLE etl`, otherwise the new tables will be invisible to
> `readonly`.

List what is currently configured, so you can tell which creating roles are already covered:

```sql
SELECT pg_get_userbyid(defaclrole)   AS creating_role,
       defaclnamespace::regnamespace AS schema,
       defaclobjtype,
       defaclacl
  FROM pg_default_acl;
```

## Attach a Login Identity

### Option A: a Service User with Platform-Managed Credentials

Create the user through the control plane, then grant it the group role:

```bash
exo dbaas user create SERVICE_NAME USERNAME -z ZONE
```

Parameters:

- `SERVICE_NAME`: name of the Managed PostgreSQL service, for example `my-postgresql`.
- `USERNAME`: name of the read-only user, for example `reporting`.
- `ZONE`: zone the service runs in, for example `ch-gva-2`.

```sql
GRANT readonly TO reporting;
```

Read the generated password back with:

```bash
exo dbaas user reveal-secrets my-postgresql reporting -z ch-gva-2
```

### Option B: a SQL-Only User

```sql
CREATE USER reporting WITH PASSWORD 'REPLACE_WITH_A_STRONG_PASSWORD';
GRANT readonly TO reporting;
```

The user appears in `exo dbaas user list`, but the platform does not hold its password:
`exo dbaas user reveal-secrets` returns an empty password until you run
`exo dbaas user reset-credentials` on it. Pick option A unless you have a reason not to.

## Close the Remaining Doors

New roles inherit whatever `PUBLIC` already has. On a fresh database `PUBLIC` holds `CONNECT` on
the database and `USAGE` on the `public` schema, so a read-only user is not as isolated as it
looks. Tighten it:

```sql
REVOKE ALL ON DATABASE defaultdb FROM PUBLIC;
GRANT CONNECT ON DATABASE defaultdb TO readonly;

REVOKE CREATE ON SCHEMA public FROM PUBLIC;
```

> [!NOTE]
> On the PostgreSQL versions Managed PostgreSQL offers (15, 16, 17 and 18), the last statement
> is normally a no-op: since PostgreSQL 15, `PUBLIC` no longer receives `CREATE` on the `public`
> schema. On a PostgreSQL 18 service the ACL of `public` in `defaultdb` reads
> `pg_database_owner=UC/pg_database_owner,=U/pg_database_owner`, where the `=U` entry is `PUBLIC`
> holding `USAGE` and nothing else. Keep the statement as a safety net for a database restored
> from an older dump or whose ACLs were edited by hand, and read the current state with
> `SELECT has_schema_privilege('public', 'public', 'CREATE');`.

> [!CAUTION]
> `REVOKE ... FROM PUBLIC` affects every role in the database, including your applications: a role
> without its own `CONNECT` grant can no longer connect, with
> `User does not have CONNECT privilege`. Grant the privileges back explicitly to the roles that
> need them, in the same transaction, and test before you do this on a production service.

As a second layer, force every session opened by the user into a read-only transaction:

```sql
ALTER ROLE reporting SET default_transaction_read_only = on;
```

This is a convenience, not the security boundary: the `GRANT` and `REVOKE` statements enforce
read-only access, and a client can still run `SET default_transaction_read_only = off` in its own
session. `avnadmin` can run this statement on users created with `exo dbaas user create` and on
roles it created itself, because it holds `ADMIN OPTION` on them.

## Do Not Count on pg_read_all_data

PostgreSQL has a predefined `pg_read_all_data` role that grants read access to every table in one
statement. On Managed PostgreSQL, `avnadmin` is not a member of it and holds no `ADMIN OPTION` on
it, so `GRANT pg_read_all_data TO reporting;` fails with
`permission denied to grant role "pg_read_all_data"`. Build the privileges explicitly, as above.
Check what your own admin user is allowed to grant:

```sql
SELECT g.rolname AS granted_role, m.admin_option
  FROM pg_auth_members m
  JOIN pg_roles r ON r.oid = m.member
  JOIN pg_roles g ON g.oid = m.roleid
 WHERE r.rolname = current_user;
```

## Restrict a User to a Single Schema

When you host several applications or tenants in one database, give each one its own schema and
keep the read-only role inside it.

```sql
CREATE SCHEMA app_a;
CREATE ROLE app_a_ro NOLOGIN;

GRANT CONNECT ON DATABASE defaultdb TO app_a_ro;
GRANT USAGE ON SCHEMA app_a TO app_a_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA app_a TO app_a_ro;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA app_a TO app_a_ro;

ALTER DEFAULT PRIVILEGES FOR ROLE avnadmin IN SCHEMA app_a
  GRANT SELECT ON TABLES TO app_a_ro;

REVOKE ALL ON SCHEMA public FROM app_a_ro;

CREATE USER app_a_reader WITH PASSWORD 'REPLACE_WITH_A_STRONG_PASSWORD';
GRANT app_a_ro TO app_a_reader;
ALTER ROLE app_a_reader SET search_path = app_a;
```

Setting `search_path` is ergonomics, not access control: the user still cannot read `app_b`
because no grant says otherwise. Keep in mind that any role can read the system catalogs, so
table and column *names* across the whole database remain discoverable.

## Verify the Result

Inspect the roles and their memberships:

```sql
SELECT rolname, rolcanlogin, rolsuper, rolcreatedb, rolcreaterole
  FROM pg_roles
 ORDER BY 1;
```

```sql
SELECT r.rolname AS member, g.rolname AS granted_role
  FROM pg_auth_members m
  JOIN pg_roles r ON r.oid = m.member
  JOIN pg_roles g ON g.oid = m.roleid
 ORDER BY 1, 2;
```

Inspect the effective grants on a table, either with the catalog:

```sql
SELECT grantee, privilege_type
  FROM information_schema.role_table_grants
 WHERE table_schema = 'public'
   AND table_name = 'orders';
```

Or with the psql meta-command, which prints the access privileges of every relation in the schema:

```text
\dp public.*
```

Finally, prove it from the outside. Connect to the service as the read-only user and run a
`SELECT`, which must succeed, then an `INSERT` into the same table, which must fail with
`permission denied for table`.

A user that can still write holds that privilege from a source other than the grants above.
There are three of them. A privilege granted to `PUBLIC`, which `\dp` prints as an entry with an
empty grantee, such as `=a/avnadmin` for `INSERT`. Membership in a role that can write, which
`\dp` shows under that role's name rather than the user's. Or ownership of the table, since the
owner always holds every privilege on it: `\dp` leaves the column empty when the privileges are
still the default, so read the owner with `\dt` instead.

A missing `FOR ROLE` clause produces the opposite symptom: tables created by the uncovered role
carry no grant at all, so the read-only user cannot even `SELECT` them and gets the same
`permission denied for table` error.

## Related Pages

- [Manage users and databases](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/manage-users-and-databases/index.md)
- [Claim public schema ownership](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/claim-public-schema-ownership/index.md)
- [Manage extensions](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/manage-extensions/index.md)
- [DBaaS overview](https://community.exoscale.com/product/dbaas/overview/index.md)
- [Privileges in the PostgreSQL documentation](https://www.postgresql.org/docs/18/ddl-priv.html) ↗

