Database engineer reviewing PostgreSQL creator roles, default privileges, and table access permissions in a SQL terminal.

Your PostgreSQL Migration Created a Table. Whose Default Privileges Applied?

Fri, Nov 27, 2026

A migration completes successfully. The new table exists, its columns are correct, the deployment reports success, yet the application reader gets permission denied. That failure is often investigated as though “the migration role,” “the owner,” and “the role whose default privileges were configured” were interchangeable. PostgreSQL does not treat them that way.

Executive summary. For a newly created table, PostgreSQL consults the default privileges of the current role that creates the object, not the defaults of roles whose privileges that creator merely inherits. SET ROLE therefore matters: it changes current_user, and objects created after activation are considered owned by that activated role. Default privileges are creation-time templates; changing them does not rewrite existing objects. Likewise, changing an existing table’s owner does not replay the new owner’s creation-time template. Existing tables with wrong access require a separate object-ACL repair, and executable access can still fail when schema USAGE is missing even though table SELECT is present.

This article specifies a disposable acceptance lab pinned to PostgreSQL 18.7 and psql 18.7. PostgreSQL’s 18.7 release notes identify November 12, 2026 as that release’s date; the runtime must independently report server_version_num = 180007 rather than trusting the documentation URL. All eleven case outcomes below are EXPECTED / NOT EXECUTED. No logs, OIDs, process IDs, timings, digests, or passing results are claimed.

Write the permission contract before changing a grant

The controlled contract is deliberately small. Ordinary permanent tables in schema acl_lab should belong to acl_owner. acl_reader may SELECT approved acl_lab tables but may not INSERT; it should not SELECT tables in acl_other. acl_outsider should not SELECT either schema’s fixture tables. Deviations in D01–D10 are controls, not acceptable end states.

That contract has several independently testable layers:

Layer

Question

Session identity

Which login authenticated: session_user?

Effective creator

Which role was current_user at CREATE TABLE?

Future template

Which pg_default_acl row applied to that creator, schema and object class?

Current ownership

Which OID appears in pg_class.relowner now?

Materialized table ACL

What is in pg_class.relacl, including NULL?

Schema access

Does the reader hold USAGE on the containing schema?

Executable access

Can a fresh real acl_reader login run the fully qualified SELECT?

PostgreSQL documents that default privileges affect future objects, and that the template used at creation belongs to the current role rather than being inherited from group memberships. The owner of a table normally receives the built-in owner privileges, while table access for another role must arise through grants, memberships, or PUBLIC.

postgres-img

This is not a substitute for PostgreSQL row-security validation under the runtime role. RLS policies, tenant filtering, owner bypass and row-policy matrices are explicitly outside this article.

Pin the runtime and separate the identities

The lab uses four controlled roles:

Role

Login

Other required attributes

acl_owner

NOLOGIN

NOSUPERUSER, NOCREATEDB, NOCREATEROLE, NOREPLICATION, NOBYPASSRLS

acl_migrator

LOGIN

same restrictions

acl_reader

LOGIN

same restrictions

acl_outsider

LOGIN

same restrictions

Only acl_migrator is a member of acl_owner, with INHERIT TRUE, SET TRUE, ADMIN FALSE. PostgreSQL 18 records those three membership properties in pg_auth_members, which is cluster-wide because role identities and membership are cluster-wide. INHERIT TRUE lets the migrator exercise privileges inherited from acl_owner; SET TRUE independently permits SET ROLE acl_owner. Once activated, objects created by the session are considered owned by the activated role.

The laboratory therefore belongs on an owned, private, disposable cluster. The eleven databases isolate database-local pg_default_acl state, while role membership remains shared cluster state. PostgreSQL explicitly documents global-versus-per-schema default-ACL records as database catalog state, not cluster-wide role policy.

Declare four connection profiles outside the scripts:

# profiles.env -- NO CREDENTIALS
ACL_BOOTSTRAP_SERVICE=acl_bootstrap
ACL_MIGRATOR_SERVICE=acl_migrator
ACL_READER_SERVICE=acl_reader
ACL_OUTSIDER_SERVICE=acl_outsider

# Administrative record only; these labels do not prove what the server used.
ACL_BOOTSTRAP_AUTH='site-defined'
ACL_MIGRATOR_AUTH='site-defined'
ACL_READER_AUTH='site-defined'
ACL_OUTSIDER_AUTH='site-defined'

Credential or certificate provisioning is an explicit prerequisite outside this object-authorization qualification. The runner must refuse a missing service profile or a profile that authenticates as the wrong role; it must never print a password, connection secret, or substitute privileged impersonation for the reader login.

psql -X suppresses system and user startup files. With ON_ERROR_STOP=on, a noninteractive script stops on error and exits with status 3; VERBOSITY=verbose exposes detailed server diagnostics, and psql exposes SQLSTATE variables for failed commands.

Verify a real login before interpreting its permissions

The preflight must establish client version, server version, endpoint and authenticated role. SET ROLE changes current_user without changing session_user, and it does not apply the target role’s login-time ALTER ROLE settings.

# preflight.sh -- executing identities: each declared service; database: postgres
set -euo pipefail
. ./profiles.env

for v in ACL_BOOTSTRAP_SERVICE ACL_MIGRATOR_SERVICE ACL_READER_SERVICE ACL_OUTSIDER_SERVICE; do
  test -n "${!v:-}" || { echo "missing $v" >&2; exit 40; }
done

