Testing PostgreSQL Read-Only Roles and Default Table Privileges
After a reporting application connects to PostgreSQL on a dedicated server, queries against existing tables work, but new tables return permission errors. Someone then grants overly broad privileges as a quick fix. The problem is not server capacity: login identities, object ownership, and default privileges have not been tied together consistently. Validating a read-only account must prove both that approved reads work and that unauthorized writes are actually denied. One successful query is not enough.
1. Define the Test Scope and Identities
This exercise assumes PostgreSQL is already running on Debian 12 or Ubuntu Server 24.04 LTS. Prepare a separate test database and ensure the client has psql available. The example does not install PostgreSQL, change connection authentication, or touch application tables.
Configure ro-lab-admin in the client's connection service file beforehand. It should point to the test database and use an administrative identity with permission to create roles. Use the connection address, identity, and authentication material appropriate to your environment; do not put passwords in command history. Server access credentials can be viewed in the console after payment. Database credentials are configured by the database administrator.
psql -X "service=ro-lab-admin" -v ON_ERROR_STOP=1
Once connected, run:
SELECT current_database(), session_user, current_user;
SHOW server_version;
Verify the database name, then check that the role and schema names used below are not already in use. Roles belong to the entire database cluster, so their names must not conflict even when the test database is separate.
Making transactions read-only by default does not provide privilege isolation. Object owners can modify their objects, and superusers can bypass ordinary privilege checks. Setting these identities to read-only by default does not prove that writes have been blocked.
This example uses two NOLOGIN roles, one for creating tables and one for reading them, to keep password management separate from the object-privilege exercise. It is suitable for testing table-level boundaries, not as a complete production authorization design involving functions, views, and multiple levels of role inheritance.
2. Grant Access to Existing and Future Tables Separately
Run the following in the same administrative session. The administrative identity creates the roles and schema. Before creating tables, explicitly switch to the owner role so the identity that creates each test object is traceable.
CREATE ROLE ic_ro_owner NOLOGIN;
CREATE ROLE ic_ro_reader NOLOGIN;
CREATE SCHEMA ro_lab AUTHORIZATION ic_ro_owner;
SET ROLE ic_ro_owner;
CREATE TABLE ro_lab.sample (
id bigint PRIMARY KEY,
note text NOT NULL
);
INSERT INTO ro_lab.sample VALUES (1, 'baseline');
GRANT USAGE ON SCHEMA ro_lab TO ic_ro_reader;
GRANT SELECT ON ALL TABLES IN SCHEMA ro_lab TO ic_ro_reader;
ALTER DEFAULT PRIVILEGES
FOR ROLE ic_ro_owner IN SCHEMA ro_lab
GRANT SELECT ON TABLES TO ic_ro_reader;
RESET ROLE;
USAGE allows object names within the schema to be resolved; it does not grant permission to read tables. Granting SELECT on existing tables and setting default privileges for future tables are separate steps. Omitting either leaves a gap.
Default privileges follow the role that actually creates an object, not the schema owner. If a deployment script creates tables under another role, the default grant above will not automatically apply to them. Default privileges are not retroactive either, so the migration process must explicitly identify which role creates tables.
Only table-read access is granted here, not permission to access or advance sequences. An ordinary query that reads an auto-increment column does not need to call its sequence. Do not grant access to all sequences or full privileges on all objects simply to make an error disappear.
3. Test Successful Reads and Rejected Writes
While still using the administrative identity, create a table after the grants have been configured, then switch to the read-only role:
SET ROLE ic_ro_owner;
CREATE TABLE ro_lab.future_sample (id bigint PRIMARY KEY);
INSERT INTO ro_lab.future_sample VALUES (2);
RESET ROLE;
SET ROLE ic_ro_reader;
SELECT session_user, current_user;
SELECT * FROM ro_lab.sample;
SELECT * FROM ro_lab.future_sample;
\set ON_ERROR_STOP off
INSERT INTO ro_lab.sample VALUES (3, 'denied');
\echo :SQLSTATE
UPDATE ro_lab.sample SET note = 'changed' WHERE id = 1;
\echo :SQLSTATE
DELETE FROM ro_lab.sample WHERE id = 1;
\echo :SQLSTATE
TRUNCATE ro_lab.sample;
\echo :SQLSTATE
CREATE TABLE ro_lab.denied_table (id bigint);
\echo :SQLSTATE
\set ON_ERROR_STOP on
SELECT * FROM ro_lab.sample;
RESET ROLE;
Both queries must succeed, and all five negative tests should return 42501, meaning insufficient privilege. The original record must still contain baseline afterward. The negative tests deliberately disable the client's stop-on-error behavior and restore it immediately afterward. Do not treat every nonzero result as proof of a successful denial: syntax mistakes and lost connections also produce errors.
Run these statements in a separate test session without an explicit BEGIN. If they are wrapped in one transaction, the first error will put the transaction into a failed state. Subsequent statements may then return only 25P02, without actually testing their respective privileges.
Here, SET ROLE tests the target role's effective object privileges. The administrative session's session_user remains the administrative identity, so this test cannot prove that the real application account has no other sources of privileges.
4. Repeat the Checks with the Real Login Identity
Have the administrator grant the reader role to the actual reporting login role. Grant only the reader role, not the owner role. Use the real account name; do not copy an experimental name in place of approving the actual identity.
Open a separate application connection, record session_user and current_user, repeat both read tests and all five denial tests, and confirm that transactions are read-write by default. Testing inside a read-only transaction may reject an operation solely because of the transaction setting, masking excess privileges.
Also check whether the login role is a superuser, can create roles, owns any target objects, or can switch to another role with write access. If a connection pool is already in use, establish fresh connections for the retest rather than drawing conclusions from role state retained in old sessions.
Functions and Schemas Provide Additional Entry Points
Passing the table-privilege checks does not mean every write path is closed. An executable SECURITY DEFINER function may write data using its owner's identity. Review the function body, execution privileges, and whether its search_path is secure. This exercise creates no such functions, so it cannot establish that application functions are safe.
Also check whether the login role can create objects in other application schemas. Do not revoke privileges from PUBLIC wholesale in a production database as a quick lockdown measure. Inventory dependencies first, then make changes individually. The ability to create temporary tables must also be assessed separately from permission to write application tables, and the validation report must state its scope.
5. Record the Evidence and Remove the Test Objects
At a minimum, retain the database name, server version, test identities, query results from both tables, and the five rejected statements with their corresponding SQLSTATE values. Also record current_user when creating the new table: this is key evidence for determining whether default grants will carry through to the next deployment.
Before cleanup, exit all read-only test sessions and verify the administrative identity and database again. The following removes only the objects from this exercise. If either role is already referenced elsewhere, stop and inspect the dependencies rather than broadening the cleanup.
DROP SCHEMA ro_lab CASCADE;
ALTER DEFAULT PRIVILEGES FOR ROLE ic_ro_owner
REVOKE SELECT ON TABLES FROM ic_ro_reader;
DROP ROLE ic_ro_reader;
DROP ROLE ic_ro_owner;
The final acceptance criterion is not that an account has “read-only” in its name. The actual login identity must be able to read approved data, future objects must receive the expected privileges, writes and object creation must be explicitly denied, and additional role and function entry points must have been checked. Include these checks in post-migration validation to avoid repeatedly patching privileges whenever new tables go live.
Frequently asked questions
Can default_transaction_read_only replace read-only grants?
No. It sets default transaction behavior, not an object permission boundary. Remove write privileges, ownership and access to writable roles, then inspect executable functions separately.
Why is a new table unreadable after setting default privileges?
Default privileges follow the role that actually creates the object. Check current_user during table creation and configure default SELECT grants for that role. Existing tables still need a separate GRANT.
AMD EPYC 7543P dedicated server,from $445.3/month