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:
- A
NOLOGINgroup role that carries the privileges. It is created in SQL and never used to connect. - A login identity that inherits the group role. Use a user created with
exo dbaas user createwhen you want the platform to manage the password, or a SQL user when you prefer to manage it yourself. 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.
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
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
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:
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:
exo dbaas user create SERVICE_NAME USERNAME -z ZONEParameters:
SERVICE_NAME: name of the Managed PostgreSQL service, for examplemy-postgresql.USERNAME: name of the read-only user, for examplereporting.ZONE: zone the service runs in, for examplech-gva-2.
GRANT readonly TO reporting;Read the generated password back with:
exo dbaas user reveal-secrets my-postgresql reporting -z ch-gva-2Option B: a SQL-Only User
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:
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:
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:
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.
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:
SELECT rolname, rolcanlogin, rolsuper, rolcreatedb, rolcreaterole
FROM pg_roles
ORDER BY 1;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:
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:
\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.