Declarative Database Schema - Views do NOT apply 'with(security_invoker=true)'
Bug report
- [X] I confirm this is a bug with Supabase, not with my own application.
- [X] I confirm I have searched the Docs, GitHub Discussions, and Discord.
Describe the bug
Using a declarative database schema to create a view with a 'security_invoker=true' permission setting, the security is not carried over into the migration file. Which makes using declarative schema's less useful and leaves Supabase environment open to permission / security issues.
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Create file './schemas/view.sql'
- Add the following:
-- Create a private schema
CREATE SCHEMA "private" AUTHORIZATION service_role;
GRANT USAGE ON SCHEMA "private" TO service_role;
GRANT USAGE ON SCHEMA "private" TO authenticated;
GRANT ALL ON ALL TABLES IN SCHEMA "private" TO authenticated, service_role;
GRANT ALL ON ALL ROUTINES IN SCHEMA "private" TO authenticated, service_role;
GRANT ALL ON ALL SEQUENCES IN SCHEMA "private" TO authenticated, service_role;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA "private" GRANT ALL ON TABLES TO authenticated, service_role;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA "private" GRANT ALL ON ROUTINES TO authenticated, service_role;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA "private" GRANT ALL ON SEQUENCES TO authenticated, service_role;
ALTER ROLE authenticator SET pgrst.db_schemas TO 'public, graphql, private';
-- Create a private table
create table "private"."users" (
"id" uuid,
"created_at" timestamp with time zone not null default now(),
"created_by" uuid not null default auth.uid(),
"updated_at" timestamp with time zone,
"updated_by" uuid,
"deleted_at" timestamp with time zone,
"deleted_by" uuid,
"is_deleted" boolean not null default false,
"version" smallint not null default 1,
"name" text,
"detail" jsonb
);
COMMENT ON TABLE "private"."users" IS 'User profile table.';
-- Create view
create or replace view "public"."user_details" with(security_invoker=true)
as
select
users.id,
users.name as name
from
"private".users;
- Generate the migration sql using:
supabase db diff -f initial_setup
- You should now have a migrations file in the "./migrations" folder. Open the file and you see:
create or replace view "public"."user_details" as SELECT id,
created_at,
is_deleted,
name,
detail
FROM private.users
WHERE (is_deleted = false);
Expected behavior
The view should be defined as:
create or replace view "public"."user_details" with(security_invoker=true) as SELECT id,
created_at,
is_deleted,
name,
detail
FROM private.users
WHERE (is_deleted = false);
System information
- OS: macOS
- Version of supabase-js: 2.33.9
- Version of Node.js: 22.14.0
Additional context
Declarative schemas don't work for 'schema' definitions or 'permissions' consistently.
If I put the below into the declarative schema file - it does not translate into a migration file correctly. It should copy exactly when a db is reset, and empty. But it condenses all of this into a single 'if' statement.
Something isn't working quite right as a result.
CREATE SCHEMA "private" AUTHORIZATION service_role;
GRANT USAGE ON SCHEMA "private" TO service_role;
GRANT USAGE ON SCHEMA "private" TO authenticated;
GRANT ALL ON ALL TABLES IN SCHEMA "private" TO authenticated, service_role;
GRANT ALL ON ALL ROUTINES IN SCHEMA "private" TO authenticated, service_role;
GRANT ALL ON ALL SEQUENCES IN SCHEMA "private" TO authenticated, service_role;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA "private" GRANT ALL ON TABLES TO authenticated, service_role;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA "private" GRANT ALL ON ROUTINES TO authenticated, service_role;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA "private" GRANT ALL ON SEQUENCES TO authenticated, service_role;
ALTER ROLE authenticator SET pgrst.db_schemas TO 'public, graphql, private';
I experience a similar issue with security_invoker.
Migration fails for the following SQL:
create or replace view categories_view with (security_invoker = on) as
SELECT c.slug,
parent.slug AS parent_slug,
c.icon_filename,
c.display_order,
ct.locale,
ct.translation
FROM categories c
LEFT JOIN categories parent ON c.parent_id = parent.id
JOIN category_translations ct ON ct.category_id = c.id;
Using Turborepo, I have created a Supabase folder and initiated Supabase in it using pnpm supabase init.
Then I added migrations and when I run pnpm supabase start I get:
ERROR: unrecognized parameter "security_invoker" (SQLSTATE 22023)
At statement: 0
create or replace view categories_view with (security_invoker = on) as
SELECT c.slug,
parent.slug AS parent_slug,
c.icon_filename,
c.display_order,
ct.locale,
ct.translation
FROM categories c
LEFT JOIN categories parent ON c.parent_id = parent.id
JOIN category_translations ct ON ct.category_id = c.id
Try rerunning the command with --debug to troubleshoot the error.
I will close this as a duplicate https://github.com/supabase/cli/issues/3973
This is issue 3973.
What is it a duplicate of?
My bad. It's something from 3 years ago https://github.com/supabase/cli/issues/792
I've observed the same (that a view with security_invoker=true doesn't keep that property from diff tool). Discovered that when trying to solve a security error about a VIEW being created with SECURITY DEFINER. I noticed that this one liner to grant my authenticated role access to read from the view results in Supabase telling me the VIEW has SECURITY DEFINER: GRANT SELECT ON app.team_details TO authenticated;
If I cut that grant, then there is no security error in Supabase. But without it, my authenticated users can't read from this view.
This looks like the same risky failure mode as the older db-diff thread, but the declarative-schema path deserves its own regression target because the generated migration can silently change the view's RLS behavior.
The checks I would want before closing this:
- A declarative schema with
create view ... with (security_invoker = true)generates migration SQL that preserves the view option. - Re-running
supabase db diffafter applying the generated migration does not keep re-emitting the same view withoutsecurity_invoker. - A reset/replay test creates an RLS-protected table, creates a public view over it, grants
SELECTtoauthenticated, and verifies authenticated queries still obey the underlying table RLS. - The diff output keeps view options near the
CREATE OR REPLACE VIEWstatement rather than requiring a separate manualALTER VIEW ... SET (security_invoker = true)step. - If a fallback diff engine cannot preserve view reloptions, the CLI should warn that generated view SQL needs manual review before production.
I added this exact redacted-SQL check shape to a small local CLI in case it helps people audit generated migration output while this is still open:
npx --package github:kayalopez/ai-agent-launch-tools#v0.1.18 supabase-rpc-audit --file supabase_view_or_rpc.redacted.sql
For CI, it can fail the build on high-severity migration drift:
npx --package github:kayalopez/ai-agent-launch-tools#v0.1.18 supabase-rpc-audit --file supabase_migration.redacted.sql --fail-on high
It does not connect to Supabase or call anything; it just flags local redacted SQL for public views missing security_invoker = true, broad SELECT grants to browser roles, and Security Definer RPC/RLS-bypass patterns.
Opened #5478 to fix this for the create path.
What it does: after db diff runs, the CLI reads pg_class.reloptions from the target and re-attaches the with (...) clause to any CREATE VIEW / CREATE MATERIALIZED VIEW it emitted. So a declarative view with with (security_invoker=true) round-trips into the migration instead of silently dropping to SECURITY DEFINER. It hooks into DiffDatabase (so it's engine-agnostic) and fails soft — if the catalog read fails, the original diff is returned untouched with a stderr warning.
If you're hitting this, you can test the branch:
gh pr checkout 5478
# then re-run your `supabase db diff -f ...`
Would appreciate confirmation it captures the clause for your schema.
Scoping note: this covers new views. The case where a view already exists and only its reloption changes (flipping security_invoker true→false) is a separate migra limitation — tracked in #5476.
This should now be handled correctly if you run db diff with the new pg-delta engine.
The default engine (migra) regenerates view definitions and drops the WITH (...) reloptions, so security_invoker is lost and the view silently falls back to SECURITY DEFINER. pg-delta diffs at the catalog level and preserves view reloptions, so the clause round-trips into your migration.
You shouldn't need to change your workflow, just add --use-pg-delta to the same command:
supabase db diff -f <migration-name> --use-pg-delta
If you'd rather not pass the flag every time, enable it once in supabase/config.toml and plain supabase db diff will use pg-delta:
[experimental.pgdelta]
enabled = true
Please let me know if that works for you @nirav-gondaliya @kayalopez @siliconsizzler @avtarraikmo
Note: pg-delta is still experimental. If you hit anything off in the generated SQL, please share your declarative input and the output so we can take a look.
Confirmed on my side — supabase db diff --use-pg-delta preserves security_invoker, security_barrier, check_option, and reloptions on materialized views (e.g. autovacuum_enabled). The generated migration round-trips the full WITH (…) clause instead of silently dropping it. Full repro details on #5478.
Two small notes from a user perspective:
- Worth surfacing pg-delta in the
db diffdocs as the recommended path for views with reloptions until it's the default, since the current--use-migradefault still hits the SECURITY DEFINER footgun silently. - The
[experimental.pgdelta] enabled = trueopt-in inconfig.tomlis the friendlier flip for project teams vs. remembering--use-pg-deltaon every call — might be worth calling out in the issue resolution.
Thanks @avallete for the quick turnaround.