Supabase row-level security for a public, read-only table

Abstract

How to let anyone read published rows and nothing else: enable RLS, add one select policy, revoke the default grants, and verify the result with a query.

A public website that reads from a database needs a rule: visitors may read published content, and that is all. Supabase gives you the tools for this in Postgres itself, row-level security (RLS) and grants. This tutorial walks through the setup this site uses for its posts table and shows how to verify it.

The SQL below is abridged from this site’s migrations (some columns, an index and a trigger are left out). The verification query was run against the live database on 23 October 2026 and its real result is shown.

Two layers

  • Grants decide which operations a role may attempt on a table at all (select, insert, update, delete and others).
  • Row-level security decides which rows a role may see or change, using policies.

Use both. Each covers a mistake in the other.

Step 1: the table (abridged)

create table public.posts (
  id uuid primary key default gen_random_uuid(),
  slug text not null unique check (slug ~ '^[a-z0-9]+(-[a-z0-9]+)*$'),
  kind text not null check (kind in ('paper', 'post')),
  title text not null,
  abstract text not null,
  body_md text not null,
  tags text[] not null default '{}',
  published boolean not null default false,
  published_at timestamptz,
  updated_at timestamptz not null default now()
);

The published column is what the policy will key on. The check constraint on slug keeps bad values out at the database level, not only in application code.

Step 2: turn on RLS and add one policy

alter table public.posts enable row level security;

create policy "public read published" on public.posts
  for select to anon, authenticated using (published = true);

With RLS enabled and no policy, nobody using the public API sees any rows. This policy allows select for the two public roles, anon and authenticated, but only on rows where published is true. Drafts stay invisible.

Step 3: remove the default privileges

Supabase grants broad table privileges to the public roles by default. A policy alone does not take them away. The second migration on this site does:

revoke all on public.posts from anon, authenticated;
grant select on public.posts to anon, authenticated;

Now the public roles can select and nothing else. The comments in the migration explain why: an earlier revoke of insert, update and delete had left other privileges (truncate, trigger, references) in place.

Step 4: verify, do not assume

Query the catalog and read what is actually there:

select 'grant' as kind, grantee as who, privilege_type as what
from information_schema.role_table_grants
where table_schema='public' and table_name='posts' and grantee in ('anon','authenticated')
union all
select 'policy', policyname, cmd || ' | ' || array_to_string(roles, ',') || ' | ' || coalesce(qual,'')
from pg_policies where schemaname='public' and tablename='posts'
union all
select 'rls', 'enabled', relrowsecurity::text from pg_class where oid='public.posts'::regclass
order by 1,2,3;

The live result for this site:

grant  | anon                   | SELECT
grant  | authenticated          | SELECT
policy | public read published  | SELECT | anon,authenticated | (published = true)
rls    | enabled                | true

Two select grants, one select policy restricted to published rows, and RLS enabled. That is the whole public surface.

How writes happen

Public roles cannot write, so content changes come from somewhere trusted: the Supabase dashboard, a migration, or an admin tool with credentials the website never sees. In this site’s case the Worker only ever holds the public read key. Even if that key leaked, it could only read what the policy exposes, which is already public.

A checklist

  1. Enable RLS on every table in an exposed schema.
  2. Write the narrowest policy you can, naming the roles and the operation.
  3. Revoke default grants you do not need, then grant back only what you do.
  4. Query the catalog to confirm. Re-run it after each migration.
  5. Check Supabase’s built-in advisors for tables with RLS disabled or overly broad access.

Conclusion

Public read-only is three statements and one verification query. The verification is the step people skip, and the one that tells you the other three did what you meant.[1] The client tutorial shows the read side of the same setup.


  1. Reviewer 2 asks whether the author tried to write to the table as an anonymous user. The author’s answer is that the catalog says no, which is a more polite way of saying the same thing. ↩︎