Tablux

Tablux › Guides › Postgres read-only user

Guide

How to create a read-only user in PostgreSQL

Create a login role, then grant it CONNECT on the database, USAGE on the schema and SELECT on its tables: CREATE ROLE reporting LOGIN PASSWORD '…'; GRANT CONNECT ON DATABASE appdb TO reporting; GRANT USAGE ON SCHEMA public TO reporting; GRANT SELECT ON ALL TABLES IN SCHEMA public TO reporting; Add ALTER DEFAULT PRIVILEGES FOR ROLE so tables created later are readable too.

The full recipe

Run this as the database owner or an admin. It assumes a database appdb, tables in schema public and a role app_owner that creates them (usually your migrations user):

CREATE ROLE reporting LOGIN PASSWORD 'use-a-long-random-password';

GRANT CONNECT ON DATABASE appdb TO reporting;
GRANT USAGE ON SCHEMA public TO reporting;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO reporting;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO reporting;

-- tables and sequences app_owner creates later
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA public
  GRANT SELECT ON TABLES TO reporting;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA public
  GRANT SELECT ON SEQUENCES TO reporting;

-- belt and braces
ALTER ROLE reporting SET default_transaction_read_only = on;
ALTER ROLE reporting SET statement_timeout = '30s';

Each line closes a different gap. CONNECT lets the role open the database (PUBLIC has it by default, but many hardened setups revoke it). USAGE on the schema lets it see the objects inside. SELECT ON ALL TABLES covers every table, view and foreign table that exists right now. For more schemas, list them: GRANT USAGE ON SCHEMA sales, billing TO reporting;.

ALTER DEFAULT PRIVILEGES and the FOR ROLE gotcha

GRANT SELECT ON ALL TABLES is a one-off: a table created tomorrow is not readable by reporting. ALTER DEFAULT PRIVILEGES fixes that, with a catch that trips most people up. Default privileges belong to the role that creates the objects. Without FOR ROLE, the statement only affects objects created later by whoever ran it.

So if you run ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO reporting as postgres, and your migrations run as app_owner, new tables still come out unreadable. Name the creating role with FOR ROLE, once for each role that creates tables. You can do this for yourself or for roles you are a member of. Check the result with \ddp in psql:

appdb=# \ddp
         Default access privileges
   Owner   | Schema |   Type   |   Access privileges
-----------+--------+----------+-----------------------
 app_owner | public | sequence | reporting=r/app_owner
 app_owner | public | table    | reporting=r/app_owner

Sequences

SELECT on a sequence lets the role read its current value (SELECT last_value FROM orders_id_seq), which some reporting and dump tools do. It does not allow nextval(), which needs USAGE or UPDATE, so it does not let the role write anything. If you never read sequences directly you can skip those two lines.

The shortcut: pg_read_all_data (PostgreSQL 14+)

PostgreSQL 14 added the predefined role pg_read_all_data. Members can read every table, view and sequence in every schema of every database, now and in the future, as if they had SELECT and schema USAGE everywhere:

CREATE ROLE reporting LOGIN PASSWORD 'use-a-long-random-password';
GRANT pg_read_all_data TO reporting;

No default privileges to manage. Two cautions. It is cluster-wide, so it is too broad when one server holds several applications' databases. And it does not bypass row-level security: on tables with RLS enabled the role only sees rows a policy allows. Granting it requires a superuser or a role with ADMIN OPTION on pg_read_all_data; since PostgreSQL 16, a plain CREATEROLE user gets permission denied to grant role "pg_read_all_data".

default_transaction_read_only and statement_timeout

ALTER ROLE … SET default_transaction_read_only = on makes every transaction for that role start read-only, so even a write the grants would allow (a temporary table, a function that writes) fails with cannot execute … in a read-only transaction. It catches mistakes. It is not a security boundary: it is only a default, and the role can turn it off for its own session:

SET default_transaction_read_only = off;   -- allowed for any role
BEGIN READ WRITE;                          -- so is this

The grants are what actually stop writes. Keep both.

statement_timeout stops a runaway report from holding locks or eating the server: any statement longer than the limit is cancelled. A value without units is milliseconds; 0 means no limit. idle_in_transaction_session_timeout and CONNECTION LIMIT are worth adding for shared read-only logins:

