You create a new table, add RLS policies that look perfect, and your API calls fail with:
error code 42501: permission denied for table profiles
No policy is wrong. The problem is one layer below RLS, and Supabase’s recent default change is why it suddenly bites. Here is what changed, the exact SQL that fixes it, and a two-user test to prove your policies still hold.
What changed
Supabase no longer automatically grants the API roles (anon, authenticated) access to new tables in the public schema. New projects got this first, and for every project it applies to tables created after Oct 30. Before, a new table was reachable by default and RLS did the filtering. Now you have to grant access explicitly.
Check the exact rollout details in Supabase’s changelog and docs: [LINK TO SUPABASE CHANGELOG/DOCS].
The thing to remember is that there are two separate layers:
• Grants decide whether a role can touch the table at all.
• RLS policies decide which rows that role can see or change.
You need both. A policy without a grant gives you 42501. A grant without RLS gives you a table anyone can read.
The fix
Grant only what each role needs, and nothing more:
-- Block anonymous access completely
revoke all on table public.profiles from anon;
-- Logged-in users can read and update (RLS limits which rows)
grant select, update on table public.profiles to authenticated;
-- Your server-side code (service role key) gets full access
grant select, insert, update, delete on table public.profiles to service_role;
Notice what is missing: authenticated has no insert or delete. That is on purpose, see the trigger section below.
Add owner-only RLS policies
Enable RLS, then write policies so users only touch their own row:
alter table public.profiles enable row level security;
create policy "Users can view own profile"
on public.profiles for select
to authenticated
using ( auth.uid() = id );
create policy "Users can update own profile"
on public.profiles for update
to authenticated
using ( auth.uid() = id )
with check ( auth.uid() = id );
The with check on the update policy matters. Without it, a user could update their row and change id to someone else’s. using filters which rows can be targeted, with check validates the row after the change.
Create profiles with a trigger, not from the client
There is no insert policy above. Clients can’t create profile rows at all. Instead, a trigger creates the row when a user signs up:
create or replace function public.handle_new_user()
returns trigger as $$
begin
insert into public.profiles (id, email, full_name, avatar_url)
values (
new.id,
new.email,
new.raw_user_meta_data->>'full_name',
new.raw_user_meta_data->>'avatar_url'
);
return new;
end;
$$ language plpgsql security definer set search_path = public;
create trigger on_auth_user_created
after insert on auth.users
for each row
execute procedure public.handle_new_user();
security definer runs the function with its owner’s permissions, so it can insert even though authenticated can’t. Setting search_path on a security definer function is important, otherwise it can be tricked into running objects from a schema you didn’t intend.
Test it with two users
Don’t trust a policy because it looks right. Create two users, sign in as each, and try to break in:
// userB is a supabase-js client signed in as User B
// userA.id is User A's id
// 1. B tries to read A's profile -> expect an empty array
const { data: read } = await userB
.from('profiles')
.select('*')
.eq('id', userA.id);
// 2. B tries to edit A's profile -> expect an empty array (no rows affected)
const { data: write } = await userB
.from('profiles')
.update({ full_name: 'hacked' })
.eq('id', userA.id)
.select();
If either call returns A’s row, your policy is too open. If you get 42501 on your own row, you are missing a grant. Both are easy to catch here and painful to catch in production.
Common mistakes
• Granting to anon “just to make it work.” Combined with a loose or missing policy, anyone on the internet can read the table. Grant to authenticated and write real policies instead.
• A policy that only says to authenticated with using (true). Any signed-in user reads every row. The owner check (auth.uid() = id) belongs in the policy.
• Forgetting with check on update. Users can reassign rows.
• Using the service role key in client code. It bypasses RLS entirely. Keep it server-side only.
Want the setup already done?
I built an Express + Supabase starter that ships with the grants, owner-only RLS, the signup trigger, and a one-command two-user test that prints 4 PASS lines. Disclosure: I’m the author, and it’s a paid starter, not an audit. It covers the profiles table. If you add your own tables, you’ll need to write and test their policies too.
https://shaameer.gumroad.com/l/SupabaseExpressjsProductionBackend/Launch10
Either way, the SQL above is free to copy. If it saved you an hour, share it with someone who is staring at a 42501.
Top comments (0)