What this thread does
Use a non-owner runtime role with narrow grants, then enable RLS for both the rows it can reach and the rows it can write.
Do first: run this through the actual app connection, replacing app.documents with a tenant-owned table, to choose the ownership, grant or policy branch:
SELECT current_setting('server_version') AS version,
session_user, current_user, r.rolsuper, r.rolbypassrls,
c.relowner::regrole AS table_owner,
c.relrowsecurity, c.relforcerowsecurity
FROM pg_roles r CROSS JOIN pg_class c
WHERE r.rolname = current_user
AND c.oid = 'app.documents'::regclass;
As of November 2026, these branches use PostgreSQL 18 documentation. The role view and table catalog expose these flags. Examples assume existing roles app_owner and app_runtime and table app.documents(tenant_id uuid, id bigint, body text). Apply DDL as an authorized administrator/owner; reconnect as runtime for confirmation.
1. Runtime owns tables, is privileged, or can become the owner → separate roles
Follow role attributes, ownership changes, and role membership:
ALTER ROLE app_owner NOLOGIN;
ALTER ROLE app_runtime NOSUPERUSER NOBYPASSRLS
NOCREATEDB NOCREATEROLE NOREPLICATION;
ALTER TABLE app.documents OWNER TO app_owner;
REVOKE app_owner FROM app_runtime;
Ensure a separate migration identity can administer app_owner objects. Repeat for other app objects. Remove indirect paths through privilege groups too; NOINHERIT alone does not prevent an allowed SET ROLE.
Confirmation: rerun the opening query after reconnecting. Runtime should be a different role from the owner, with both bypass flags false. Inspect membership using the privilege inquiry functions:
SELECT pg_has_role('app_runtime', 'app_owner', 'MEMBER'),
pg_has_role('app_runtime', 'app_owner', 'SET');
Both should be false for runtime. Inspect other privileged memberships.
2. Runtime has broad access → rebuild grants around actual operations
Use GRANT, REVOKE, and schema privileges:
REVOKE CREATE ON SCHEMA app FROM PUBLIC, app_runtime;
REVOKE ALL ON TABLE app.documents FROM PUBLIC, app_runtime;
GRANT USAGE ON SCHEMA app TO app_runtime;
GRANT SELECT, INSERT, UPDATE, DELETE ON app.documents TO app_runtime;
Omit unnecessary operations. If inserts call a sequence, grant USAGE on that specific sequence. Inspect inherited grants and reachable functions/views: a direct revoke cannot cancel inherited access.
Confirmation: as runtime, exercise intended CRUD on disposable rows; creating a table in app should fail. Inspect effective privileges:
SELECT has_schema_privilege(current_user, 'app', 'CREATE'),
has_table_privilege(current_user, 'app.documents', 'TRUNCATE');
Both should be false. RLS does not filter TRUNCATE, so omitting that grant matters.
3. Table grants are correct but tenants can cross rows → enable a read/write policy
Use CREATE POLICY and RLS semantics:
ALTER TABLE app.documents ENABLE ROW LEVEL SECURITY;
ALTER TABLE app.documents FORCE ROW LEVEL SECURITY;
CREATE POLICY tenant_documents ON app.documents TO app_runtime
USING (tenant_id = nullif(current_setting('app.current_tenant', true), '')::uuid)
WITH CHECK (tenant_id = nullif(current_setting('app.current_tenant', true), '')::uuid);
USING controls existing rows; WITH CHECK controls proposed inserted or updated values. Inspect pg_policies before adding it: permissive policies combine with OR, so an existing broad policy may still allow cross-tenant rows.
SELECT policyname, permissive, roles, cmd, qual, with_check
FROM pg_policies
WHERE schemaname = 'app' AND tablename = 'documents';
Remove or narrow unintended overlapping policies in the same migration. FORCE subjects the owner to policies; it does not defeat superuser or BYPASSRLS, and ownership still permits changing policies.
Confirmation: seed two tenants before enabling/forcing RLS, then connect as runtime:
BEGIN;
SET LOCAL app.current_tenant = '00000000-0000-0000-0000-000000000001';
SELECT tenant_id, count(*) FROM app.documents GROUP BY tenant_id;
ROLLBACK;
Only tenant 1 should appear. Repeat with tenant 2. With no tenant setting or role/database default on a fresh connection, expect no rows. Avoid persistent tenant settings: SET LOCAL can restore an earlier session value. In separate disposable transactions, attempt an insert for the other tenant and an update moving an owned row to the other tenant; both should fail. Roll back after each failure. Cross-tenant reads, updates and deletes of existing foreign rows should find none.
Use SET LOCAL only after BEGIN, on the same connection as the work. The app authenticates the tenant; a custom setting does not authenticate it. Arbitrary SQL clients can change this value. Use trusted server code; this setting alone cannot isolate untrusted SQL clients.
4. New objects undo restrictions → set defaults for their actual creator
Follow ALTER DEFAULT PRIVILEGES. Defaults affect future objects created by the named role, not existing objects or every role it belongs to:
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner
REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner
REVOKE ALL ON TABLES FROM PUBLIC, app_runtime;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA app
REVOKE ALL ON TABLES FROM PUBLIC, app_runtime;
A schema-scoped revoke cannot cancel global defaults. Audit other applicable default grants and existing functions. Grant only intended entry points, and review SECURITY DEFINER functions for owner privileges and a safe search path.
Keep new tables inaccessible to runtime until their grants and policies are ready in the same migration transaction. Confirmation: inspect \ddp in psql. In a rollback-only migration, create app.permission_probe as app_owner, then run SELECT has_table_privilege('app_runtime', 'app.permission_probe', 'SELECT');; expect false before an explicit grant. Repeat for the actual creator if migrations use a different role.
Evidence and limits
The 18.0 release notes, dated 2025-09-25, establish the major-version baseline. Michael Beardsley's 2020-05-18 AWS implementation report demonstrates tenant filtering and the owner-bypass trap, plus a shared-login tenant setting. Scope its session-setting pattern to transactions when reusing connections.
Authentication, network access, backups and provider administrators need separate controls. Exercise app views, functions and prepared statements as runtime too; one table policy does not cover every entry point.
Not for: PostgreSQL 15 public-schema migration failures; see postgres-15-permission-denied-for-schema-public-migration-fails.
Started from a post by @Michael Beardsley, packaged by the editor. Original post