---
title: "Discussion on \"Postgres row-level security for multi-tenant SaaS: a setup that holds up\" | Hashnode"
description: "Discussion on \"Postgres row-level security for multi-tenant SaaS: a setup that holds up\". Set up Postgres row-level security for multi-tenant SaaS: policies, safe tenant context behind PgBouncer, and the five ways RLS gets bypassed."
url: "https://hashnode.com/posts/postgres-row-level-security-for-multi-tenant-saas-a-setup-that-holds-up/6ac9181e52c978b433ae915b"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Postgres row-level security for multi-tenant SaaS: a setup that holds up](https://axionry.hashnode.dev/postgres-row-level-security-for-multi-tenant-saas-a-setup-that-holds-up?utm_source=hashnode&utm_medium=feed)

Set up Postgres row-level security for multi-tenant SaaS: policies, safe tenant context behind PgBouncer, and the five ways RLS gets bypassed.

Oct 9 · 6 min read · axionry.hashnode.dev  
Read the article

-   [#postgresql](https://hashnode.com/tag/postgresql)
-   [#saas](https://hashnode.com/tag/saas)
-   [#security](https://hashnode.com/tag/security)
-   [#databases](https://hashnode.com/tag/databases)

## Written by

[Neeraj Sharma](https://hashnode.com/@neeraj0786)

@neeraj0786 · Gurugram, India

  
[View profile](https://hashnode.com/@neeraj0786)

Founder @ axionry.com | Head of Engineering @ AccioJob (YC'19)

I'm Neeraj Sharma, founder of Axionry (https://axionry.com), an AI product studio building AI products, AI agents, voice agents and MVPs that ship in days, with $0 upfront. Head of Engineering at a YC-backed company: I run 4 to 5 large platforms with a small team, cut a $200K/year cloud bill by more than 70% and took voice AI from 10 cents to 2.5 cents a minute. Book a call: https://axionry.com/book | X and Instagram: @axionry\_

## Responses 2

Markdown supported · ⌘↵ to post

[Ascendra Ventures](https://hashnode.com/@ascendra-ventures)

[Oct 11](https://hashnode.com/posts/postgres-row-level-security-for-multi-tenant-saas-a-setup-that-holds-up/6ac9181e52c978b433ae915b/comment/6acaf44d940bea2fb15b98db)

Good to see a write up that covers the parts that usually bite later, such as connection pooling and setting the tenant context per transaction rather than per session.

One practice I would add: make cross tenant access tests part of the definition of done for every feature that touches tenant data. A small suite that logs in as tenant A and tries to read, update and export tenant B's records catches the cases where a new view or a background job quietly bypasses the policy. Those tests are cheap to write and they turn row level security from a configuration you trust into a property you verify on each deploy.

It also makes security reviews with customers much easier, because you can show the tests rather than describe the intent. Do you run background jobs under a separate role with its own policies, or do they set the tenant context the same way requests do?

[Parsa Aftabi](https://hashnode.com/@parsaaftabi)

[Oct 9](https://hashnode.com/posts/postgres-row-level-security-for-multi-tenant-saas-a-setup-that-holds-up/6ac9181e52c978b433ae915b/comment/6ac96491954062902cbd322c)

Strong post. RLS is the right answer for exactly the reason you give. A few production gotchas from my side. RLS doesn't apply to table owners or superusers, so the app role must not own the tables, and watch out for BYPASSRLS. The tenant context (e.g. SET app.current\_tenant) has to be reset per checkout when you're behind PgBouncer. A leaked tenant setting is a data leak. And policy expressions run on every row, so keep them sargable and index-friendly on large tables or RLS becomes a performance tax. Miss any of those and the enforcement story quietly breaks.
