Supabase RLS for Directories: Admin, Owner, Visitor
A directory's rows start public and get an owner later. How grants, policies, a view and one WITH CHECK decide what each of the three roles actually sees.
Most Supabase RLS material models a SaaS. Every row belongs to a tenant, nobody outside that tenant ever sees it, and the row's owner is whoever created it. A directory breaks all three assumptions at once: its rows exist to be read by strangers, most of them belong to nobody, and the ones that do got their owner months after somebody imported them from a spreadsheet.
Copy tenant policies onto that shape and you end up with one of two sites. Either visitors see an empty catalogue, or a signed-in user can quietly publish their own listing. What follows is the access model that fits a directory: which check belongs in a grant rather than a policy, why the phone number needs a view, what with check has to forbid on a row the user genuinely owns, and the constraint that will cheerfully confirm the existence of rows your policies hide.
Three audiences carry the whole model. A visitor arrives from Google, signed out, and has to read every live listing. A business owner signs in to edit the single row that describes them. An admin moderates everything. Postgres only sees the first two of those: Supabase maps an unauthenticated request to the anon role and a signed-in one to authenticated. "Admin" is a column in a table you own, not a Postgres role. That trips people up on day one.
The grant runs before the policy, and it fails louder
Postgres runs two checks before a client touches a table. Grants decide whether a role may run the operation at all. Policies decide which rows it applies to. The order matters more than most guides admit, because a missing grant raises a 42501 error before any policy runs — so a request your policy was written to allow can fail without your policy ever being consulted.
The other half of that page is the part that bites directories. On existing Supabase projects, a new table in public arrives with select, insert, update and delete already granted to anon, authenticated and service_role. Adding policies does not take those grants back. A listings table protected only by policies still hands the signed-out role an insert path.
anon needs exactly one privilege on your listings table. Read.
alter table public.apps enable row level security;
revoke all on table public.apps from anon, authenticated;
grant select on table public.apps to anon;
grant select, insert, update on table public.apps to authenticated;
-- No delete grant for either client role. A listing that vanishes takes its
-- reviews, its inbound links and its city page with it. Deletion is an admin
-- action through the server, and "removed" is a status, not a missing row.Withholding delete from authenticated is a directory-specific call, and it is worth making deliberately. In a SaaS, a user deleting their own document is normal. In a directory, the row is a public page that other pages link to.
RLS hands out whole rows, so the phone number needs a view
A select policy filters rows. It has no opinion about columns whatsoever: if the row passes, every column on it passes with it. Listings are exactly where that hurts, because one row carries both the part you publish — name, category, city, description — and the part the business handed over so you could contact them.
Two mechanisms close the gap. Postgres column privileges are one; the passwd example in the PostgreSQL manual grants select on a named list of columns to public and leaves the password hash unreachable. They are precise, they fail fast, and they show up in the schema. The bookkeeping is the cost: every column you add is a column you have to remember to grant.
A view moves that decision into one place you can read in a single sitting. Views need care of their own, though. Supabase's guide is blunt about it: views bypass RLS by default, because Postgres creates them with security definer, so a view over a protected table hands out every row its policies were meant to withhold. From Postgres 15 you fix that with security_invoker.
create view public.apps_public
with (security_invoker = true)
as select id, slug, name, description, categories, status,
address_city, address_country, latitude, longitude
from public.apps;DirectoryLaunch takes the view route. Migration 0013_security_hardening.sql moves public listing reads to an apps_public view that leaves out contact_email, phone, submitted_by and address_exact. Skip that migration and those four columns are readable by anyone holding the publishable key, which ships inside your client bundle by design. The go-live checklist has the verification query for it.
An owner may edit the listing. Publishing it is not theirs.
This is the policy most directories forget, and the Postgres manual happens to contain the exact shape of it. In the passwd example, a normal user may update their own row, and the update policy's with check clause pins one column to an allowlist of shells. Swap the nouns and you have a listing: the owner may change their description, their hours and their logo. They may not flip status from pending to live, they may not set a featured flag, and they may not hand the row to somebody else.
using decides which existing rows the update applies to. with check decides what the resulting row is allowed to look like.
-- Anyone, signed in or not, reads what is live.
create policy "anyone reads live listings"
on public.apps for select
to anon, authenticated
using ( status = 'live' );
-- An owner also reads their own row while it is still pending.
create policy "owners read their own listing"
on public.apps for select
to authenticated
using ( (select auth.uid()) = owner_user_id );
-- Editing is scoped on both sides: only a live row, still live afterwards.
create policy "owners update their own listing"
on public.apps for update
to authenticated
using ( (select auth.uid()) = owner_user_id and status = 'live' )
with check ( (select auth.uid()) = owner_user_id and status = 'live' );
-- The policy filters on owner_user_id, so that column needs its own index.
create index apps_owner_user_id_idx on public.apps using btree (owner_user_id);Those two select policies are permissive, and permissive policies combine with OR. The owner sees their pending row through the second one while the public sees nothing through the first. That is the behaviour you want, and it comes from the OR. Order is irrelevant here.
The and status = 'live' on both sides of the update policy is doing something a tenant model never has to do. with check cannot compare the new row against the old one, so "status must not change" is not expressible as a policy at all. Pinning the value is the blunt version of the fix, and it has a side effect you should decide on rather than discover: an owner can no longer unpublish themselves. The precise version is a column grant (grant update (name, description, logo_url) on public.apps to authenticated) or a before update trigger that compares old and new with is distinct from. MakerKit's RLS write-up walks through the trigger pattern in detail, and it is the right read if your columns are stricter than your rows.
One more trap from the same Supabase page: an UPDATE needs a matching SELECT policy. Without one the update does not work as expected, and it does not tell you why.
Ownership arrives late, and that is where claim policies go wrong
In a SaaS, the row is created by its owner. A directory listing is normally imported by you and claimed months later, which makes owner_user_id both nullable and mutable — the two properties RLS handles worst.
The harmless half first. auth.uid() returns null when nobody is signed in, and null = owner_user_id is never true in SQL, so a policy written as using (owner_user_id = auth.uid()) silently matches nothing for a visitor. Naming the role with to authenticated stops the expression running for anon at all, which is why every policy above carries one.
The dangerous half is the handover. If the policy that lets an owner update their row also permits writing owner_user_id, any signed-in user can take any unclaimed listing by sending a single field. So the transfer should not be an RLS decision. In DirectoryLaunch it runs through a server route with verification steps, and finalizeClaim() is idempotent behind a partial unique index — claims(app_id) where status = 'approved' — so an admin approval and a payment webhook arriving in the same second cannot transfer the same listing twice. The mechanics are in the claims docs and the longer write-up on verifying business owners.
There is a piece of honesty owed here about this boilerplate. The db layer in lib/supabase/database.ts uses the service-role key, which bypasses RLS entirely, so policies are the backstop that protects you from the publishable key rather than your first line of defence. A missing ownership check in a route handler is a live vulnerability even when the table's policies are perfect. The rules for that live in writing secure code, and they are not optional just because RLS is on.
Your unique index answers questions your policies refuse to
Near the end of the Postgres manual's RLS chapter sits a sentence nobody quotes: referential integrity checks, such as unique or primary key constraints and foreign key references, always bypass row security. The manual follows it with a warning about covert-channel leaks through those checks.
Directories hit this early. Listings have a unique slug. A visitor submits "Acme Plumbing", your form derives acme-plumbing, and Postgres rejects the insert with a unique violation — because a rejected, pending or admin-hidden listing already holds that slug. Your policies worked exactly as designed. The row is invisible. The constraint just confirmed it is there.
Whether that matters depends on what you catalogue. A restaurant guide leaks approximately nothing. A directory of therapists, a private vendor catalogue, or anything where "we have a pending application from this business" is itself news, leaks something real. The fix is not a policy: derive the slug server-side, suffix it on collision instead of surfacing the error, and keep the message the user sees generic. Foreign keys behave the same way, so a claim row pointing at an app_id the user cannot see still succeeds or fails based on whether that row exists.
Where a different model is the right one
Three cases where the shape above is the wrong answer, stated plainly because the comparison is useless otherwise.
Your product is a team SaaS. If access is decided by membership in an account, with role hierarchies and per-permission checks, a directory's policies will not stretch that far. A kit built for that — MakerKit's Supabase starter is the obvious one — ships an accounts and memberships model, permission tables, and explicit guards against privilege escalation when one user edits another's role. A listings table needs none of that, and a team product needs all of it.
Nothing ever talks to your database from the browser. If every read and write goes through your own server with a secret key, you can run a much thinner policy set and put the real authorization in route handlers. RLS still earns its keep as a backstop for the day someone adds a client-side query, but the discipline of four policies per table is a cost you are allowed to weigh.
Your columns are stricter than your rows. When the rule is "this user may touch this row, but never these three fields", policies are the wrong tool and column grants or a trigger are the right one. RLS has no way to express it.
And a quirk we carry rather than recommend: the users table in this boilerplate has two admin signals, role = 'admin' and is_admin = true. The isAdmin() helper in application code reads one; the database policies read the other. Migration 0013 makes the policies accept either signal, but two sources of truth for "who is an admin" is a design to set both fields on, not a pattern to copy. It is documented in the schema overview so nobody has to discover it at 2am.
What to check before you go live
Six things, in the order that finds problems fastest.
- Confirm RLS actually landed. Query
pg_class.relrowsecurityfor every table inpublic. Every row must come back true. The exact query is in the go-live checklist. - List what
anoncan reach.select table_name, privilege_type from information_schema.role_table_grants where grantee = 'anon' and table_schema = 'public'— anything pastselecton your public tables is a bug you have not hit yet. - Read the columns, not just the rows. For every table
anoncan select, print the column list and ask which of them you would publish on a webpage. That is the moment to decide between a column grant and a view. - Index the filter columns. A column only counts as indexed when it comes first in a btree index, so a composite primary key on
(app_id, user_id)gives you nothing for a policy that filters onuser_id. - Write the pgTAP tests, and match the assertion to how the denial happens. A missing grant and a
with checkviolation both raise42501, sothrows_okis right for those. Ausingclause that filters the row out raises nothing and matches zero rows, so useis_emptyover the statement withreturning, then read the target row back to prove it survived. Never confirm an allowed write withlives_ok— it passes on zero rows. - Run the claim twice, concurrently. If both succeed, the uniqueness constraint is missing, not the policy.
Start by running the grant query from item two against your production project. It takes thirty seconds. On a directory where every policy reads correctly, that is the check that most often comes back wrong.