supabase
Supabase Row Level Security explained for beginners
What RLS is, why the anon key depends on it, copy-paste policies for a todos app, and how to test that strangers cannot read your users’ rows.
Paste your production URL into the free Vibe Code Health grader for a Semrush-style score on secrets, headers, TLS, and common leaks—then come back to this guide for the fixes.
If you use Supabase inside a Lovable, Bolt, Cursor, or Next.js app, you have probably heard this scary sentence:
“Turn on Row Level Security or anyone can read your database.”
This post is only about RLS—what it is, why the anon key depends on it, how to write your first policies, how to test them, and what to do when something breaks. It is not a general Supabase keys overview (that lives in How to stop leaking Supabase keys).
When you finish, paste your public URL into the Vibe Code Health grader. We treat a normal anon key in the browser as informational—not a failure—because anon is expected when RLS is correct. A service_role key in the browser is an incident.
The one idea that unlocks RLS
Think of your database like an apartment building:
- Tables are floors.
- Rows are apartments.
- RLS policies are the locks on each apartment door.
- The anon / authenticated roles are visitor badges.
Without RLS, turning on the lights in the lobby (giving people the anon key) can mean every apartment door is unlocked. With RLS, the badge only opens your apartment.
Official docs to keep open in another tab:
Anon key vs service_role (30-second reminder)
| Key | Where it belongs | RLS behavior |
|---|---|---|
anon / public | Browser OK | Enforced |
authenticated (user JWT) | Browser after login | Enforced |
service_role | Server only | Bypasses RLS |
The anon key is not a password for “do anything.” It is a public label that says “apply the policies.” If policies are missing, Postgres may allow nothing—or, depending on how you set the table up historically, far too much. Supabase’s product guidance is clear: enable RLS on tables exposed to clients.
Never put service_role in Lovable/Bolt/v0 public env, NEXT_PUBLIC_*, or VITE_*.
What “enable RLS” actually does
In the Supabase Table Editor (or SQL), enabling RLS means: for roles subject to RLS, no row access unless a policy allows it.
That default-deny posture is what you want for user data.
-- Turn on the lock system for this table
alter table public.todos enable row level security;
If you stop here with no policies, logged-in users (and anon) typically see zero rows. That feels “broken” in your UI—but it is safer than open access. Next you add policies that describe who can do what.
Your first policies: a todos app
Assume a table:
create table public.todos (
id uuid primary key default gen_random_uuid(),
user_id uuid not null references auth.users (id),
title text not null,
done boolean not null default false,
created_at timestamptz not null default now()
);
alter table public.todos enable row level security;
Read only your rows
create policy "Users can read own todos"
on public.todos
for select
to authenticated
using (auth.uid() = user_id);
auth.uid() is the UUID of the currently signed-in user from the JWT. The using clause filters which existing rows are visible.
Insert only as yourself
create policy "Users can insert own todos"
on public.todos
for insert
to authenticated
with check (auth.uid() = user_id);
with check validates new/updated row contents. This stops someone from inserting a todo tagged as another user.
Update / delete only your rows
create policy "Users can update own todos"
on public.todos
for update
to authenticated
using (auth.uid() = user_id)
with check (auth.uid() = user_id);
create policy "Users can delete own todos"
on public.todos
for delete
to authenticated
using (auth.uid() = user_id);
What about anonymous visitors?
If todos require login, do not add anon policies. If you want a public marketing table (for example published blog posts), be explicit:
create policy "Anyone can read published posts"
on public.posts
for select
to anon, authenticated
using (published = true);
Public write policies for anon are rarely what you want on day one.
How vibe builders accidentally skip RLS
Common paths we see in vibe-coded apps:
- AI scaffold creates tables but never runs
enable row level security. - You test only with the service role in a server script, so everything “works,” then the client uses anon against an unlocked table.
- Policies use the wrong column (
userinstead ofuser_id) and silently match nothing—or worse, you disable RLS to “fix the demo.” - Storage buckets are public-read when you meant private (Storage has its own policies—see Storage access control).
How to test RLS like a beginner (do this before launch)
Test A — Two real users in the app
- Create User A and User B.
- As A, create two todos.
- Log out, log in as B.
- B must not see A’s todos.
- B must not be able to update A’s todo IDs via the API.
If B can see A’s data, stop shipping.
Test B — Anon key in the SQL / API explorer
In Supabase, use the client with the anon key (not service role) and no session. You should not see private user rows.
Test C — Intentional negative test
From the browser console (while logged in as B), try to select with a filter you hope fails. Better: use the REST API with B’s JWT and request A’s row by id. You want an empty result or error—not the row.
Debugging “my UI shows no data”
When RLS is new, empty screens are common. Checklist:
- Is the user actually signed in? (
auth.getSession()) - Does the row’s
user_idequalauth.uid()? (type UUID vs text mismatches happen) - Did you enable RLS but forget SELECT policy?
- Are you querying the right schema/table name?
- Are you accidentally using a server client with service role locally and anon in production (behavior differs!)?
Add a temporary SELECT policy only if you understand it—and remove debug policies before launch.
Patterns that scale past the first table
Shared documents (owner + collaborators)
Use a junction table (document_members) and policies that exists (...) a membership row. Supabase documents multi-table patterns in the RLS guides above.
Admin overrides
Do not put service_role in the client for admin screens. Use a server route with service_role, or an is_admin claim / table checked inside policies.
“Public read, owner write”
create policy "Public read"
on public.profiles
for select
to anon, authenticated
using (true);
create policy "Owner updates self"
on public.profiles
for update
to authenticated
using (auth.uid() = id)
with check (auth.uid() = id);
Only use using (true) on tables that are truly public.
Launch rule of thumb
Before you share a Supabase-backed vibe app:
- Every user-data table has RLS enabled.
- Every such table has explicit policies you can explain in one sentence each.
- Two-user test passes.
service_roleis not in any client bundle.- You ran a public grade for leaked keys and other ship-readiness issues.
Next reads
- How to stop leaking Supabase keys
- How to check if your vibe-coded app is safe before launch
- Is my Lovable app secure? A beginner checklist
RLS is not advanced wizardry—it is the lock on the door that makes the public anon key safe. Install the locks before you mail out keys.