DEV Community

How to Guard PostgreSQL & Supabase Schema Drift and RLS Without Granting SELECT on Any Table

Most database drift and data-quality tools ask for a connection string with SELECT privileges across your production tables. For B2B SaaS teams handling customer PII, billing records, or multi-tenant Supabase schemas, giving a CI workflow or monitoring agent read access to every production row is a hard no.

Yet three silent production regressions keep happening on PostgreSQL, Supabase, and Neon:

  • Someone disables Row-Level Security (RLS) on a table in the dashboard during debugging and forgets to re-enable it.
  • A developer creates a public view without security_invoker = true - silently bypassing RLS on the underlying tables because views default to running with the view owner's privileges.
  • An unreviewed migration adds a column with a volatile default, runs CREATE INDEX without CONCURRENTLY, or drops a column that downstream services still query.

I built pgseal (GitHub Actions Marketplace) - a single static Go binary (~10.75 MB, MIT license, only pgx/v5 as a runtime dependency) - to catch all three while proving it has zero read access to user table rows.

Zero-Data-Access Attestation (select_privileged_tables = 0)

Running pgseal init --role pgseal_ro --db appdb prints the least-privilege SQL for your database:

CREATE ROLE pgseal_ro LOGIN PASSWORD '<set-via-secret-manager>' NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS NOINHERIT CONNECTION LIMIT 2 ;
GRANT CONNECT ON DATABASE appdb TO pgseal_ro ;
ALTER ROLE pgseal_ro SET default_transaction_read_only = on ;
ALTER ROLE pgseal_ro SET statement_timeout = '3s' ;
ALTER ROLE pgseal_ro SET lock_timeout = '200ms' ;

Notice there are zero GRANT SELECT statements on any user table. PostgreSQL lets any role with CONNECT inspect pg_catalog.* and pg_stat_* metadata. pgseal executes all 12 embedded catalog queries inside a single transaction:

BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY ;
SET LOCAL statement_timeout = '3000ms' ;
SET LOCAL lock_timeout = '200ms' ;

Every pgseal doctor and pgseal snapshot run records a live attestation:

  • select_privileged_tables = 0
  • is_superuser = false
  • bypass_rls = false
  • read_only_tx = true

In CI across PostgreSQL 15, 16, and 17 behind PgBouncer (pool_mode=transaction), our integration suite explicitly verifies two things:

  • pg_stat_user_tables user-table scan delta (seq_scan + idx_scan) equals 0 (validated alongside a negative control that proves a real SELECT increments the counter).
  • Attempting SELECT 1 FROM public.<user_table> with pgseal_ro returns PostgreSQL error 42501 permission denied.

Catching Real RLS Leaks (Verified Live on Neon PostgreSQL 17.12)

During live testing against a Neon Serverless PostgreSQL 17.12 instance (-pooler.*.aws.neon.tech with channel_binding=require&sslmode=require), two real-world catches stood out immediately:

  • Views silently bypassing RLS (RLS-004): When we created a normal public.invoice_summary view and granted SELECT to PUBLIC, pgseal rls flagged [BREAKING] RLS-004 (security_invoker=false), AND pgseal doctor simultaneously caught that pgseal_ro had inherited SELECT on that view via PUBLIC (DOC-001, exit code 2).
  • Live RLS toggle + breaking column drop: After revoking PUBLIC access on the view (doctor passed in 398 ms over the internet with select_privileged_tables=0, pooler_detected="neon-pooler"), we took a baseline snapshot (841 ms) and ran:
ALTER TABLE public.invoices DISABLE ROW LEVEL SECURITY ;
ALTER TABLE public.invoices DROP COLUMN created_at ;

pgseal diff immediately exited 2 catching DIFF-010 [BREAKING], and pgseal rls --baseline exited 2 catching RLS-001 [BREAKING] (rls_enabled: true -> false).

Altogether, pgseal rls checks 7 rules (RLS-001 through RLS-007), including USING (true) / 1=1 tautologies, 0-policy lockouts, SECURITY DEFINER functions exposed to anon / public, and policies referencing client-mutable user_metadata.

Transaction Pooler Safety (PgBouncer / Supavisor / Neon Pooler)

A common reason schema tools fail on Supabase (:6543 Supavisor) and Neon (-pooler.neon.tech) is prepared-statement caching or unsupported startup options parameters in transaction pooling mode. pgseal enforces:

  • pgx.QueryExecModeSimpleProtocol with StatementCacheCapacity = 0
  • Transaction-local SET LOCAL instead of startup options
  • Automatic Neon host detection (extending connect timeout to 30s for scale-to-zero compute wakeups, retrying transient 57P03 errors while failing fast on 28P01 auth errors)
  • Default exclusion of internal Supabase schemas (auth, storage, realtime, vault, extensions, pgsodium, supabase_migrations, etc.)

Fast Deterministic Lockfiles, Offline SQL Linting & Zero-ML Freshness

  • Deterministic diff: .pgseal/schema.lock.json stores canonical JSON with Merkle SHA-256 sub-hashes. On Apple M2, diffing a 500-table / 5,000-column schema takes 0.24 ms/op (0.14 ms/op on unchanged schemas).
  • Offline migration lock safety (pgseal check-sql): A pure-Go SQL lexer checks 9 rules before merge (SQL-001 - SQL-009), including CREATE INDEX without CONCURRENTLY, CONCURRENTLY inside BEGIN..COMMIT, volatile ADD COLUMN DEFAULT, SET NOT NULL without NOT VALID, missing SET lock_timeout, and CREATE TABLE in exposed schemas without ENABLE ROW LEVEL SECURITY.
  • Zero-ML table freshness (pgseal watch): Tracks pg_stat_user_tables write-activity deltas across 168 weekday×hour seasonal buckets using MAD/IQR with stats-reset and replica guards.

3-Line GitHub Action

- uses: yun87457-web/pgseal@v0
  with:
    mode: drift # drift | rls | check-sql | watch
    dsn: ${{ secrets.PGSEAL_DATABASE_URL }}

The Action downloads the pinned static release binary, verifies its SHA-256 against checksums.txt, posts or updates a single sticky PR comment (<!-- pgseal -->), and safely skips with exit code 0 on fork PRs when secrets are unavailable.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.