Loading articles…
Loading articles…
Loading article…
Loading article…
A tenant_id column is not an isolation strategy by itself. Every read, update, delete, report, and background job must apply the same tenant boundary. PostgreSQL row-level security (RLS) can enforce that boundary close to the data, including when an application query forgets a filter.
With RLS enabled, normal table access must be allowed by a matching policy; if no policy applies, PostgreSQL defaults to denying rows. A SELECT policy uses a predicate to decide which existing rows are visible. INSERT and UPDATE should also use a WITH CHECK predicate to ensure a caller cannot create or move a row into another tenant.
A simplified policy might compare each row's tenant_id to a transaction-local tenant setting. That setting must be established from authenticated server context, never copied from an untrusted request field. With a connection pool, use transaction-local settings and reset context reliably so one request cannot inherit the previous request's tenant.
Table owners normally bypass row security, as do superusers and roles with BYPASSRLS. If the application connects as the owner, the policy may appear correct in a console and still fail to protect production queries. Use a restricted application role, and consider FORCE ROW LEVEL SECURITY when owner behavior must also be subject to policies.
RLS is a strong backstop, not a replacement for authorization. The service still needs to confirm that the user belongs to the requested tenant and is allowed to perform the action. RLS does not automatically protect operations such as TRUNCATE, and administrative paths may deliberately use a more privileged role.
RLS adds confidence when the database role and request context are designed carefully. Decide which services need cross-tenant access, how those paths are audited, and how tenant identity flows from authentication to each transaction before turning policies into production controls.
Chief Technology Officer
Newsletter
New articles from our engineers, straight to your inbox. One email a month, no spam.