Basics

What is RLS — and why your table is open to the whole internet

Illustration: a display case of record cards, with a gate at the entrance letting through only cards marked with your badge

Open any site built on Supabase, hit F12, and find the key to its database in the page source. It's there. Always.

That's not a leak and not somebody's mistake. That key is public by design: it ships to every visitor's browser. Which means the question "who gets into my table" isn't answered by the key. It's answered by RLS — and this is exactly where beginners lose data.

A public key is fine, as long as the database can say no

RLS stands for Row Level Security. It's a Postgres database mechanism: rules that decide which rows of a table a given request gets to see.

The logic goes like this. Since the key ends up in the browser anyway, you shouldn't be guarding the door — you should be guarding the data. Let the database ask, on every single request: "which rows is this person entitled to?"

Without RLS, the database never asks. A request arrived with the public key, so here's the whole table. Every customer's orders, everyone's private notes. Not because someone broke in, but because the database was never given a single reason to refuse.

A policy is a WHERE clause that adds itself

Here's the good part. An RLS rule is called a policy, and it looks almost like ordinary SQL:

create policy "Everyone sees only their own todos"
on todos for select
using ( (select auth.uid()) = user_id );

This is the mental model worth taking away. A policy is a WHERE condition the database appends to every query against that table. Your code asks for "all todos", and Postgres actually runs:

select * from todos
where auth.uid() = todos.user_id;  -- the policy added itself

auth.uid() is the id of whoever sent the request; the database reads it from the session token. So "give me all todos" physically cannot return someone else's. Even if the frontend is sloppy. Even if the request didn't come from your app at all, but from a console, by hand.

That's why RLS isn't "one more check on top". It's the one check that can't be bypassed, because it lives inside the database rather than in code anyone can swap out.

The trap: the table wasn't created by you, it was created by SQL

Now the part people actually get caught by. Supabase turns RLS on for you — but not always:

  • Created the table with a button in the Table Editor? RLS is on by default. You're protected and you never noticed.
  • Created it with a SQL statement (create table ...) in the SQL editor or a migration? RLS will not be enabled. Supabase's own docs say it outright: you have to turn it on yourself.

See the catch for vibe coding? When an AI agent sets up a table for you, it almost never clicks buttons. It writes create table. Which means you land on the unprotected branch by default — and everything looks perfectly fine in the dashboard.

Checking takes one line. So does fixing it:

alter table todos enable row level security;

"I turned RLS on and everything disappeared"

The second classic moment. You enable RLS, reload your app — and the lists are empty. No errors, the data is still in the table, and the screen shows zero rows.

That's correct behaviour. RLS works on "denied until allowed": the moment it's on, the API shows nothing until you create at least one policy. An empty screen here isn't a bug — it's proof the protection took hold.

From there you add policies one at a time, one per action: one for reading, one for inserting, one for updating. Permission to read your rows doesn't grant permission to change them — and that's a good thing.

Here's the practical order if you've already built something:

  • Walk every table in the public schema and check whether RLS is on. A single one left off is a hole.
  • Enable RLS everywhere people's data lives.
  • Add policies one action at a time, and test the app after each one.
  • The service role key bypasses RLS entirely — it's server-only. It never reaches a browser, a mobile app, or a repository, like any other secret.

A two-minute check

If your project is already live, don't guess — ask the database. One query in the SQL editor lists every unprotected table:

select tablename
from pg_tables
where schemaname = 'public' and rowsecurity = false;

An empty result means you can relax. Any row in the answer is a table that anyone holding the public key can read right now.

The Supabase dashboard gives you a second route: unprotected tables get flagged in the table list, and the built-in security advisor collects them into one report. Good habit: check it after every session where the tables were created by an agent rather than by you.

Do I need RLS if the app is just for me?

If the table lives in a cloud database exposed over an API — yes. "Just for me" describes who knows the app's address, not who can send a request. The key is public, the database URL is visible, and the rest takes about two minutes.

Why not just check permissions in the app code?

Because your code isn't the only way to talk to the database. A request can be sent directly, skipping your interface entirely, and every frontend check goes with it. RLS checks in the one place a request has to arrive no matter what — the database itself.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →