Multi-Tenant SaaS With Postgres Row-Level Security: A Practical Guide

Postgres row-level security is a database feature that lets a multi-tenant software-as-a-service product enforce, inside the database itself, which rows each user or tenant may read and change. In a multi-tenant product, many customers share the same tables, and a single missing filter in application code can show one customer's data to another. Row-level security moves the rule into the database so that the filter is applied to every query. This guide explains how it works, using the PostgreSQL documentation for version 18 (the current release page read on 19 November 2026), the Supabase documentation and OWASP's 2025 list of web application risks, and then sets out a tenant design pattern, the pitfalls the documentation warns about and a testing checklist. It is a technical overview for founders and engineers, not a security audit, and it does not replace testing your own system.
Key takeaways
- The PostgreSQL documentation says that once row-level security is enabled on a table, all normal access must be allowed by a policy, and that with no policy a default-deny policy applies.
- Superusers and roles with the BYPASSRLS attribute always bypass it, and table owners normally bypass it unless the table is set to force it.
- CREATE POLICY has a USING expression that filters existing rows and a WITH CHECK expression that validates new or changed rows, and permissive policies combine with OR while restrictive policies combine with AND.
- The documentation warns that referential integrity and uniqueness checks ignore policies, which can leak information, and that functions marked leakproof can be evaluated before policy expressions.
- Supabase's documentation says a table exposed through its API without the feature enabled can be read and written by any role with a grant, so enable it on every exposed table and test with two tenants.
Postgres row-level security: how it works
PostgreSQL's chapter on row security policies explains that, in addition to ordinary SQL privileges, a table can have policies that restrict which rows normal queries return and which rows data-changing commands can insert, update or delete. Without policies, ordinary privileges alone decide access and all rows are equally available. The feature is switched on with ALTER TABLE ... ENABLE ROW LEVEL SECURITY, after which all normal access to the table must be allowed by a policy. If no policy exists, the documentation says a default-deny policy is used, so no rows are visible or modifiable. The same chapter notes that TRUNCATE and REFERENCES are not subject to the feature.
Who bypasses it
The documentation says superusers and roles with the BYPASSRLS attribute always bypass row security, and that table owners normally do too, although the table can be set with FORCE ROW LEVEL SECURITY so that the owner is also subject to policies. Only the owner can enable or disable the feature and create policies. For a SaaS product, this has a practical meaning: the database role your application uses to serve customers must not be a superuser, must not have the bypass attribute and, ideally, should not own the tables. Administrative and migration roles can be separate and used only for maintenance. Supabase's documentation adds that its service role bypasses the feature and must be kept on the server and never used in the browser.
Writing policies: USING and WITH CHECK
The CREATE POLICY page describes a policy as applying to a command, such as SELECT, INSERT, UPDATE, DELETE or ALL, for a role, with a USING expression and a WITH CHECK expression. USING filters rows that already exist, and rows that fail it are silently hidden. WITH CHECK validates rows being created or changed, and a failure raises an error. A SELECT policy cannot have WITH CHECK, and an INSERT policy cannot have USING. Policies are permissive by default, which means access is granted if any permissive policy passes, and restrictive policies are combined with AND, so access requires at least one permissive policy and every restrictive one. The documentation also says a policy with only USING gets an identical WITH CHECK implicitly in the cases it describes, so reads and writes are both covered.
A common tenant pattern
A widely used design adds a tenant identifier column to every tenant-owned table, sets a session variable or reads a claim from the authenticated user's token at the start of each request, and writes a policy that compares the row's tenant identifier to that value. This pattern is a design choice, not something the PostgreSQL documentation prescribes. Supabase's documentation shows policies that use the signed-in user's identity from its authentication layer. Whatever the mechanism, make three things true. The identifier must come from a trusted source, never from user input. Every table that holds tenant data must have the column and the policy. And new tables must not be created without them, so add a check to your migration process that fails if a tenant table lacks a policy.
Pitfalls the documentation warns about
- Covert channels: PostgreSQL says referential integrity checks and uniqueness checks ignore policies, so a user may be able to infer that a row exists in another tenant's data.
- Leakproof functions: policy expressions are evaluated before user-supplied conditions except for functions marked leakproof, which the optimiser may apply earlier, so be careful what you mark leakproof.
- Race conditions in subqueries: policies that query other tables can behave unexpectedly under concurrent updates, and the documentation suggests locking options.
- Views: Supabase's documentation says views bypass the feature by default unless configured otherwise.
- Privileges of the querying user: policy expressions run with the privileges of the user running the query, so the role must be able to read anything the policy references.
Performance
The PostgreSQL documentation says policy expressions that look only at the current row's values are the simplest and best-performing case, and that policy expressions are evaluated before conditions in the user's query. In practice, index the tenant identifier column, keep policy expressions simple and avoid subqueries where a column comparison will do. Measure the slowest queries with and without the policy before launch, using realistic data volumes and not a handful of demo rows.
Testing checklist
- Two-tenant test: create two tenants with data and confirm each can see only its own rows through the application and through direct database access using the application role.
- Write tests: try to insert and update rows with another tenant's identifier and confirm they fail.
- Role check: confirm the application role is not a superuser, does not have the bypass attribute and does not own the tables.
- Coverage check: list all tables and confirm each tenant table has the feature enabled and at least one policy.
- Search and exports: test reports, exports, full-text search and background jobs, which often bypass the normal path.
- Performance test on realistic data volumes.
Why this matters
OWASP's Top 10:2025 puts Broken Access Control first. In a multi-tenant product, the worst version of that flaw is one customer reading another's data. Row-level security does not replace good application design, but it adds a second line of defence that applies even when application code forgets a filter. The guide to scalable multi-tenant SaaS architecture covers the wider design choices, and the guide to rescuing an AI-built MVP shows how often generated apps miss this protection.
Where CreativeIn Tek fits
CreativeIn Tek builds multi-tenant software through its custom software development service. This guide describes a technique and does not claim that any CreativeIn Tek product is certified or immune to attack. A team planning a multi-tenant product can talk to CreativeIn Tek about architecture and review.
Frequently asked questions
What is Postgres row-level security?
It is a PostgreSQL feature that restricts which rows a user can see or change through policies attached to a table, in addition to normal privileges.
What happens if I enable it without a policy?
The documentation says a default-deny policy is used, so no rows are visible or modifiable for normal access.
Who can bypass it?
Superusers and roles with the BYPASSRLS attribute, and table owners unless the table is set to force it.
Is it enough to secure a multi-tenant app?
No. It is a strong second layer, but you still need safe application code, correct roles, tested policies and monitoring.
Does it slow queries down?
Simple policies that compare a column to a value perform best, so index the tenant column and test with realistic data volumes.
Last reviewed: 19 November 2026, against the PostgreSQL documentation on row security policies and CREATE POLICY, the Supabase row level security documentation and the OWASP Top 10:2025. Software and documentation change, and this guide is informational and not a security audit.


