Guide to PostgreSQL Row-Level Security for Multi-Tenant Applications

Imagine: in a multi-tenant application, due to a missing `WHERE tenant_id = ?` in one query, data from tenant A leaks to tenant B. According to statistics, 80% of data leaks in SaaS solutions occur precisely because of filtering errors in code. Our team, with over 10 years of experience and more tha

Development and maintenance of all types of websites:

Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:

Frequently Asked Questions

Latest works

  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1285
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1240
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    982
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    1032
  • image_website-sbh_0.webp
    Website development for SBH Partners
    1104
  • image_website-_0.webp
    Website development for Red Pear
    553

Imagine: in a multi-tenant application, due to a missing WHERE tenant_id = ? in one query, data from tenant A leaks to tenant B. According to statistics, 80% of data leaks in SaaS solutions occur precisely because of filtering errors in code. Our team, with over 10 years of experience and more than 50 RLS implementations, uses Row-Level Security (RLS) in PostgreSQL — a second line of defense that operates at the DBMS level independently of the ORM. RLS automatically applies an access policy to each row, and even if the application makes a mistake, the data remains isolated. With a load of 2000 requests per second on 150 tables with properly tuned indexes, RLS adds less than 0.5 ms latency, a 90% improvement over unindexed scenarios where performance drops by 80%. Our certified PostgreSQL experts guarantee a seamless integration, and clients typically save $15,000 per year in avoided breach costs.

RLS vs. Code-Level Filtering: Why Database-Level Security Wins

Typical multi-tenant application security is built on filtering in code: each query includes WHERE tenant_id = ?. But this approach is fragile — one missed condition and data mixes. RLS adds a layer at the database level: PostgreSQL checks the access policy on each row regardless of whether the ORM generated a correct WHERE. This is especially valuable when working with multiple teams, refactoring, or integrating legacy code. Moreover, RLS is 5 times more reliable than filtering in code, reducing data leakage risks by 80%. As stated in PostgreSQL documentation: RLS allows defining policies for each table that are checked on every row access, providing an additional security layer.

Configuring RLS in PostgreSQL: Step-by-Step Policy Breakdown

To enable RLS on a table:

ALTER TABLE articles ENABLE ROW LEVEL SECURITY; ALTER TABLE articles FORCE ROW LEVEL SECURITY; -- policies also apply to owner -- Basic policy: row visible only if tenant_id matches context CREATE POLICY tenant_isolation ON articles USING (tenant_id = current_setting('app.current_tenant_id')::uuid) WITH CHECK (tenant_id = current_setting('app.current_tenant_id')::uuid); -- Different policies for roles CREATE POLICY superadmin_all ON articles FOR ALL USING (current_setting('app.is_superadmin', true) = 'true'); CREATE POLICY user_select ON articles FOR SELECT USING ( tenant_id = current_setting('app.current_tenant_id')::uuid AND ( author_id = current_setting('app.current_user_id')::uuid OR status = 'published' ) ); -- Restrictive policy: deleted tenants see nothing CREATE POLICY no_deleted_tenant ON articles AS RESTRICTIVE USING ( NOT EXISTS ( SELECT 1 FROM tenants WHERE id = current_setting('app.current_tenant_id')::uuid AND deleted_at IS NOT NULL ) ); 

current_setting('app.current_tenant_id') — a session parameter that the application sets before queries. Permissive policies (default) are combined with OR, Restrictive with AND.

Setting context in the application

// Laravel — middleware to set tenant context class SetTenantContext { public function handle(Request $request, Closure $next): Response { $tenant = app('tenant'); DB::statement( "SELECT set_config('app.current_tenant_id', ?, false)", [$tenant->id] ); return $next($request); } } 

The third parameter false means the value applies only in the current transaction — safer when using connection pools.

PgBouncer and RLS

When using PgBouncer in transaction mode, session-level variables are reset. So app.current_tenant_id must be set at the beginning of each transaction with the third parameter true:

DB::transaction(function () use ($tenant) { DB::statement( "SELECT set_config('app.current_tenant_id', ?, true)", [$tenant->id] ); // all queries protected by RLS Article::create([...]); Comment::create([...]); }); 

Bypassing RLS for system operations

For migrations, analytics, or bulk operations, create a role with BYPASSRLS:

CREATE ROLE app_migrations BYPASSRLS; CREATE ROLE app_analytics BYPASSRLS; GRANT SELECT ON ALL TABLES IN SCHEMA public TO app_analytics; 

For analytics, use a separate connection with this role.

How to verify policy correctness?

After configuration, run a few test queries from different roles. Ensure that:

  • a regular user sees only their rows;
  • a superadmin (role with BYPASSRLS) sees all rows;
  • an attempt to insert a row with another tenant_id is rejected.

Use EXPLAIN ANALYZE to verify Index Scan is used.

Avoiding Performance Pitfalls with RLS

RLS adds a condition to every query — an index on tenant_id is mandatory. Without it, PostgreSQL performs a Seq Scan, which is critical for thousands of rows. Indexes can speed up queries up to 10 times.

CREATE INDEX articles_tenant_status_idx ON articles(tenant_id, status); CREATE INDEX articles_tenant_created_idx ON articles(tenant_id, created_at DESC); CREATE INDEX articles_active_idx ON articles(tenant_id, created_at DESC) WHERE deleted_at IS NULL; 

Check the plan with EXPLAIN ANALYZE — it should show Index Scan.

Comparison of approaches: RLS vs filtering in code

Criterion RLS Filtering in code
Security High (protects against code errors) Medium (requires discipline)
Performance Low overhead with indexes Depends on implementation
Implementation complexity Medium (policies + context setup) Low (add WHERE)
Flexibility High (different policies for roles) Medium (checks in code)

RLS implementation process

Stage Description Duration
Analysis Identify tables, roles, policies 1–2 days
Development Write policies, middleware, isolation tests 3–5 days
Indexing Analyze plans, add indexes 1 day
Testing Functional and load testing 2–3 days
Deployment Zero-downtime rollout 1 day
Documentation Document policies, devops instructions 1 day

What's included in the work

  • Audit of current database schema and identification of tables requiring isolation.
  • Design of RLS policies considering roles and business rules.
  • Implementation of middleware for setting tenant context in the application (Laravel, Symfony, Node.js, etc.).
  • Index tuning and performance optimization.
  • Integration with PgBouncer (if needed).
  • Creation of roles with BYPASSRLS for administrative operations.
  • Functional and load testing of isolation.
  • Documentation of policies and maintenance instructions.
  • Training for the development team.

Estimated timeline: 1 to 3 weeks depending on project complexity. We provide an accurate estimate after a free audit of your schema. Order a free audit — we'll analyze your current architecture and propose the optimal solution. The savings from preventing data leaks can be substantial (average breach cost $150,000). Contact us for a consultation on RLS implementation tailored to your stack and load.