How to enable Supabase RLS correctly
If you asked an AI coding tool to stand up a Cursor, Lovable, or Bolt app on Supabase, you have probably asked some version of this: how do I enable RLS in Supabase, and how do I know it actually worked?
Do it in a fixed order. Create the table first. Enable Row Level Security after the table exists. Add narrow owner policies that use auth.uid(). Then sign in as two different users and try to read each other's rows. That last step shows what the database will do, not what the SQL file says.
We learned this from a saved test, not a live leak. On October 26, 2026 we asked one model, six times, to build a small Next.js + Supabase app with private notes in a notes table. Three runs had no project rules. Three had our free rules loaded as project instructions. Every raw output is in the public vibe-guardrails-demo repo. Nobody ran the SQL against a database, and nothing was deployed.
The line can be in the file and still do nothing
In one of the three no-rules outputs, the SQL started like this:
-- Enable RLS alter table if exists notes enable row level security;
A few lines later, it created the notes table.
When that first line runs, notes does not exist yet. Because of if exists, Postgres skips it without an error. The table is then created with RLS off, which is the Postgres default, and the owner-only policies written below it have no effect. Who can reach that table now depends on its grants. In a typical Supabase setup, the API roles often have them.
So the comment says "Enable RLS," the policies look right, and the table never gets RLS. In that same output, a profiles table never got RLS either, and it had a using (true) read policy. The other two no-rules outputs got the order right. This is our clearest example, not the typical result.
The order that works
For a new table of private, user-owned data, use one migration, in this order:
- Create the table. Give private rows a
user_id(or equivalent) you can compare to the signed-in user. - Enable RLS after the table exists. Use
alter table notes enable row level security;. Do not runalter table if exists … enable row level securitybeforecreate table. On a fresh database that line does nothing. - Add narrow owner policies. Write one policy per operation (
select,insert,update,delete), scoped withauth.uid() = user_id. Put an explicitwith checkoninsertandupdate. Do not useusing (true)on private user data. - Test with two users. Sign in as user A and try to read or change user B's row. It should fail.
Here is that pattern on a notes table like the one in our test:
create table notes ( id bigint generated by default as identity primary key, user_id uuid references auth.users not null, title text, content text, created_at timestamp with time zone default now() ); alter table notes enable row level security; create policy "notes_select_own" on notes for select using (auth.uid() = user_id); create policy "notes_insert_own" on notes for insert with check (auth.uid() = user_id); create policy "notes_update_own" on notes for update using (auth.uid() = user_id) with check (auth.uid() = user_id); create policy "notes_delete_own" on notes for delete using (auth.uid() = user_id);
Also check grants, and know which roles (anon, authenticated) can reach each table: policies do not take a grant away. The service_role key bypasses RLS, so keep it in server code only.
What the two-user test is for
Reading the migration only tells you what the SQL says. Signing in as user A and requesting user B's row by id shows what the database actually does. You can also ask Postgres which tables still have RLS off:
select tablename from pg_tables where schemaname = 'public' and rowsecurity = false;
That query should return nothing you did not mean to leave open. We suggest this check in our rules. Nobody ran it in our test.
What our test did and did not show
This was one model, one prompt, and three runs per side. It was not a controlled benchmark, and it says nothing about improvement rates. The rules runs also got a follow-up message the others did not, and AI output changes from run to run.
With the rules loaded, two outputs included SQL, and both enabled RLS right after each create table and added owner policies. The third declined to write the app because of the demo keys in the prompt, even after a follow-up.
Owner-only access protects privacy, but it does not protect a paid-access column. In every output that included SQL, with or without rules, the user's own-row update policy probably let them set is_pro to true themselves. Some outputs set it from the browser. The rules we loaded did not catch this. Paid access belongs on the server, for example from a verified Stripe webhook.
A rules file guides what the assistant writes. It does not enforce permissions, and it does not replace database security or testing.
Checklist
- Every
enable row level securitycomes after itscreate table. select tablename from pg_tables where schemaname = 'public' and rowsecurity = false;returns nothing you did not mean to leave open.- You know which roles (
anon,authenticated) can reach each table. - Private, user-owned tables use owner policies on
auth.uid(), notusing (true). insertandupdatepolicies includewith check (auth.uid() = user_id).- Users cannot write protected columns like
is_pro, plan, or credits. - The
service_rolekey is in server code only. - Two different users cannot see or change each other's rows.
A rules file that asks for this order
All six raw outputs, the prompt, and the rules we loaded are public: vibe-guardrails-demo on GitHub.
The three rules used in that test come from our Ship-Safe Pack (24 rules and prompts for Cursor, Claude Code, AGENTS.md, and paste-in tools such as Lovable and Bolt). A Stripe rule in the pack says to grant paid access only from a verified webhook. It was not loaded in this test, so we cannot show what it does here.
Rules guide what the assistant writes. They do not replace a review.
Made by VibeGuard.