Skip to content

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

Add auto-enable RLS example to rls docs instead (#42493)

Inian84ccde68c259
diff --git a/apps/docs/content/guides/database/postgres/event-triggers.mdx b/apps/docs/content/guides/database/postgres/event-triggers.mdx
index 924795093bc..78da36e91e9 100644
--- a/apps/docs/content/guides/database/postgres/event-triggers.mdx
+++ b/apps/docs/content/guides/database/postgres/event-triggers.mdx
@@ -55,43 +55,7 @@ EXECUTE FUNCTION dont_drop_function();
### Example trigger function - auto enable Row Level Security
-```sql
-CREATE OR REPLACE FUNCTION rls_auto_enable()
-RETURNS EVENT_TRIGGER
-LANGUAGE plpgsql
-SECURITY DEFINER
-SET search_path = pg_catalog
-AS $$
-DECLARE
- cmd record;
-BEGIN
- FOR cmd IN
- SELECT *
- FROM pg_event_trigger_ddl_commands()
- WHERE command_tag IN ('CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO')
- AND object_type IN ('table','partitioned table')
- LOOP
- IF cmd.schema_name IS NOT NULL AND cmd.schema_name IN ('public') AND cmd.schema_name NOT IN ('pg_catalog','information_schema') AND cmd.schema_name NOT LIKE 'pg_toast%' AND cmd.schema_name NOT LIKE 'pg_temp%' THEN
- BEGIN
- EXECUTE format('alter table if exists %s enable row level security', cmd.object_identity);
- RAISE LOG 'rls_auto_enable: enabled RLS on %', cmd.object_identity;
- EXCEPTION
- WHEN OTHERS THEN
- RAISE LOG 'rls_auto_enable: failed to enable RLS on %', cmd.object_identity;
- END;
- ELSE
- RAISE LOG 'rls_auto_enable: skip % (either system schema or not in enforced list: %.)', cmd.object_identity, cmd.schema_name;
- END IF;
- END LOOP;
-END;
-$$;
-
-DROP EVENT TRIGGER IF EXISTS ensure_rls;
-CREATE EVENT TRIGGER ensure_rls
-ON ddl_command_end
-WHEN TAG IN ('CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO')
-EXECUTE FUNCTION rls_auto_enable();
-```
+See how to [auto enable RLS for new tables](/docs/guides/database/postgres/row-level-security#auto-enable-rls-for-new-tables).
### Event trigger Functions and firing events
diff --git a/apps/docs/content/guides/database/postgres/row-level-security.mdx b/apps/docs/content/guides/database/postgres/row-level-security.mdx
index ce0c5a8b58c..387bbcabc40 100644
--- a/apps/docs/content/guides/database/postgres/row-level-security.mdx
+++ b/apps/docs/content/guides/database/postgres/row-level-security.mdx
@@ -59,6 +59,50 @@ alter table "table_name" enable row level security;
Once you have enabled RLS, no data will be accessible via the [API](/docs/guides/api) when using the public `anon` key, until you create policies.
+## Auto-enable RLS for new tables
+
+If you want RLS enabled automatically for new tables, you can create an event trigger that runs after table creation. This uses a Postgres [event trigger](/docs/guides/database/postgres/event-triggers) to call `ALTER TABLE ... ENABLE ROW LEVEL SECURITY` on each newly created table.
+
+```sql
+CREATE OR REPLACE FUNCTION rls_auto_enable()
+RETURNS EVENT_TRIGGER
+LANGUAGE plpgsql
+SECURITY DEFINER
+SET search_path = pg_catalog
+AS $$
+DECLARE
+ cmd record;
+BEGIN
+ FOR cmd IN
+ SELECT *
+ FROM pg_event_trigger_ddl_commands()
+ WHERE command_tag IN ('CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO')
+ AND object_type IN ('table','partitioned table')
+ LOOP
+ IF cmd.schema_name IS NOT NULL AND cmd.schema_name IN ('public') AND cmd.schema_name NOT IN ('pg_catalog','information_schema') AND cmd.schema_name NOT LIKE 'pg_toast%' AND cmd.schema_name NOT LIKE 'pg_temp%' THEN
+ BEGIN
+ EXECUTE format('alter table if exists %s enable row level security', cmd.object_identity);
+ RAISE LOG 'rls_auto_enable: enabled RLS on %', cmd.object_identity;
+ EXCEPTION
+ WHEN OTHERS THEN
+ RAISE LOG 'rls_auto_enable: failed to enable RLS on %', cmd.object_identity;
+ END;
+ ELSE
+ RAISE LOG 'rls_auto_enable: skip % (either system schema or not in enforced list: %.)', cmd.object_identity, cmd.schema_name;
+ END IF;
+ END LOOP;
+END;
+$$;
+
+DROP EVENT TRIGGER IF EXISTS ensure_rls;
+CREATE EVENT TRIGGER ensure_rls
+ON ddl_command_end
+WHEN TAG IN ('CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO')
+EXECUTE FUNCTION rls_auto_enable();
+```
+
+Note that this applies to tables created after the trigger is installed. Existing tables still need RLS enabled manually.
+
<Admonition type="caution" label="`auth.uid()` Returns `null` When Unauthenticated">
When a request is made without an authenticated user (e.g., no access token is provided or the session has expired), `auth.uid()` returns `null`.