Database Security Row Level Security
Row Level Security
Secure your data with Postgres policies. RLS decides which rows each user can read and write, right in the database.
Enable RLS on a table
RLS is on by default for tables created in the dashboard. For tables created in SQL, enable it yourself.
sql
alter table todos enable row level security;
Write a policy
Policies are SQL rules attached to a table. These let users read and insert only their own todos.
Restrict who can read and write
Use with check to validate new rows before they are written.
policies.sql
create policy "Users read own todos" on todos for select to authenticated using ( (select auth.uid()) = user_id ); create policy "Users insert own todos" on todos for insert to authenticated with check ( (select auth.uid()) = user_id );
Test your policies
Try each policy as a real role in the SQL playground before you ship.
sql
set role authenticated; select * from todos; -- only your rows
Service role keys bypass RLS. Never expose them in the browser.