client_num="$(
  psql -X "service=$ACL_BOOTSTRAP_SERVICE dbname=postgres" -At \
    -c '\echo :VERSION_NUM'
)"
test "$client_num" = "180007" || {
  echo "psql baseline mismatch: expected 180007" >&2; exit 41;
}

psql -X "service=$ACL_BOOTSTRAP_SERVICE dbname=postgres" \
  -v ON_ERROR_STOP=on -v VERBOSITY=verbose -At <<'SQL'
SELECT current_setting('server_version_num')::int = 180007 AS server_ok \gset
\if :server_ok
\else
  \echo 'server baseline mismatch'
  \quit 42
\endif
SELECT version(),
       current_setting('server_version_num'),
       current_database(),
       inet_server_addr(),
       inet_server_port(),
       session_user,
       current_user;
SQL

After cluster bootstrap, the migrator, reader and outsider service checks must respectively report acl_migrator, acl_reader and acl_outsider for both identities before any test interpretation. A successful bootstrap connection proves administrative reachability only; it does not prove application access.

Build independent case databases and an expected ledger

Each case gets a separate database, acl_lab_d01 through acl_lab_d11, created from template0. The bootstrap refuses collisions instead of quietly dropping an existing database. That prevents a test run from laundering pre-existing state into “clean” evidence.

Structural schema checks belong to a different contract; see verifying the actual schema after bootstrap. Here every test table intentionally has the identical definition:

(id integer PRIMARY KEY, marker text NOT NULL)

and exactly one authored row:

1 | case-table-marker

The marker is part of the expected ledger before any observation.

The administrative bootstrap is:

-- 00_bootstrap_cluster.sql
-- executing identity: ACL_BOOTSTRAP_SERVICE
-- database: postgres
\set ON_ERROR_STOP on
\set VERBOSITY verbose

SELECT NOT EXISTS (
  SELECT 1 FROM pg_roles
  WHERE rolname IN ('acl_owner','acl_migrator','acl_reader','acl_outsider')
) AS roles_free \gset
\if :roles_free
\else
  \echo 'refusing pre-existing fixture role names'
  \quit 43
\endif

SELECT NOT EXISTS (
  SELECT 1 FROM pg_database
  WHERE datname IN (
    'acl_lab_d01','acl_lab_d02','acl_lab_d03','acl_lab_d04','acl_lab_d05',
    'acl_lab_d06','acl_lab_d07','acl_lab_d08','acl_lab_d09','acl_lab_d10',
    'acl_lab_d11')
) AS dbs_free \gset
\if :dbs_free
\else
  \echo 'refusing pre-existing fixture database names'
  \quit 44
\endif

CREATE ROLE acl_owner NOLOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE
  NOREPLICATION NOBYPASSRLS;
CREATE ROLE acl_migrator LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE
  NOREPLICATION NOBYPASSRLS;
CREATE ROLE acl_reader LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE
  NOREPLICATION NOBYPASSRLS;
CREATE ROLE acl_outsider LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE
  NOREPLICATION NOBYPASSRLS;

GRANT acl_owner TO acl_migrator
  WITH INHERIT TRUE, SET TRUE, ADMIN FALSE;

CREATE DATABASE acl_lab_d01 TEMPLATE template0 OWNER acl_owner;
CREATE DATABASE acl_lab_d02 TEMPLATE template0 OWNER acl_owner;
CREATE DATABASE acl_lab_d03 TEMPLATE template0 OWNER acl_owner;
CREATE DATABASE acl_lab_d04 TEMPLATE template0 OWNER acl_owner;
CREATE DATABASE acl_lab_d05 TEMPLATE template0 OWNER acl_owner;
CREATE DATABASE acl_lab_d06 TEMPLATE template0 OWNER acl_owner;
CREATE DATABASE acl_lab_d07 TEMPLATE template0 OWNER acl_owner;
CREATE DATABASE acl_lab_d08 TEMPLATE template0 OWNER acl_owner;
CREATE DATABASE acl_lab_d09 TEMPLATE template0 OWNER acl_owner;
CREATE DATABASE acl_lab_d10 TEMPLATE template0 OWNER acl_owner;
CREATE DATABASE acl_lab_d11 TEMPLATE template0 OWNER acl_owner;

REVOKE CONNECT, TEMPORARY ON DATABASE
  acl_lab_d01,acl_lab_d02,acl_lab_d03,acl_lab_d04,acl_lab_d05,
  acl_lab_d06,acl_lab_d07,acl_lab_d08,acl_lab_d09,acl_lab_d10,acl_lab_d11
FROM PUBLIC;

GRANT CONNECT ON DATABASE
  acl_lab_d01,acl_lab_d02,acl_lab_d03,acl_lab_d04,acl_lab_d05,
  acl_lab_d06,acl_lab_d07,acl_lab_d08,acl_lab_d09,acl_lab_d10,acl_lab_d11
TO acl_migrator,acl_reader,acl_outsider;

Then initialize each database through the bootstrap service:

-- 01_init_case.sql
-- executing identity: ACL_BOOTSTRAP_SERVICE
-- database: one acl_lab_dNN supplied by runner
\set ON_ERROR_STOP on
BEGIN;
CREATE SCHEMA acl_lab AUTHORIZATION acl_owner;
CREATE SCHEMA acl_other AUTHORIZATION acl_owner;
REVOKE ALL ON SCHEMA acl_lab, acl_other FROM PUBLIC;
GRANT USAGE ON SCHEMA acl_lab, acl_other TO acl_reader, acl_outsider;
COMMIT;

The runner loops over the eleven known names, never an arbitrary database discovered from the server:

