Menu

1 / 2

Supabase multi-tenant RLS: membership policy pattern

DEV Community: postgres·TIC·10 days ago
#Qt3Vfbt1
#devtopostgresql#devto#dev#tenant#policy
Reading 0:00
15s threshold

Multi-tenant row level security in Supabase means every row carries a tenant id and a memberships table says which users belong to which tenant. Three policies read that table: members see their own memberships, and they read and write only documents whose tenant they belong to. Tested on a real PostgreSQL with two tenants.

What a tenant is, to the database

What is multi-tenant RLS? It is row level security where the row's owner is a tenant rather than a user, so the policy cannot compare auth.uid() with the row directly. It has to ask a second table whether the signed-in user belongs to the row's tenant. That second table is the whole pattern. With row level security on and no policy, nothing is visible at all; PostgreSQL : "If no policy exists for the table, a default-deny policy is used, meaning that no rows are visible or can be modified." Proven: before any policy, a member sees 0 documents and no error.

public . tenants ( id , name ) public .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.