If your app was built with Lovable or Bolt on Supabase, its data is protected (or not) by Row Level Security policies. The AI tool usually writes them for you. This guide is a short reference so you can read those policies, tell whether they do what you expect, and fix the common gaps. It covers what RLS is, how grants and policies fit together, the difference between USING and WITH CHECK, templates for the table shapes these tools generate, the mistakes to look for, and how to test.
If you’re here because of a specific error, see fixing “new row violates row-level security policy” instead.
RLS in plain English
In a Supabase app, the browser talks to the database through Supabase’s API using a public key. Anyone can see that key in your app’s code. What stops one user reading another user’s data is RLS: rules attached to each table that decide, row by row, who can read or change what.
Supabase’s own RLS Simplified note describes a policy as a WHERE or CHECK condition applied automatically to database queries, for specific actions and specific roles.
The warning to take seriously is in Supabase’s RLS guide: a table in an exposed schema without RLS “is readable and writable by any role with a grant on it.” That’s why Supabase’s database advisors flag rls_disabled_in_public. Enable RLS on every table in the public schema.
In Lovable, you can review policies under More → Database → Manage → Policies. Lovable’s database docs say that view is read-only, and you change a policy by asking Lovable in the chat.
Grants vs policies
Postgres checks two things before a request touches a table, and Supabase’s docs are explicit that you need both:
- Grants decide whether a role (such as
anonorauthenticated) can run an operation on the table at all. - Policies decide which rows that operation applies to.
Two details from the current Supabase docs:
- On existing projects, new tables in
publicget every privilege granted toanon,authenticatedandservice_roleautomatically. Not every project does this, so check yours. - “Adding policies doesn’t take those grants back.” If you never revoke an insert grant from
anon, a mistakenanonpolicy can still let visitors write.
Supabase’s RLS guide shows how to revoke all grants from the client roles and grant back only what each needs. A missing grant raises error 42501 before any policy runs.
The roles you’ll see in policies:
anon: a request from someone who isn’t signed in.authenticated: a signed-in user. Supabase notes that its anonymous sign-in users also use this role.service_role: full access that “bypasses RLS, so keep it server-side.”
USING vs WITH CHECK, one operation at a time
A policy has up to two conditions:
USINGchecks existing rows: which rows the user can see, update or delete. Rows that fail are simply left out. PostgreSQL’s CREATE POLICY docs say no error usually occurs.WITH CHECKchecks new or changed rows: whether the row being written is allowed. Rows that fail cause an error.
How that maps to each operation:
| Operation | Uses | What it checks |
|---|---|---|
SELECT | USING | Which rows the user can read |
INSERT | WITH CHECK | Whether the new row is allowed |
UPDATE | USING and WITH CHECK | Which rows can be changed, and what they may look like afterwards |
DELETE | USING | Which rows can be removed |
Three rules that catch people out:
- UPDATE needs a SELECT policy too. Supabase’s docs: “To perform an
UPDATEoperation, a correspondingSELECTpolicy is required.” - Without
WITH CHECK,USINGcovers both. Supabase says that if nowith checkexpression is defined, theusingexpression decides both which rows are visible and which new rows are allowed. - Reading back a written row needs SELECT. If a write returns the row (for example
.insert(...).select()in supabase-js), PostgreSQL requires the new row to pass your SELECT policies too.
Supabase also recommends one policy per operation rather than for all, because a for all policy “hides which operation each rule was meant to cover.” Postgres doesn’t accept a list of operations in one for clause.
Copy-paste: owner-only SELECT, INSERT, UPDATE, DELETE
For a table where each row belongs to one user through a user_id column (orders, notes, tasks, invoices). These follow the patterns in Supabase’s docs. Replace orders and check that your owner column really is user_id.
alter table public.orders enable row level security;
create policy "orders: owner can read"
on public.orders for select
to authenticated
using ( (select auth.uid()) = user_id );
create policy "orders: owner can insert"
on public.orders for insert
to authenticated
with check ( (select auth.uid()) = user_id );
create policy "orders: owner can update"
on public.orders for update
to authenticated
using ( (select auth.uid()) = user_id )
with check ( (select auth.uid()) = user_id );
create policy "orders: owner can delete"
on public.orders for delete
to authenticated
using ( (select auth.uid()) = user_id );
Why (select auth.uid()) rather than auth.uid(): Supabase’s docs say wrapping the function lets Postgres run it once and reuse the result, rather than calling it for every row. They also recommend an index on any column a policy filters on:
create index orders_user_id_idx on public.orders using btree (user_id);
Skip the policies for operations users shouldn’t perform. For example, if orders must never be deleted by users, leave out the delete policy. No policy means no access.
Copy-paste: public read, owner write
For content anyone can view but only the author can change, such as posts, listings or public profiles:
alter table public.posts enable row level security;
create policy "posts: anyone can read"
on public.posts for select
to anon, authenticated
using ( true );
create policy "posts: author can insert"
on public.posts for insert
to authenticated
with check ( (select auth.uid()) = user_id );
create policy "posts: author can update"
on public.posts for update
to authenticated
using ( (select auth.uid()) = user_id )
with check ( (select auth.uid()) = user_id );
create policy "posts: author can delete"
on public.posts for delete
to authenticated
using ( (select auth.uid()) = user_id );
using ( true ) for reading is only right because this data is meant to be public. Supabase’s docs warn that a policy reading to anon using ( true ) gives every signed-out visitor access to every row, so use it only for data that is meant to be public. If you only want unpublished drafts hidden, add a condition such as published = true or (select auth.uid()) = user_id.
Copy-paste: a profiles table linked to auth.users
Lovable and Bolt often create a profiles table with one row per user. Check which column holds the user’s ID. Sometimes it’s user_id, sometimes the row’s id is the user’s ID itself. Adjust the comparison to match:
alter table public.profiles enable row level security;
-- If profiles.id is the user's ID, compare against id.
-- If there is a separate user_id column, compare against user_id instead.
create policy "profiles: user can read own"
on public.profiles for select
to authenticated
using ( (select auth.uid()) = id );
create policy "profiles: user can insert own"
on public.profiles for insert
to authenticated
with check ( (select auth.uid()) = id );
create policy "profiles: user can update own"
on public.profiles for update
to authenticated
using ( (select auth.uid()) = id )
with check ( (select auth.uid()) = id );
If other users need to see some profile fields (display names, avatars), don’t loosen this policy. Put the public fields in a separate table or view with its own read policy. If you use a view, Supabase notes that views bypass RLS by default, and that on Postgres 15 and later you can create them with (security_invoker = true) so they follow the underlying table’s policies.
Storage bucket policies by user folder
File uploads are controlled by policies on the storage.objects table. Supabase’s Storage access control docs say Storage allows no uploads “without RLS policies”, and give this pattern for letting users upload only into a folder named after their user ID:
create policy "uploads: user's own folder"
on storage.objects for insert
to authenticated
with check (
bucket_id = 'my_bucket_id' and
(storage.foldername(name))[1] = (select auth.jwt()->>'sub')
);
Your app then has to upload to paths like <user-id>/file.png. For a plain upload, the INSERT policy is the only one needed. To overwrite files with upsert, the docs say you also need SELECT and UPDATE policies. Add a matching SELECT policy if users should be able to download their own private files.
Common AI-generated mistakes
When you read the policies your AI tool wrote, look for these:
using (true)orwith check (true)on private data. Anyone the policy covers can read or write every row. Fine for genuinely public content, wrong for orders, messages or anything personal.auth.uid() is not nullon its own. That means “any signed-in user”, not “the owner”. Every user can see every row.- Policies for
anonon private tables. Often added while testing signed out, just to stop an error. - No
toclause. Supabase recommends always naming the role a policy applies to. user_idset from the client with nothing checking it. If there’s noWITH CHECKcomparing it toauth.uid(), a user can create rows that claim to belong to someone else.- UPDATE with
USINGbut a missing or looseWITH CHECK. A user can edit their own row and change itsuser_idto someone else’s. - Policies that read
user_metadata. Supabase warns thatraw_user_meta_data“can be updated by the authenticated user”, so it isn’t a safe place for roles or permissions. Useapp_metadataor a table only admins can change, such as the roles table in our Lovable admin panel guide. - RLS disabled “temporarily”. Check the advisors for
rls_disabled_in_public,policy_exists_rls_disabledandrls_enabled_no_policy.
How to test the policies
Reading policies isn’t enough. Test them.
- Signed out. Open the app in a private window without signing in, and try to load data that should be private. Signed-out requests run as
anon. Supabase notes thatauth.uid()returnsnullfor them, so owner policies return nothing. - A second account. Sign in as user A and create something. Sign in as user B and try to read, edit or delete A’s record, for example by changing an ID in the URL or request. B should be refused or see nothing.
supabase test db. Supabase’s RLS guide includes a complete pgTAP test file per table. It switches role withset local roleand user withset local request.jwt.claim.sub, then asserts allow and deny for each operation. If you have the Supabase CLI and a repo, this makes the checks repeatable.
For a wider pre-launch review, including grants, secrets and storage, see our vibe-coded app security checklist.
Paste-in policies cover simple, one-owner tables. Teams, shared records, admin roles and multi-tenant data need more careful design. inahei.pro pairs you with vetted senior engineers who review and fix AI-built apps and show you what went wrong. Fixed price, agreed upfront. Experts sign NDAs and use scoped tokens and collaborator invites, never shared passwords. See /fix/supabase, /fix/lovable or /fix/bolt.
FAQ
Do I need RLS if my app has a login?
Yes. A login screen controls who uses your interface, not who can call your database. Your public key is in the app’s code, so anyone can query the API directly. RLS is what limits what they get back.
What does “RLS disabled in public” mean in Supabase?
It’s a database advisor warning that a table in the public schema has RLS switched off. Supabase says such a table is readable and writable by any role with a grant on it. Enable RLS and add the policies the table needs.
What is the difference between USING and WITH CHECK?
USING filters existing rows: which ones a user can see, update or delete. WITH CHECK validates rows being written: whether a new or updated row is allowed. Inserts use WITH CHECK, reads and deletes use USING, and updates use both.
Is using (true) ever OK?
For reading data that is genuinely public, such as published posts or a product catalogue, yes. For writes, or for anything private, no.
Does the service_role key bypass RLS?
Yes. Supabase says it has full access and bypasses RLS, so keep it on the server only. Code that uses it has to do its own ownership checks.
Comments