Supabase RLS Tutorial: Table and Storage Policies

Supabase RLS (Row Level Security) is a set of Postgres policies that decide which rows each request may read or change. Once you enable it on a table, a publishable key gets no data until a policy allows it, so you write one policy per action: select, insert, update and delete. Storage files follow the same rules.
Key takeaways
- Enabling RLS denies every client request until a policy allows it. Select, insert, update and delete each need their own policy, and an update also needs a select policy.
- Grants and policies are separate checks. On existing projects a new table in
publicgrants every privilege toanonandauthenticated, and adding policies does not take those grants back. - Storage access is RLS on
storage.objects. Buckets are private by default; a public bucket lets anyone with a file URL download it. - Put each user's files in a folder named after their user ID, and write the bucket policies against that folder.
- Test with two real users and with pgTAP. The dashboard shows whether RLS is switched on, not whether each policy is right.
Everything below was checked against the Supabase documentation on November 13, 2026, and each section links the page it relies on. The SQL follows the patterns those pages show, so you can paste it into the SQL Editor or a migration and adapt the table names.
What is Supabase RLS?
Row Level Security is a Postgres feature that Supabase applies to every request coming through its Data API. A policy is attached to a table and runs whenever the table is queried, so it behaves like a WHERE clause the client cannot remove (Supabase: Row Level Security). This policy:
create policy "Individuals can view their own todos."
on todos for select
to authenticated
using ( (select auth.uid()) = user_id );
means that a signed-in user who runs select * from todos only gets the rows whose user_id matches their own ID. Nothing in the frontend has to remember to add the filter.
Every request maps to a Postgres role, and policies are written for those roles (Supabase: API keys):
Postgres checks two things before a client touches a table. Grants decide whether a role may run an operation on the table at all, and policies decide which rows that operation applies to. A missing grant fails with error 42501 before any policy runs. On existing projects, a new table in public starts with every privilege granted to anon, authenticated and service_role, and adding policies does not revoke them (Supabase: default privileges).
Why does every Supabase table need RLS?
A table in an exposed schema without RLS can be read and written by any role that holds a grant on it. The Supabase Security Advisor reports this as an error called "Table publicly accessible" (lint 0013) and warns that anyone with your project URL can then read, edit and delete all data in that table (Supabase: Database Advisors).
When an app calls Supabase straight from the browser with the publishable key, RLS is the only check between a visitor and your data. That is the usual setup in apps generated with AI tools, and it is why missing or loose policies top our list of vibe coding security risks.
Switching RLS on is the start. In one audit, a member saw 0 rows in a private list and could still open each of those records by its ID.
How do you enable RLS on a Supabase table?
Run these statements in the SQL Editor, or put them in a migration so every environment gets the same rules. The example secures a reports table that only signed-in users use (Supabase: enable RLS and set the grants).
- Enable RLS. From now on the Data API returns no rows to a publishable key until a policy allows them.
alter table public.reports enable row level security; - Revoke the default grants from both client roles.
revoke all on table public.reports from anon, authenticated; - Grant back only what the app needs. Here signed-in users manage reports and signed-out visitors get nothing.
grant select, insert, update, delete on table public.reports to authenticated;
A table that clients only read, such as announcements a backend job writes, gets grant select and a single select policy. In the Data API settings you can also switch off Automatically expose new tables, and Supabase documents event triggers that enable RLS on every new table.
How do you write a Supabase RLS policy for each action?
Postgres accepts one command per for clause, and Supabase recommends a separate policy for select, insert, update and delete instead of one for all policy, so each rule shows which action it covers (Supabase: write a policy for each operation). These four policies let each user manage only their own profile row:
create table profiles (
id uuid primary key,
user_id uuid references auth.users,
avatar_url text
);
alter table profiles enable row level security;
revoke all on table profiles from anon, authenticated;
grant select, insert, update, delete on table profiles to authenticated;
Select: who can read a row
create policy "Users can view their own profile."
on profiles for select
to authenticated
using ( (select auth.uid()) = user_id );
Insert: what a new row may look like
with check validates the new row, so a user cannot create a row that belongs to someone else.
create policy "Users can create their own profile."
on profiles for insert
to authenticated
with check ( (select auth.uid()) = user_id );
Update: which rows, and what they may become
using picks the existing rows a user may change, and with check validates the result, which stops a user from reassigning user_id to someone else. An update also needs a matching select policy, or it will not work as expected.
create policy "Users can update their own profile."
on profiles for update
to authenticated
using ( (select auth.uid()) = user_id )
with check ( (select auth.uid()) = user_id );
Delete: which rows can be removed
create policy "Users can delete their own profile."
on profiles for delete
to authenticated
using ( (select auth.uid()) = user_id );
Four details from the same page prevent most policy bugs:
- Name the role. Write
to authenticatedorto anonon every policy, so the expression never runs for requests it was not meant for. - Wrap helper calls in a select.
(select auth.uid())is evaluated once per statement instead of once per row. Add an index on every column a policy filters on, such asuser_id. - Expect null for signed-out requests.
auth.uid()returns null when there is no signed-in user, andnull = user_idis never true, so the policy denies silently. - Never authorize on user metadata. Signed-in users can change
raw_user_meta_datathemselves. Keep roles and team IDs inraw_app_meta_data, which users cannot update (Supabase: auth.jwt()).
How do you write RLS policies for teams and shared records?
Shared data needs a membership check. The obvious version, where the policy on lists reads list_members and the policy on list_members reads lists, fails with error 42P17, infinite recursion. The documented fix is a security definer function in a schema the Data API does not expose (Supabase: avoid recursive policies):
create schema if not exists private;
create function private.user_list_ids()
returns setof uuid
language sql
security definer
set search_path = ''
stable
as $
select list_id from public.list_members
where user_id = (select auth.uid())
$;
revoke execute on function private.user_list_ids() from public;
grant usage on schema private to authenticated;
grant execute on function private.user_list_ids() to authenticated;
create policy "members read lists" on lists for select
to authenticated
using ( id in (select private.user_list_ids()) );
create policy "members read membership" on list_members for select
to authenticated
using ( list_id in (select private.user_list_ids()) );
Set search_path = '' on every security definer function and schema-qualify the names inside it. Never create one in an exposed schema such as public: it would be callable over the Data API with its owner's privileges (Supabase: security definer functions).
Row level security vs column level security: what is the difference?
RLS filters rows. Column level security limits which columns a role may read or change, and Postgres implements it with privileges instead of policies (Supabase: Column Level Security).
The two combine. RLS lets only the owner update a post, and a column privilege stops even the owner from changing fields you do not want edited from the client:
revoke update on table public.posts from authenticated;
grant update (title, content) on table public.posts to authenticated;
How do Supabase Storage RLS policies work?
Supabase Storage keeps file metadata in the storage.objects table, and access control is RLS on that table. Without policies, Storage allows no uploads at all (Supabase: Storage access control).
Buckets come in two kinds. Private buckets are the default, and every operation, downloads included, goes through your policies. A public bucket skips access control for reading, so anyone who has a file's URL can download it; uploads, deletes, moves and copies are still checked (Supabase: Storage buckets). Each client method needs specific permissions on storage.objects, as listed in the supabase-js Storage reference:
Table policies do not cover files. In one audit, every table had RLS switched on, and receipts on private records could still be downloaded by any member of the same account.
How do you write a Supabase storage RLS policy for user folders?
The pattern that holds up is one private bucket with one folder per user, named after the user's ID. The policies then compare the first folder in the file path with the caller, using the storage.foldername() helper (Supabase: Storage helper functions).
Step 1: create a private bucket. The dashboard works, or SQL (Supabase: creating buckets):
insert into storage.buckets (id, name, public)
values ('documents', 'documents', false);
Step 2: one policy per action, scoped to the bucket and the user's folder.
create policy "Users upload to their own folder"
on storage.objects for insert
to authenticated
with check (
bucket_id = 'documents' and
(storage.foldername(name))[1] = (select auth.jwt()->>'sub')
);
create policy "Users read their own files"
on storage.objects for select
to authenticated
using (
bucket_id = 'documents' and
(storage.foldername(name))[1] = (select auth.jwt()->>'sub')
);
create policy "Users update their own files"
on storage.objects for update
to authenticated
using (
bucket_id = 'documents' and
(storage.foldername(name))[1] = (select auth.jwt()->>'sub')
)
with check (
bucket_id = 'documents' and
(storage.foldername(name))[1] = (select auth.jwt()->>'sub')
);
create policy "Users delete their own files"
on storage.objects for delete
to authenticated
using (
bucket_id = 'documents' and
(storage.foldername(name))[1] = (select auth.jwt()->>'sub')
);
Step 3: upload into the user's folder and share through signed URLs.
const { data: { user } } = await supabase.auth.getUser()
const path = `${user.id}/invoice-2026-11.pdf`
const { error } = await supabase.storage
.from('documents')
.upload(path, file)
// A link that works for 10 minutes
const { data } = await supabase.storage
.from('documents')
.createSignedUrl(path, 600)
A signed URL cannot be revoked on its own. To cut access before it expires, delete, move or rename the file (Supabase: serving downloads). When files belong to a shared record instead of a single user, name the folder after the record ID and check membership in the policy with the same kind of security definer function you use for the table.
How do you test Supabase RLS policies?
The dashboard tells you RLS is on. Only a test tells you the policies do what you meant. Use three layers:
- The two-user test. Create two test users. Sign in as the first, note the ID of a private record and the path of a private file. Sign in as the second and request both directly through the API, then try to update and delete them. Every attempt should end in an error or an empty result, in lists and by direct ID.
- pgTAP tests in the repo. Create one test file per table with
supabase test new reports_rlsand run the suite withsupabase test db. Assert allow and deny foranonandauthenticatedon every action. A row filtered out byusingraises no error, so prove denied writes withreturningand an empty result, never withlives_ok(Supabase: testing RLS with pgTAP). - The advisors. Open the Security Advisor in the dashboard, or run
supabase db advisors --remoteagainst the linked project, and read every RLS finding (Supabase: Database Advisors).
These tests belong in the same review as the rest of the code. Our guide to auditing a vibe-coded app before it goes live shows where they fit.
What are the most common Supabase RLS mistakes?
- Views that bypass RLS. A view created by
postgresignores the policies of its tables. On Postgres 15 and later, create itwith (security_invoker = true)(Supabase: expose a view safely). - Policies without grants revoked. A table protected only by policies still gives
anonan insert path if the grant stays. - One
for allpolicy. It hides which action each condition was meant for, and update rules often end up withoutwith check. - A secret key in browser code. It skips every policy, whatever you wrote.
- A server client that carries a user session. A secret key bypasses RLS only when the request carries no user access token. An SSR client built from cookies sends the user's token, so its queries run under that user's policies (Supabase: bypassing RLS).
- Security definer functions in
public. They are callable over the Data API with the creator's rights.
What good looks like: a Supabase RLS checklist
What good looks like
- The Security Advisor lists no RLS errors.
- Every table in an exposed schema has RLS enabled and grants only the actions the app uses.
- Each action has its own policy, and each policy names a role.
- Update policies have
with check, and a select policy exists for every table users update. - User files sit in private buckets with folder policies; public buckets hold only public assets.
- A second test user gets an error or an empty result for every private record and file, by list and by direct ID.
- pgTAP tests for every table run with
supabase test db, and secret keys appear only in server code.
RLS is one line of a wider launch review. The production readiness checklist for vibe-coded apps covers the rest.
Want your Supabase policies checked by someone else?
Valletta Software runs a fixed-price vibe code audit, from $199, for apps built with AI tools. We read your source code, including migrations and policies, and we never touch production or customer data. The Supabase security audit covers RLS, Storage policies, key handling and the queries your frontend sends.
Supabase RLS FAQ
What is Supabase RLS?
Supabase RLS is Postgres Row Level Security applied to the tables your app reaches through the Supabase Data API. Each policy works like a WHERE clause the client cannot remove, so a signed-in user only sees and changes the rows the policy allows. It also protects Storage, because file access is RLS on the storage.objects table.
Does enabling RLS block all access to a table?
Yes, for client requests. Once RLS is enabled, a publishable or anon key gets no rows until a policy allows them, and every action needs its own policy. A secret key or legacy service_role key still bypasses RLS, so keep those keys on servers you control and out of browser code.
What is the difference between row level security and column level security?
Row level security decides which rows a role can read or change, using policies that can check the signed-in user. Column level security decides which columns a role can touch, using Postgres grants. Supabase calls column privileges an advanced feature and recommends RLS plus a dedicated roles table for most apps.
How do I write a Supabase storage RLS policy?
Create policies on storage.objects, one per action. For per-user files, put each file in a folder named after the user ID and compare the first folder with the caller, for example (storage.foldername(name))[1] = (select auth.jwt()->>'sub'). Uploads need insert, downloads and signed URLs need select, and deletes need select and delete.
Do public Supabase buckets need RLS policies?
For reading, no: anyone who has the URL of a file in a public bucket can download it, without any policy. Uploads, deletes, moves and copies are still checked against your policies. Keep user files in private buckets and use public buckets only for assets you would publish anyway, such as product images.
Why does my RLS policy return an empty result instead of an error?
That is how Postgres reports a row filtered out by a using clause: the query matches zero rows and raises nothing. A missing grant or a failed with check raises error 42501 instead. When a request your policy should allow fails, check the table grants first, then the policy.