for n in $(seq -w 1 11); do
  db="acl_lab_d${n}"
  psql -X "service=$ACL_BOOTSTRAP_SERVICE dbname=$db" \
    -v ON_ERROR_STOP=on -v VERBOSITY=verbose \
    -f 01_init_case.sql \
    >"evidence/${db}.init.stdout" \
    2>"evidence/${db}.init.stderr"
  printf '%s\n' "$?" >"evidence/${db}.init.status"
done

Author expectations before reading the catalogs

The independent summary ledger is fixed before runtime evidence:

ID

Authored state and intervention

Independent expected assertions: all EXPECTED / NOT EXECUTED

D01

No altered defaults; migrator activates acl_owner, creates t_d01.

creator/owner acl_owner; reader SELECT false and 42501; outsider false; NULL relacl compatible with hard-wired owner rights.

D02

Owner creates t_old; configure future owner/schema SELECT; owner creates t_new.

both owned by owner; old reader false; new true and returns marker. Template edit did not repair old.

D03

Owner schema template grants SELECT; migrator creates without SET ROLE.

creator/owner acl_migrator; reader false despite owner template.

D04

Same policy; real migrator login executes SET ROLE acl_owner.

session migrator, current creator/owner owner; reader SELECT true, INSERT false; outsider false.

D05

No defaults; migrator omits FOR ROLE when granting future SELECT.

template belongs migrator; t_m migrator-owned/readable; t_o owner-owned/denied.

D06

Owner template grants SELECT; migrator creates denied table; transfer to owner; explicit object grant.

OID preserved; transfer alone remains denied; named-table GRANT enables marker read.

D07

No default; owner creates t_before; grant SELECT on all existing tables; create t_after.

before readable; after denied. Existing-object grant created no future template.

D08

Global owner default SELECT; schema-local REVOKE; owner tables in both schemas.

both readable; global grant persists; local REVOKE is not DENY.

D09

Owner schema default SELECT; create before; revoke default; create after; revoke old object SELECT.

after template revoke: old true/new false; after object revoke: old false with same OID.

D10

Correct owner/table SELECT but reader schema USAGE removed.

table privilege true; schema privilege false; fresh SELECT fails 42501 at schema layer; restoring USAGE and reconnecting yields marker.

D11

Global default creates old tables; remove global; add acl_lab default; repair old_other; create new pair.

owner all four; reader SELECT T/F/T/F for old_lab/old_other/new_lab/new_other; all reader INSERT false; all outsider SELECT false; no global reader default; old_other OID preserved.

The checker must not derive expectations from relacl. It receives authored keys such as case, subphase, login, operation, schema, table, expected Boolean, expected marker and expected SQLSTATE. It rejects missing or duplicate observations before comparing values.

Establish what a table without a custom template permits

D01 establishes the hard-wired baseline.

-- D01_create.sql
-- executing identity: acl_migrator; database: acl_lab_d01
\set ON_ERROR_STOP on
BEGIN;
SELECT session_user, current_user;          -- migrator/migrator
SET ROLE acl_owner;
SELECT session_user, current_user;          -- migrator/owner
CREATE TABLE acl_lab.t_d01 (
  id integer PRIMARY KEY,
  marker text NOT NULL
);
INSERT INTO acl_lab.t_d01 VALUES (1,'case-table-marker');
SELECT session_user, current_user;          -- preserve CREATE identity
COMMIT;

PostgreSQL says object ownership normally follows the role executing the creation statement. With no ALTER DEFAULT PRIVILEGES, ordinary tables have no default PUBLIC table privileges, while the owner has the hard-wired owner capabilities.

The observer query used everywhere is:

-- metadata.sql
-- executing identity: administrative metadata connection
SELECT current_database(), session_user, current_user,
       inet_server_addr(), inet_server_port(),
       current_setting('server_version_num');

SELECT c.oid AS relation_oid,
       n.oid AS namespace_oid,
       n.nspname,
       c.relname,
       c.relowner,
       owner.rolname AS owner_name,
       c.relacl,
       c.relacl IS NULL AS relacl_is_null,
       has_table_privilege(reader.oid,c.oid,'SELECT') AS reader_select,
       has_table_privilege(reader.oid,c.oid,'INSERT') AS reader_insert,
       has_schema_privilege(reader.oid,n.oid,'USAGE') AS reader_schema_usage
FROM pg_class c
JOIN pg_namespace n ON n.oid=c.relnamespace
JOIN pg_roles owner ON owner.oid=c.relowner
JOIN pg_roles reader ON reader.rolname='acl_reader'
WHERE n.nspname IN ('acl_lab','acl_other')
  AND c.relkind='r'
ORDER BY n.nspname,c.relname;

SELECT c.oid AS relation_oid,
       x.grantor,
       CASE WHEN x.grantee=0 THEN 'PUBLIC'
            ELSE grantee.rolname END AS grantee,
       x.privilege_type,
       x.is_grantable
FROM pg_class c
CROSS JOIN LATERAL aclexplode(
  COALESCE(c.relacl, acldefault('r',c.relowner))
) x
LEFT JOIN pg_roles grantee ON grantee.oid=x.grantee
JOIN pg_namespace n ON n.oid=c.relnamespace
WHERE n.nspname IN ('acl_lab','acl_other')
  AND c.relkind='r'
ORDER BY c.oid,grantee,x.privilege_type;

