Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

docs(database): add access control to the tables guide (#50024)

Miranda Limonczenkoef3cfad9edf7
diff --git a/apps/docs/content/guides/database/tables.mdx b/apps/docs/content/guides/database/tables.mdx
index cbfa85a0d2f..e8da0a6631e 100644
--- a/apps/docs/content/guides/database/tables.mdx
+++ b/apps/docs/content/guides/database/tables.mdx
@@ -102,13 +102,98 @@ When naming tables, use lowercase and underscores instead of spaces. For example
</Admonition>
-You now have a table with its columns defined. To put rows in it, continue to [Loading data](#loading-data).
+You now have a table with its columns defined. Before you put rows in it, protect it.
+
+### Securing your tables
+
+A table in the `public` schema is reachable through the Data API. Until you enable row level security and write a policy, anyone holding your project's publishable key can read and write every row in it.
+
+The Table Editor enables row level security for you when you create a table in the Dashboard. When you create a table with SQL, enable it yourself.
+
+#### Enabling row level security
+
+1. Enable row level security on the table:
+
+ ```sql
+ alter table movies enable row level security;
+ ```
+
+2. Add a policy that describes who can read the table. Until one exists, Data API requests return no rows. The table's owner and roles with `BYPASSRLS` aren't subject to policies, which is why the same query still returns rows in the SQL editor:
+
+ ```sql
+ create policy "Anyone can read movies"
+ on movies for select
+ to anon, authenticated
+ using ( true );
+ ```
+
+A policy decides which rows a role reaches, not whether it holds privileges on the table. The Data API roles carry the grants they need by default, so a request that fails with `permission denied for table` points at a revoked grant rather than a missing policy. See [Securing your API](/docs/guides/api/securing-your-api).
+
+For insert, update, and delete policies, and for how policies are evaluated, see [Row Level Security](/docs/guides/database/postgres/row-level-security).
+
+#### Tables with different readers
+
+Most applications mix two kinds of table: shared data that everyone reads, and per-person data that only its owner reads. Each kind needs its own policy, and the shared one is the easiest to forget.
+
+`movies` is the shared kind. The policy above lets anyone browse it, signed in or not.
+
+The `watchlists` table is the other kind. Each row belongs to the person who created it, and only that person can read it:
+
+```sql
+create table watchlists (
+ id bigint generated always as identity primary key,
+ user_id uuid not null references auth.users default auth.uid(),
+ movie_id bigint not null references movies
+);
+
+alter table watchlists enable row level security;
+
+create policy "Users can read their own watchlist"
+on watchlists for select
+to authenticated
+using ( (select auth.uid()) = user_id );
+
+create policy "Users can add to their own watchlist"
+on watchlists for insert
+to authenticated
+with check ( (select auth.uid()) = user_id );
+```
+
+Give both tables a policy, even when one of them is `using ( true )`. A shared table with row level security enabled and no policy is as unreachable as a private one.
+
+#### Verifying your tables
+
+Confirm that every table exists and is protected before you build against it.
+
+1. List the tables in the `public` schema and whether row level security is enabled on each one:
+
+ ```sql
+ select tablename, rowsecurity
+ from pg_tables
+ where schemaname = 'public'
+ order by tablename;
+ ```
+
+2. Check that every table you meant to create appears in the results, and that `rowsecurity` is `true` for each one.
+
+3. List the policies on those tables:
+
+ ```sql
+ select tablename, policyname, cmd, roles
+ from pg_policies
+ where schemaname = 'public'
+ order by tablename, policyname;
+ ```
+
+4. Check that every table has at least one policy, and that any table meant to be readable by signed-out visitors lists `anon` among its roles.
### Loading data
There are several ways to load data in Supabase. You can load data directly into the database, or use the [Data API](/docs/guides/api).
If you're loading large data sets, follow the [bulk data loading](#bulk-data-loading) instructions.
+The read-only policy from [Securing your tables](#securing-your-tables) rejects inserts through the Data API. Run the client examples below against a table that has an insert policy for the role you're using, or load the data over a direct connection instead.
+
#### Basic data loading
<Tabs