Dev.to Security 🔐 Cybersecurity 👁 2 📖 4 min read

Row-Level Security (RLS) in PostgreSQL for multi-tenant SaaS

Row-Level Security (RLS) moves tenant isolation from “every engineer remembers WHERE tenant_id = $1” to “PostgreSQL refuses to return another tenant’s rows even if a query forgets the filter.” In a multi-tenant SaaS pro

Row-Level Security (RLS) moves tenant isolation from “every engineer remembers WHERE tenant_id = $1” to “PostgreSQL refuses to return another tenant’s rows even if a query forgets the filter.”

In a multi-tenant SaaS product, forgetting a single WHERE tenant_id = $1 in one reporting query or background job is an instant S1 data breach. In Colophon CMS, we pair type-level tenant scoping in Rust with PostgreSQL RLS so both layers have to fail simultaneously before a cross-tenant read can happen.

The four rules of production RLS

Most RLS tutorials stop right after ENABLE ROW LEVEL SECURITY, leaving three dangerous gaps open:

  1. Run the app as a non-superuser role without BYPASSRLS. Superusers and roles with BYPASSRLS ignore every RLS policy silently. Separate your migrator role (owns tables) from your runtime app role (least-privilege SELECT, INSERT, UPDATE, DELETE only).
  2. Always add FORCE ROW LEVEL SECURITY if the table owner could ever query it. Without FORCE, the table owner bypasses RLS by default.
  3. Set tenant context with SET LOCAL inside a transaction, never session-level SET. With connection pooling (PgPool, PgBouncer), a session-level SET app.tenant_id leaks onto the next HTTP request that borrows that pooled connection. SET LOCAL resets automatically at COMMIT or ROLLBACK.
  4. Fail closed when the setting is missing. Use current_setting('app.tenant_id', true) and compare strictly—if the variable is unset or empty, return zero rows instead of throwing or matching everything.

Exact schema and policy setup

CREATE TABLE documents (
    id          uuid PRIMARY KEY,
    tenant_id   uuid NOT NULL,
    title       text NOT NULL,
    body        jsonb NOT NULL
);

CREATE INDEX idx_documents_tenant ON documents (tenant_id, id);

ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
ALTER TABLE documents FORCE ROW LEVEL SECURITY;

CREATE POLICY tenant_isolation ON documents
    AS RESTRICTIVE
    FOR ALL
    TO app_runtime
    USING (
        tenant_id = NULLIF(current_setting('app.tenant_id', true), '')::uuid
    )
    WITH CHECK (
        tenant_id = NULLIF(current_setting('app.tenant_id', true), '')::uuid
    );

Notice both USING (guards SELECT, UPDATE, DELETE) and WITH CHECK (guards INSERT and UPDATE so Tenant A cannot insert a row claiming to belong to Tenant B).

And on every request in Rust (sqlx):

let mut tx = pool.begin().await?;
sqlx::query("SELECT set_config('app.tenant_id', $1, true)")
    .bind(tenant_id.to_string())
    .execute(&mut *tx)
    .await?;

// All queries on &mut *tx are now strictly scoped to `tenant_id`.
// On commit or drop/rollback, the local setting disappears automatically.

The third argument true in set_config('app.tenant_id', $1, true) is the SQL equivalent of SET LOCAL.

Watch out for the Neon Postgres gotcha

If you host on Neon (where there is no superuser), do not attach SET app.tenant_id = '' directly onto a CREATE FUNCTION ... SET or ALTER DATABASE ... SET statement—Neon refuses custom parameter SET clauses for non-superusers and refuses GRANT SET ON PARAMETER. Instead, save, clear, and restore the setting inside the function body with set_config. (See Migrating to Neon Postgres without superuser.)

Prove RLS works with a cross-tenant attack test

Never assume a policy is active until a test tries to break it:

  1. Insert a row for Tenant A and a row for Tenant B.
  2. Connect using the actual app_runtime role (not the test superuser!).
  3. Run SELECT count(*) FROM documents without setting app.tenant_id → assert 0.
  4. Set app.tenant_id to Tenant A and run SELECT * FROM documents WHERE id = $tenant_b_row_id → assert 0 rows.
  5. Attempt INSERT INTO documents (id, tenant_id, ...) VALUES (..., $tenant_b_id, ...) while scoped to Tenant A → assert PostgreSQL rejects it with a policy violation (42501).

Pair that test with a mutation proof so you know it goes red if someone accidentally grants BYPASSRLS or drops the policy.

Frequently asked questions

Does PostgreSQL Row-Level Security (RLS) work with connection pooling like PgBouncer?
Yes—as long as you set the tenant context using transaction-scoped SET LOCAL (or set_config(..., true)) inside every transaction. Never use session-level SET with a connection pooler, or one tenant’s ID will leak to the next request sharing that connection.

Why is FORCE ROW LEVEL SECURITY needed in Postgres?
By default, the PostgreSQL role that owns a table bypasses RLS policies silently. Adding ALTER TABLE ... FORCE ROW LEVEL SECURITY ensures even the table owner is subject to the policy (though running production queries as a separate least-privilege non-owner role is still best practice).

Does PostgreSQL RLS slow down multi-tenant queries?
Overhead is minimal when your policy compares an indexed tenant_id column against a simple current_setting('app.tenant_id', true)::uuid expression. Always create composite indexes starting with tenant_id (e.g., (tenant_id, id)) and inspect EXPLAIN ANALYZE to confirm index scans.

Originally published at 404human.dev.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.