ALTER ROLE reporting SET idle_in_transaction_session_timeout = '5min';
ALTER ROLE reporting CONNECTION LIMIT 5;
ALTER ROLE reporting SET search_path = public;

These settings apply to new sessions. Confirm them with SELECT rolname, rolconfig FROM pg_roles WHERE rolname = 'reporting';.

Check what the role can do

\du reporting                 -- role attributes, connection limit
\dp public.orders             -- privileges on one table

SELECT has_table_privilege('reporting', 'public.orders', 'SELECT') AS can_read,
       has_table_privilege('reporting', 'public.orders', 'INSERT') AS can_insert,
       has_schema_privilege('reporting', 'public', 'CREATE')        AS can_create;

SELECT table_schema, table_name, privilege_type
FROM information_schema.role_table_grants
WHERE grantee = 'reporting'
ORDER BY 1, 2;

You want can_read true and the other two false. In databases created before PostgreSQL 15 (including ones upgraded since), PUBLIC can create objects in the public schema; run REVOKE CREATE ON SCHEMA public FROM PUBLIC; there. PostgreSQL 15 changed the default for new databases. Then log in as the new role and try a write; you should get permission denied.

Revoke access or drop the role

REVOKE SELECT ON ALL TABLES IN SCHEMA public FROM reporting;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA public
  REVOKE SELECT ON TABLES FROM reporting;
-- same again with SEQUENCES if you granted those

-- removing the role entirely: run DROP OWNED in every database it has privileges in
DROP OWNED BY reporting;
DROP ROLE reporting;

DROP ROLE fails with role "reporting" cannot be dropped because some objects depend on it while grants or default privileges still mention it. DROP OWNED BY removes those (and drops anything the role owns, which for a read-only role should be nothing; if in doubt, run REASSIGN OWNED BY reporting TO app_owner; first).

Managed services: RDS, Aurora and Supabase

Amazon RDS and Aurora PostgreSQL. The master user is not a true superuser; it is a member of rds_superuser. The per-schema grants above work as written. If GRANT pg_read_all_data is refused, use them instead. On Aurora PostgreSQL, AWS notes that a role needs CONNECT on the database even when it is in rds_superuser, so keep the GRANT CONNECT line.

Supabase. Create the role in the SQL editor with the same statements. Two things differ. Through the shared connection pooler (Supavisor) the user name is role.project-ref, for example reporting.abcdefghijklmnop; direct connections use the plain role name. And Supabase tables in public often have row-level security on, so a new role with SELECT sees zero rows until a policy lets it in:

CREATE POLICY reporting_read ON public.orders
  FOR SELECT TO reporting USING (true);

Use session mode (port 5432) rather than transaction mode (6543) for a read-only login: transaction mode does not keep session-level state, so a session setting such as SET default_transaction_read_only may not stick.

Using the login from a client

The public Tablux demo database is set up this way: a login with default_transaction_read_only, a fixed search_path and a 15-second statement timeout. If you connect with Tablux, you can also mark a connection read-only, and on PostgreSQL the app then opens a read-only session on the server itself as well as refusing writes in the app; see production safety. A read-only database role is still the layer to rely on.

Questions

Why can my read-only user not see new tables?

GRANT SELECT ON ALL TABLES only covers tables that exist when you run it. Add ALTER DEFAULT PRIVILEGES FOR ROLE table_creator IN SCHEMA ... GRANT SELECT ON TABLES TO your user, naming the role that creates the tables.

What is the difference between CREATE USER and CREATE ROLE?

None that matters here: CREATE USER is CREATE ROLE with LOGIN implied. Both create a role that can log in.

Is default_transaction_read_only enough to make a user read-only?

No. It is a default the user can turn off with SET default_transaction_read_only = off or BEGIN READ WRITE. Use grants as the boundary and the setting as a safety net.

Should I use pg_read_all_data?

On PostgreSQL 14 and later it is the simplest way to read everything, including future tables. It applies to every database on the server and does not bypass row-level security, so use per-schema grants when you need narrower access.

Why does the read-only user get permission denied for schema?

SELECT on the tables is not enough; the role also needs USAGE on the schema. Run GRANT USAGE ON SCHEMA schema_name TO the role.

Updated 2026-11-25