SELECT d.defaclrole,
       r.rolname AS creator_role,
       d.defaclnamespace,
       CASE WHEN d.defaclnamespace=0 THEN '<GLOBAL>'
            ELSE n.nspname END AS template_scope,
       d.defaclobjtype,
       x.grantee,
       CASE WHEN x.grantee=0 THEN 'PUBLIC'
            ELSE g.rolname END AS grantee_name,
       x.privilege_type,
       x.is_grantable
FROM pg_default_acl d
JOIN pg_roles r ON r.oid=d.defaclrole
LEFT JOIN pg_namespace n ON n.oid=d.defaclnamespace
CROSS JOIN LATERAL aclexplode(d.defaclacl) x
LEFT JOIN pg_roles g ON g.oid=x.grantee
WHERE d.defaclobjtype='r'
ORDER BY creator_role,template_scope,grantee_name,x.privilege_type;

pg_class supplies relation OID, namespace, owner and relacl; pg_default_acl separately identifies creator, schema/global scope and object class. PostgreSQL warns that a NULL existing-object ACL means hard-wired defaults; it does not dynamically point to today’s pg_default_acl. acldefault('r', relowner) is therefore used only to normalize NULL object ACLs to their hard-wired effective form, while aclexplode exposes one privilege per row.

Never test has_table_privilege(...,'SELECT,INSERT') when you need both assertions. PostgreSQL’s privilege inquiry accepts a comma-separated list as an “any of these privileges” test; the fixture asks SELECT and INSERT separately.

A reader denial is diagnostic success only when the intended SELECT acl_lab.t_d01 fails as acl_reader with SQLSTATE 42501, which PostgreSQL 18.7 names insufficient_privilege. It remains an unacceptable application state.

Compare inherited CREATE authority with explicit SET ROLE

D03 and D04 isolate the operational scenario: unchanged CREATE TABLE text can materialize different permissions when the effective creator changes.

Both databases first configure the owner’s future template:

-- D03/D04 template phase
-- executing identity: acl_migrator; database: respective case database
ALTER DEFAULT PRIVILEGES FOR ROLE acl_owner IN SCHEMA acl_lab
  GRANT SELECT ON TABLES TO acl_reader;
COMMIT;

D03 does not activate the owner:

