RLS insert still failing for anon role even with correct policy
I’m trying to submit a booking form from my frontend to a Supabase table bookings with RLS enabled. I’ve created the following policy:
which grants:
pg_policies confirms the policy is PERMISSIVE and exists for the anon role, and my call to a debug_auth() function shows:
So I KNOW I'm using the anon role. this is what the frontend code to insert looks like:
example contents of bookingData (to be submitted:
and finally the issue i am facing when trying to submit:
ANY help would be appreciated. It should be noted that this all works if i disable RLS, but I dont think thats a good idea for many reasons.
19 Replies
Check the API Gateway log details and see if you are really anon, versus authenticated.
And is that your real code for insert? You don't have a .select() on it?
I did have a select on it, I just removed it for the sake of testing (apparently that might have been a problem as i didnt grant select permissions.) didnt help though.
as for the logs, here is one such failure message (i dont really know whats sensitive but this is a test anyway

You are anon.
Turn off RLS on the table and see if it works.
Then add in a select policy.
Any other RLS policies on the table?
Permissive is the correct type and your With Check looks fine.
Turning off RLS does work, I'm able to upload stuff directly to the database (scary) without problems.
Going to try the select policy right now
select policy i added
and...
OH
it seems to work now! no way that was the only issue...
Yeah, but it says you have a .single() or .select() on the insert if that is limiting you.
Well it doesnt seem to be, it seems that adding that select policy was all thats needed because now i have it going cleanly into my database!
thank you so so so much for your rapid response my guy, you saved my night you have no idea
Did you turn back on RLS
yes!
Then your code that is running has a .select() in it.
aye, now it does, I added it back to test it out. perhaps it did before, i changed the insert statement so many times what was shown here was my last resort
i may have been running it elsewhere. should anon not have access to select?
im sure there are safer ways to set this up, but i think for a public booking form this might be fine
That is up to how you want to do things.
I would not have anon insert or select.
how would you suggest this be handled? if youve any resources i would gladly read them
you have helped quite a bit already!
So you have a booking form that does not require log in?
yes
If so at a minimum don't do the .select() in your client and don't turn on select RLS. With that on anyone can see all the booking table entries.
oh dear yeah that would be pretty bad...
You could also consider doing an edge function or rpc call to a postgres function that allows anon users to insert and yet you can still protect the tables. With edge functions you could even rate limit. You could also track IP and only allow one booking per IP with function checks. But this all gets complicated.
but certainly necessary. I dont feel too comfortable with the current setup if its possible to read all the values publically. This however is a good starting point, I will look into this right now. theres always more to learn... Thanks again for your help!
NICE I set up an edge functino that actually seems to work!. deleted all the previous unsafe policies to accomodate it, thank you for the idea!