Repository navigation
Description
Bug report
- I confirm this is a bug with Supabase, not with my own application.
- 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';
Activity
siliconsizzler commentedon Aug 2, 2025
I experience a similar issue with
security_invoker.Migration fails for the following SQL:
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 startI get:sweatybridge commentedon Aug 6, 2025
I will close this as a duplicate #3973
avtarraikmo commentedon Aug 6, 2025
This is issue 3973.
What is it a duplicate of?
sweatybridge commentedon Aug 6, 2025
My bad. It's something from 3 years ago #792
feat: promote pg-delta as default diff engine for declarative schemas
TheRyanBurke commentedon May 14, 2026
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
authenticatedrole 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.
refactor(ci): split apply-to-dev, de-dup step gates, declare view opt…
refactor(ci): split apply-to-dev, de-dup step gates, declare view opt…
fix(ci): drop migra's no-op budget_summary view-recreations
fix(db): pin budget_summary view options & grants outside declarative…
fix(db): restore budget_summary options & grants after side-effect re…
fix(db): restore budget_summary options & grants after side-effect re…
fix(db): own budget_summary entirely via migrations, strip migra's vi…
fix(db): pin budget_summary view options & grants outside declarative…
fix(db): own budget_summary entirely via migrations, strip migra's vi…
kayalopez commentedon May 24, 2026
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:
create view ... with (security_invoker = true)generates migration SQL that preserves the view option.supabase db diffafter applying the generated migration does not keep re-emitting the same view withoutsecurity_invoker.SELECTtoauthenticated, and verifies authenticated queries still obey the underlying table RLS.CREATE OR REPLACE VIEWstatement rather than requiring a separate manualALTER VIEW ... SET (security_invoker = true)step.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:
For CI, it can fail the build on high-severity migration drift:
It does not connect to Supabase or call anything; it just flags local redacted SQL for public views missing
security_invoker = true, broadSELECTgrants to browser roles, and Security Definer RPC/RLS-bypass patterns.nirav-gondaliya commentedon Jun 5, 2026
Opened #5478 to fix this for the create path.
What it does: after
db diffruns, the CLI readspg_class.reloptionsfrom the target and re-attaches thewith (...)clause to anyCREATE VIEW/CREATE MATERIALIZED VIEWit emitted. So a declarative view withwith (security_invoker=true)round-trips into the migration instead of silently dropping to SECURITY DEFINER. It hooks intoDiffDatabase(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:
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.
avallete commentedon Jun 5, 2026
This should now be handled correctly if you run
db diffwith the new pg-delta engine.The default engine (migra) regenerates view definitions and drops the
WITH (...)reloptions, sosecurity_invokeris lost and the view silently falls back toSECURITY 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-deltato the same command:If you'd rather not pass the flag every time, enable it once in
supabase/config.tomland plainsupabase db diffwill use pg-delta: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.
nirav-gondaliya commentedon Jun 5, 2026
Confirmed on my side —
supabase db diff --use-pg-deltapreservessecurity_invoker,security_barrier,check_option, and reloptions on materialized views (e.g.autovacuum_enabled). The generated migration round-trips the fullWITH (…)clause instead of silently dropping it. Full repro details on #5478.Two small notes from a user perspective:
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.[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.