-- D03_create.sql -- acl_migrator; acl_lab_d03
SELECT session_user,current_user;
CREATE TABLE acl_lab.t_d03 (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_lab.t_d03 VALUES (1,'case-table-marker');
COMMIT;

Because acl_migrator inherits the owner role’s schema authority, it can create in the schema; inheritance does not cause the owner’s pg_default_acl template to be selected. PostgreSQL expressly says creation-time default privileges come only from the current role and are not inherited from roles of which it is a member. Expected result: owner acl_migrator, reader SELECT false.

D04 changes only the effective creator:

-- D04_create.sql -- acl_migrator; acl_lab_d04
SELECT session_user,current_user;
SET ROLE acl_owner;
SELECT session_user,current_user;
CREATE TABLE acl_lab.t_d04 (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_lab.t_d04 VALUES (1,'case-table-marker');
COMMIT;

Expected: session_user=acl_migrator, current_user=acl_owner at creation, owner acl_owner, reader SELECT true, reader INSERT false, outsider SELECT false. SET ROLE is not “safer” in the abstract; it is simply the mechanism required by this migration contract. Broader database integration and migration foundations remain useful context, but this acceptance gate needs database evidence rather than assumptions about ORM or migration-tool identity.

Record the effective creator at the CREATE statement

Current ownership cannot prove historical creation identity. A later ALTER TABLE ... OWNER TO can change relowner, so the migration record should preserve session_user and current_user immediately around CREATE TABLE.

That distinction follows directly from PostgreSQL’s membership model: inherited privileges can let a member use the owning role’s authority, whereas explicit role activation makes created objects belong to the activated role.

Treat role activation as migration configuration. If the approved path requires SET ROLE acl_owner, then absence of that activation is a migration defect even when CREATE TABLE succeeds.

Target the intended creator when editing defaults

D05 exposes the common omitted-FOR ROLE mistake.

-- D05.sql -- executing identity: acl_migrator; database: acl_lab_d05
ALTER DEFAULT PRIVILEGES IN SCHEMA acl_lab
  GRANT SELECT ON TABLES TO acl_reader;

CREATE TABLE acl_lab.t_m (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_lab.t_m VALUES (1,'case-table-marker');

SET ROLE acl_owner;
CREATE TABLE acl_lab.t_o (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_lab.t_o VALUES (1,'case-table-marker');
COMMIT;

When FOR ROLE is omitted, PostgreSQL changes defaults for the current role. FOR ROLE target_role changes the target role’s template; it does not itself switch current_user to that role.

Before creating either table, the metadata query must show defaclrole = acl_migrator, defaclnamespace = acl_lab’s OID and defaclobjtype = 'r'. pg_default_acl documents r as the relation class and namespace zero as global scope.

Expected results are intentionally asymmetric: t_m is owned by acl_migrator and readable by the reader; t_o is owned by acl_owner and denied. The readable table is still wrong for the declared ownership contract. “Reader can query it” is therefore insufficient acceptance evidence.

The narrow correction for future owner-created tables is:

ALTER DEFAULT PRIVILEGES FOR ROLE acl_owner IN SCHEMA acl_lab
  GRANT SELECT ON TABLES TO acl_reader;

That command repairs the future template only. It does not repair t_o, nor does it ensure that the next migration will create as acl_owner.

Change ownership without assuming default privileges replay

D06 derives a critical non-replay prediction.

-- D06 phase A -- acl_migrator; acl_lab_d06
ALTER DEFAULT PRIVILEGES FOR ROLE acl_owner IN SCHEMA acl_lab
  GRANT SELECT ON TABLES TO acl_reader;

CREATE TABLE acl_lab.t_d06 (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_lab.t_d06 VALUES (1,'case-table-marker');
COMMIT;

Expected phase A: the table is migrator-owned and reader-denied because the migrator, not acl_owner, created it.

Capture its OID, owner, raw relacl, normalized ACL, has_table_privilege and marker state. Then transfer ownership:

-- D06 phase B -- acl_migrator; acl_lab_d06
ALTER TABLE acl_lab.t_d06 OWNER TO acl_owner;
COMMIT;

Expected phase B: the OID is unchanged, the owner becomes acl_owner, but reader SELECT remains false. This is a derived expectation, not an observed result: PostgreSQL says pg_default_acl is consulted only during object creation, while the privileges documentation separately defines ownership transfer. There is no documented step that re-runs default-ACL selection on ALTER ... OWNER.

Finally repair the existing object:

-- D06 phase C -- acl_migrator; acl_lab_d06
SET ROLE acl_owner;
GRANT SELECT ON TABLE acl_lab.t_d06 TO acl_reader;
COMMIT;

Expected phase C: same OID and owner, now reader SELECT true with the literal row.

For ordinary non-superuser ownership transfer, PostgreSQL requires the caller to own the object or inherit the current owner’s rights and to be able to SET ROLE to the new owner. This fixture already gives the migrator exactly that path; superuser is unnecessary for the migration-side repair.

Repair existing grants and future defaults separately

D02 and D07 form opposite controls.

D02 first creates an object and only then changes the future template:

-- D02 -- acl_migrator; acl_lab_d02
SET ROLE acl_owner;
CREATE TABLE acl_lab.t_old (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_lab.t_old VALUES (1,'case-table-marker');
RESET ROLE;
COMMIT;

ALTER DEFAULT PRIVILEGES FOR ROLE acl_owner IN SCHEMA acl_lab
  GRANT SELECT ON TABLES TO acl_reader;
COMMIT;

SET ROLE acl_owner;
CREATE TABLE acl_lab.t_new (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_lab.t_new VALUES (1,'case-table-marker');
COMMIT;

Expected: both owners are acl_owner; t_old remains denied while t_new is readable. PostgreSQL explicitly states that ALTER DEFAULT PRIVILEGES has no effect on already existing objects.

D07 does the inverse:

-- D07 -- acl_migrator; acl_lab_d07
SET ROLE acl_owner;
CREATE TABLE acl_lab.t_before (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_lab.t_before VALUES (1,'case-table-marker');
COMMIT;

GRANT SELECT ON ALL TABLES IN SCHEMA acl_lab TO acl_reader;
COMMIT;

CREATE TABLE acl_lab.t_after (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_lab.t_after VALUES (1,'case-table-marker');
COMMIT;

GRANT ... ON ALL TABLES IN SCHEMA grants on tables that exist at that point; it is not ALTER DEFAULT PRIVILEGES and does not establish a creation template. PostgreSQL documents the existing-object and role-grant variants separately. Expected: t_before readable, t_after denied.

Use an explicit object inventory for historical repair

The production analogue should not begin with an unbounded GRANT ON ALL TABLES merely because D07 uses one inside an owned isolated schema. Build a reviewed inventory first:

SELECT c.oid, n.nspname, c.relname, r.rolname AS owner
FROM pg_class c
JOIN pg_namespace n ON n.oid=c.relnamespace
JOIN pg_roles r ON r.oid=c.relowner
WHERE n.nspname='acl_lab'
  AND c.relkind='r'
ORDER BY c.oid;

For a known object, repair only the intended operation:

SET ROLE acl_owner;
GRANT SELECT ON TABLE acl_lab.known_table TO acl_reader;

Then configure the future template separately:

ALTER DEFAULT PRIVILEGES FOR ROLE acl_owner IN SCHEMA acl_lab
  GRANT SELECT ON TABLES TO acl_reader;

Create a new sentinel under the intended creator and probe it. Fixing historical tables says nothing about whether the next migration will use the correct effective role.

Test global defaults against a schema-specific revoke

D08 tests an easy-to-misread property of PostgreSQL default privileges.

-- D08 -- acl_migrator; acl_lab_d08
ALTER DEFAULT PRIVILEGES FOR ROLE acl_owner
  GRANT SELECT ON TABLES TO acl_reader;

ALTER DEFAULT PRIVILEGES FOR ROLE acl_owner IN SCHEMA acl_lab
  REVOKE SELECT ON TABLES FROM acl_reader;

SET ROLE acl_owner;
CREATE TABLE acl_lab.t_lab (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_lab.t_lab VALUES (1,'case-table-marker');

CREATE TABLE acl_other.t_other (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_other.t_other VALUES (1,'case-table-marker');
COMMIT;

The expected template evidence contains the owner’s global relation grant. PostgreSQL states that per-schema default privileges are additive to the global defaults. A schema-level REVOKE can reverse a matching schema-level addition, but it cannot subtract a privilege granted globally.

Expected executable result: both tables are reader-readable.

“Global” here means all relevant new objects created by this creator in the current database, subject to object type; it does not mean every database in the cluster or defaults for every creator role. pg_default_acl.defaclnamespace = 0 is the catalog representation of that database-local global scope.

A reviewer should therefore reject the mental model “schema REVOKE overrides global GRANT.” PostgreSQL default ACLs do not implement deny entries.

Withdraw future access without forgetting existing objects

D09 proves that withdrawing a future policy and withdrawing existing access are distinct changes.

-- D09 phase A -- acl_migrator; acl_lab_d09
ALTER DEFAULT PRIVILEGES FOR ROLE acl_owner IN SCHEMA acl_lab
  GRANT SELECT ON TABLES TO acl_reader;

SET ROLE acl_owner;
CREATE TABLE acl_lab.t_before (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_lab.t_before VALUES (1,'case-table-marker');
RESET ROLE;
COMMIT;

ALTER DEFAULT PRIVILEGES FOR ROLE acl_owner IN SCHEMA acl_lab
  REVOKE SELECT ON TABLES FROM acl_reader;
COMMIT;

SET ROLE acl_owner;
CREATE TABLE acl_lab.t_after (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_lab.t_after VALUES (1,'case-table-marker');
COMMIT;

Expected after the template revocation: t_before remains readable; t_after is denied. Again, creation-time template consultation is the controlling documented behavior.

Now record the original t_before OID and repair that existing object:

-- D09 phase B -- acl_migrator; acl_lab_d09
SET ROLE acl_owner;
REVOKE SELECT ON TABLE acl_lab.t_before FROM acl_reader;
COMMIT;

Expected: t_before becomes denied while preserving its OID. That object-level REVOKE is independent of the earlier default-privilege change. GRANT and REVOKE operate additively across direct grants, memberships and PUBLIC, so reviewers should also inspect unexpected privilege paths instead of assuming one revoked ACL item eliminates every route.

Nothing about this fixture establishes whether unauthorized reads occurred before a correction. It qualifies present object state and the selected future policy only.

Separate schema lookup permission from table SELECT

D10 creates a correctly owned, correctly granted table, then breaks only schema access.

-- D10 phase A -- acl_migrator; acl_lab_d10
ALTER DEFAULT PRIVILEGES FOR ROLE acl_owner IN SCHEMA acl_lab
  GRANT SELECT ON TABLES TO acl_reader;
SET ROLE acl_owner;
CREATE TABLE acl_lab.t_d10 (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_lab.t_d10 VALUES (1,'case-table-marker');
COMMIT;

-- phase B
REVOKE USAGE ON SCHEMA acl_lab FROM acl_reader;
COMMIT;

The observer should now report:

has_table_privilege(reader_oid, table_oid, 'SELECT') = true
has_schema_privilege(reader_oid, schema_oid, 'USAGE') = false

PostgreSQL defines schema USAGE as permission to look up contained objects, assuming their own privilege requirements are also satisfied. Thus a table-level SELECT grant can be correct while a qualified query still fails at the schema boundary.

Do not “repair” the default table template in this case. The table ACL is already correct. Restore the missing layer:

-- D10 phase C -- acl_migrator after SET ROLE acl_owner; acl_lab_d10
GRANT USAGE ON SCHEMA acl_lab TO acl_reader;
COMMIT;

Expected: the same table OID and table ACL remain; after reconnect, the reader gets (1,'case-table-marker').

Reconnect the reader for the schema-USAGE control

PostgreSQL warns that revoking schema USAGE is not a complete barrier for an already-running session that has previously performed the relevant object lookup. This control therefore uses a new reader connection after revocation and another new connection after restoration.

The generic negative SELECT probe is:

-- deny_select.sql
-- executing identity: actual reader or outsider service
\set ON_ERROR_STOP on
\set VERBOSITY verbose
SELECT session_user,current_user;
SELECT current_database();
SELECT id,marker FROM acl_lab.t_d10 ORDER BY id;

The wrapper captures its process evidence:

set +e
psql -X "service=$ACL_READER_SERVICE dbname=acl_lab_d10" \
  -v ON_ERROR_STOP=on -v VERBOSITY=verbose \
  -f deny_select.sql \
  >evidence/D10.pre_usage.reader_select.stdout \
  2>evidence/D10.pre_usage.reader_select.stderr
rc=$?
set -e
printf '%s\n' "$rc" >evidence/D10.pre_usage.reader_select.status

test "$rc" -eq 3 || exit 60
grep -q '42501' evidence/D10.pre_usage.reader_select.stderr || exit 61

A connection failure, missing table, wrong identity, timeout, wrong SQLSTATE, or an error at a prerequisite statement is not evidence for the intended control. It is HOLD or RERUN.

Qualify a scoped repair with old and new tables

D11 combines historical repair with future-policy repair across both schemas.

-- D11 phase A -- acl_migrator; acl_lab_d11
ALTER DEFAULT PRIVILEGES FOR ROLE acl_owner
  GRANT SELECT ON TABLES TO acl_reader;

SET ROLE acl_owner;
CREATE TABLE acl_lab.old_lab (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_lab.old_lab VALUES (1,'case-table-marker');
CREATE TABLE acl_other.old_other (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_other.old_other VALUES (1,'case-table-marker');
RESET ROLE;
COMMIT;

Preserve both OIDs. Then correct the future scope and historical unwanted grant:

-- D11 phase B -- acl_migrator; acl_lab_d11
ALTER DEFAULT PRIVILEGES FOR ROLE acl_owner
  REVOKE SELECT ON TABLES FROM acl_reader;

ALTER DEFAULT PRIVILEGES FOR ROLE acl_owner IN SCHEMA acl_lab
  GRANT SELECT ON TABLES TO acl_reader;

SET ROLE acl_owner;
REVOKE SELECT ON TABLE acl_other.old_other FROM acl_reader;
COMMIT;

Finally create new sentinels:

-- D11 phase C -- acl_migrator; acl_lab_d11
SET ROLE acl_owner;
CREATE TABLE acl_lab.new_lab (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_lab.new_lab VALUES (1,'case-table-marker');
CREATE TABLE acl_other.new_other (id integer PRIMARY KEY, marker text NOT NULL);
INSERT INTO acl_other.new_other VALUES (1,'case-table-marker');
COMMIT;

The required twelve-cell operation matrix is:

Login / operation

old_lab

old_other

new_lab

new_other

reader SELECT

true, marker

false, 42501

true, marker

false, 42501

reader INSERT

false, 42501

false, 42501

false, 42501

false, 42501

outsider SELECT

false, 42501

false, 42501

false, 42501

false, 42501

All four current owners are expected to be acl_owner. old_other must retain its captured relation OID through the object-level repair. The post-repair pg_default_acl inventory must contain the acl_lab relation grant for acl_owner and no global reader SELECT grant.

For every denied INSERT, use a transaction so unexpected success cannot leave a test row:

-- deny_insert.sql -- executing identity: actual acl_reader login
\set ON_ERROR_STOP on
\set VERBOSITY verbose
BEGIN;
SELECT session_user,current_user;
INSERT INTO acl_lab.old_lab VALUES (999999,'unexpected-probe');
ROLLBACK;

If the INSERT fails as expected, ON_ERROR_STOP terminates psql and connection shutdown rolls back the open transaction. If it unexpectedly succeeds, the explicit ROLLBACK runs and the wrapper records an unexpected success rather than committing the row. The psql stopped-script exit status is expected to be 3 for the intended error path.

This kind of bounded permission gate belongs naturally in repeatable database administration automation, but automation must preserve evidence rather than merely emit a green build status.

Keep template evidence separate from executable access

The evidence bundle must retain, per invocation: script identity and target database; exact SQL/configuration; PostgreSQL and psql versions; endpoint; session_user and current_user; role attributes and pg_auth_members flags; pg_default_acl; raw and normalized relacl; schema and table privilege inquiries; relation/schema OIDs; stdout; stderr; process status; SQLSTATE; permitted row results; and cleanup status.

The membership check is:

-- administrative metadata connection; postgres
SELECT roleid::regrole AS granted_role,
       member::regrole AS member_role,
       admin_option,inherit_option,set_option
FROM pg_auth_members
WHERE roleid=(SELECT oid FROM pg_roles WHERE rolname='acl_owner')
   OR member IN (
      SELECT oid FROM pg_roles
      WHERE rolname IN ('acl_migrator','acl_reader','acl_outsider'))
ORDER BY roleid,member;

The only expected fixture membership row is:

acl_owner | acl_migrator | admin=false | inherit=true | set=true

pg_auth_members is the authoritative catalog for those flags.

A template catalog row does not prove that a migration used that template. A true administrative has_table_privilege call does not replace execution through the real reader. Likewise, observer inquiries should use the already resolved role and object OIDs so the metadata connection is measuring known entities rather than accidentally resolving a similarly named object.

The probe manifest should be explicit and checker-driven. A compact independently authored machine roster can be stored as:

D01 final reader SELECT acl_lab t_d01 DENY
D01 final outsider SELECT acl_lab t_d01 DENY
D02 final reader SELECT acl_lab t_old DENY
D02 final reader SELECT acl_lab t_new ALLOW_MARKER
D03 final reader SELECT acl_lab t_d03 DENY
D04 final reader SELECT acl_lab t_d04 ALLOW_MARKER
D04 final reader INSERT acl_lab t_d04 DENY
D04 final outsider SELECT acl_lab t_d04 DENY
D05 final reader SELECT acl_lab t_m ALLOW_MARKER
D05 final reader SELECT acl_lab t_o DENY
D06 pre_transfer reader SELECT acl_lab t_d06 DENY
D06 post_transfer reader SELECT acl_lab t_d06 DENY
D06 post_grant reader SELECT acl_lab t_d06 ALLOW_MARKER
D07 final reader SELECT acl_lab t_before ALLOW_MARKER
D07 final reader SELECT acl_lab t_after DENY
D08 final reader SELECT acl_lab t_lab ALLOW_MARKER
D08 final reader SELECT acl_other t_other ALLOW_MARKER
D09 post_template_revoke reader SELECT acl_lab t_before ALLOW_MARKER
D09 post_template_revoke reader SELECT acl_lab t_after DENY
D09 post_object_revoke reader SELECT acl_lab t_before DENY
D10 no_usage reader SELECT acl_lab t_d10 DENY
D10 restored_usage reader SELECT acl_lab t_d10 ALLOW_MARKER
D11 final reader SELECT acl_lab old_lab ALLOW_MARKER
D11 final reader SELECT acl_other old_other DENY
D11 final reader SELECT acl_lab new_lab ALLOW_MARKER
D11 final reader SELECT acl_other new_other DENY
D11 final reader INSERT acl_lab old_lab DENY
D11 final reader INSERT acl_other old_other DENY
D11 final reader INSERT acl_lab new_lab DENY
D11 final reader INSERT acl_other new_other DENY
D11 final outsider SELECT acl_lab old_lab DENY
D11 final outsider SELECT acl_other old_other DENY
D11 final outsider SELECT acl_lab new_lab DENY
D11 final outsider SELECT acl_other new_other DENY

A checker should treat the first six fields as the composite key, reject duplicate keys, compare the observed roster to the expected roster as sets, require permitted SELECT output to be exactly 1|case-table-marker, require denied probes to have verified identity plus exit 3 plus SQLSTATE 42501, and fail on any missing case/subphase/probe. That reconciles the complete roster rather than merely counting eleven case labels.

A digest belongs in the evidence record only if the runner genuinely computes one. Nothing in this unexecuted specification claims a digest exists.

Decide what to accept and when to requalify

Case correctness and policy acceptance are different. D03 can perfectly reproduce its expected bad state and still be unacceptable for the final migration policy.

Decision

Use when

Required action

ACCEPT

D11’s declared final policy matches in full: creator path, owner, templates, object ACLs, schema access, all twelve operation cells, versions and evidence complete.

Preserve evidence and close this bounded permission qualification.

REPAIR

Defect is deterministic and understood: wrong effective creator, wrong FOR ROLE/schema template, incorrect existing-table ACL, wrong owner, or missing schema USAGE.

Apply the narrow repair to the defective layer, then rerun affected and dependent probes.

HOLD

Unexpected grant path, unexpected successful write/read, undeclared membership, incomplete evidence, mismatched OID continuity, or unexplained catalog/runtime disagreement.

Do not approve; investigate before modifying expectations.

RERUN

Environment made the result inconclusive: connection failure, timeout, wrong profile, missing object, setup failure, baseline mismatch, probe omission.

Diagnose the environment and rerun from a controlled state.

The operational ownership is straightforward. The migration maintainer owns the effective-login and role-activation configuration. The data/security owner defines who should read or write. The DBA reviews membership, creator templates, object ownership, actual ACLs, schema privileges, repair scope and retained evidence.

Requalification is appropriate when the migration credentials or authentication path change; the migration starts or stops using SET ROLE; membership flags change; owner or schema changes; the object class changes; PostgreSQL runtime changes from the qualified 18.7 baseline; or the intended grant policy changes. A wider version qualification is a separate concern; Refonte’s broader PostgreSQL workload-replay review covers a materially different problem and its dated beta-era wording should not be reused as present release-status evidence.

The lab’s sequential shell wrapper should preserve each invocation before cleanup:

mkdir -p evidence
# For each invocation:
# psql -X ... > evidence/<case>.<phase>.<probe>.stdout \
#              2> evidence/<case>.<phase>.<probe>.stderr
# printf '%s\n' "$?" > evidence/<case>.<phase>.<probe>.status
#
# Only after all comparison output and manifest files are outside the lab DBs:
tar -cf evidence.tar evidence/

Cleanup must be manifest-scoped:

-- 99_cleanup.sql
-- executing identity: ACL_BOOTSTRAP_SERVICE; database: postgres
\set ON_ERROR_STOP on

DROP DATABASE acl_lab_d01;
DROP DATABASE acl_lab_d02;
DROP DATABASE acl_lab_d03;
DROP DATABASE acl_lab_d04;
DROP DATABASE acl_lab_d05;
DROP DATABASE acl_lab_d06;
DROP DATABASE acl_lab_d07;
DROP DATABASE acl_lab_d08;
DROP DATABASE acl_lab_d09;
DROP DATABASE acl_lab_d10;
DROP DATABASE acl_lab_d11;

REVOKE acl_owner FROM acl_migrator;

DROP ROLE acl_outsider;
DROP ROLE acl_reader;
DROP ROLE acl_migrator;
DROP ROLE acl_owner;

Run that cleanup only after confirming the names are the fixture manifest created by this run. Never generalize it into pattern-based removal, and never remove a pre-existing database or unrelated role. PostgreSQL grants and ownership changes that were already committed are not undone merely because a later qualification step fails; they need explicit corrective SQL. Preserved evidence is therefore part of the control, not an optional artifact.

The complete case runner is sequential by design: bootstrap roles/databases; initialize schemas; verify real login profiles; execute D01 through D11 phases with commits before probes; open fresh reader/outsider connections; record metadata and operation evidence; compare against the authored roster; preserve evidence externally; then perform manifest-scoped cleanup. psql -X, ON_ERROR_STOP, fresh sessions and process-status capture are all deliberate because PostgreSQL and psql document different meanings for SQL privilege failure, connection failure and script termination.

The scope remains narrow. It does not qualify RLS, tenant filtering, sequences or identity columns, routines, views, column-level grants, SECURITY DEFINER, pooling, concurrent DDL, managed-service limitations, search_path security, major-version upgrades, authentication strength, historical exposure, or production rollout safety. It answers one operational question: which creator-specific PostgreSQL default privileges became materialized on these ordinary tables, and which existing-object or schema permissions require separate repair?

Strengthen database access skills with Refonte Learning’s Database Administrator Essentials

Refonte Learning’s Database Administrator Essentials program currently describes a three-month program requiring about 12–14 hours per week, with basic programming knowledge recommended and admission requiring the learner to be working toward a bachelor’s or higher-level degree. Its published competencies include database architecture, SQL optimization, security, role-based access control, migration and integration, recovery, and monitoring; its learning path also names advanced SQL and cloud administration involving AWS RDS, Azure SQL and Google Cloud Spanner. The page presents educational mentoring, a potential internship, and Training and Internship certificates upon successful completion.

The discipline in this lab (separating declared policy from catalog state, runtime identity and executable evidence) is a practical extension of those database-operations foundations. PostgreSQL-specific ALTER DEFAULT PRIVILEGES or pg_default_acl labs, this exact workflow, PostgreSQL/psql 18.7, guaranteed internship placement, and any particular mentoring frequency require confirmation with the program and are not claimed program content. Readers who want a structured foundation in SQL, RBAC, migration and monitoring can review the program page without treating this specialized PostgreSQL qualification as a promised syllabus module.