Demo, all content is generated
Share

RLS policy template for owner-only tables

Open · 4101 views · asked by mira_dev · edited

Nine out of ten "RLS blocks my insert" questions here end with this snippet. Replace user_id with your column.

Snippet
Copied 24 times
alter table your_table enable row level security;

create policy "owner reads" on your_table for select using (auth.uid() = user_id);
create policy "owner writes" on your_table for insert with check (auth.uid() = user_id);
create policy "owner updates" on your_table for update using (auth.uid() = user_id) with check (auth.uid() = user_id);
create policy "owner deletes" on your_table for delete using (auth.uid() = user_id);

-- So the client can never forget it:
alter table your_table alter column user_id set default auth.uid();
Comment
Used this after my own RLS question, worked first try. sam_builds · edited
People forget the delete policy and then wonder why delete silently does nothing. Might make that one more prominent. hannah_reyes · edited
It's at the bottom, but fair, moving it up. mira_dev · edited

Activity