Skip to content

Can't push foreign key add to remote DB #128

Closed

Description

@madx

Bug report

Describe the bug

When using supabase db push to deploy a schema change I get the following error:

$ supabase db push
Applying unapplied migrations...
Error: ERROR: must be owner of table leaderboard (SQLSTATE 42501)

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Add a foreign key to a table and generate a migration using supabase db commit. Here's what it produced for me:
ALTER TABLE IF EXISTS public.leaderboard
  ADD CONSTRAINT "leaderboard_userId_fkey" FOREIGN KEY ("userId")
  REFERENCES public.users ("userId") MATCH SIMPLE
  ON UPDATE NO ACTION
  ON DELETE NO ACTION;
  1. Try to push the schema update using supabase db push
  2. See the error message above

Expected behavior

I expect the remote schema to be updated as intended.

System information

  • OS: Arch Linux
  • Version of supabase-cli: 0.15.14
  • Version of Node.js: 17.3.0

Activity

  1. added
    bug
    on Jan 4, 2022
  2. madx commented on Jan 4, 2022

    @madx
    ContributorAuthor

    If someone else has the same issue a quick workaround is to use the UI to temporarily change owners then revert back:

    alter table public.yourtable owner to supabase_admin;
    /* Run your migrations */
    alter table public.yourtable owner to postgres;
  3. soedirgo commented on Jan 5, 2022

    @soedirgo
    Member

    Thanks @madx - the solution is to simply run ALTER TABLE your_table OWNER TO postgres. Right now tables created using the table editor is owned by supabase_admin but we might change this in the future.

    i.e. do:

    alter table public.yourtable owner to postgres;
    /* Run your migrations */
  4. sweatybridge commented on Mar 19, 2023

    @sweatybridge
    Contributor

    We have addressed this in https://github.com/orgs/supabase/discussions/9314 so all your tables and schemas are owned by postgres role.

    If that is not the case, please file a support ticket so that we can fix it for your project.

  5. stephaniegoldman12 commented on Jan 11, 2024

    @stephaniegoldman12

    @sweatybridge Hi, I'm getting this error all of the sudden on my prod DB. Can't update any columns "failed to update pg.tables with the given ID: must be owner of table [name of table]", and ERROR: 42501: must be owner of table exports results from trying to fix it with any SQL commands to alter the table owner. Can you help?

  6. dvorakjt commented on May 6, 2025

    @dvorakjt

    Hello,

    I am having a similar issue. I merged some changes into my staging branch, triggering a Github Actions workflow that resets the staging database. The changes were unrelated to Supabase, so I was surprised to see the supabase db reset --linked <<< 'y' command fail with the following error:

    ERROR: must be owner of sequence refresh_tokens_id_seq (SQLSTATE 42501)                                                                                                   
    At statement 0:                                                                                                                                                           
    do $$ declare                                                                                                                                                             
      rec record;                                                                                                                                                             
    begin                                                                                                                                                                     
      -- extensions                                                                                                                                                           
      for rec in                                                                                                                                                              
        select *                                                                                                                                                              
        from pg_extension p                                                                                                                                                   
        where p.extname not in ('pg_graphql', 'pg_net', 'pg_stat_statements', 'pgcrypto', 'pgjwt', 'pgsodium', 'plpgsql', 'supabase_vault', 'uuid-ossp')                      
      loop                                                                                                                                                                    
        execute format('drop extension if exists %I cascade', rec.extname);                                                                                                   
      end loop;                                                                                                                                                               
                                                                                                                                                                              
      -- functions                                                                                                                                                            
      for rec in                                                                                                                                                              
        select *                                                                                                                                                              
        from pg_proc p                                                                                                                                                        
        where p.pronamespace::regnamespace::name = 'public'                                                                                                                   
      loop                                                                                                                                                                    
        -- supports aggregate, function, and procedure                                                                                                                        
        execute format('drop routine if exists %I.%I(%s) cascade', rec.pronamespace::regnamespace::name, rec.proname, pg_catalog.pg_get_function_identity_arguments(rec.oid));
      end loop;                                                                                                                                                               
                                                                                                                                                                              
      -- views (necessary for views referencing objects in Supabase-managed schemas)                                                                                          
      for rec in                                                                                                                                                              
        select *                                                                                                                                                              
        from pg_class c                                                                                                                                                       
        where                                                                                                                                                                 
          c.relnamespace::regnamespace::name = 'public'                                                                                                                       
          and c.relkind = 'v'                                                                                                                                                 
      loop                                                                                                                                                                    
        execute format('drop view if exists %I.%I cascade', rec.relnamespace::regnamespace::name, rec.relname);                                                               
      end loop;                                                                                                                                                               
                                                                                                                                                                              
      -- materialized views (necessary for materialized views referencing objects in Supabase-managed schemas)                                                                
      for rec in                                                                                                                                                              
        select *                                                                                                                                                              
        from pg_class c                                                                                                                                                       
        where                                                                                                                                                                 
          c.relnamespace::regnamespace::name = 'public'                                                                                                                       
          and c.relkind = 'm'                                                                                                                                                 
      loop                                                                                                                                                                    
        execute format('drop materialized view if exists %I.%I cascade', rec.relnamespace::regnamespace::name, rec.relname);                                                  
      end loop;                                                                                                                                                               
                                                                                                                                                                              
      -- tables (cascade to views)                                                                                                                                            
      for rec in                                                                                                                                                              
        select *                                                                                                                                                              
        from pg_class c                                                                                                                                                       
        where                                                                                                                                                                 
          c.relnamespace::regnamespace::name = 'public'                                                                                                                       
          and c.relkind not in ('c', 'S', 'v', 'm')                                                                                                                           
        order by c.relkind desc                                                                                                                                               
      loop                                                                                                                                                                    
        -- supports all table like relations, except views, complex types, and sequences                                                                                      
        execute format('drop table if exists %I.%I cascade', rec.relnamespace::regnamespace::name, rec.relname);                                                              
      end loop;                                                                                                                                                               
                                                                                                                                                                              
      -- truncate tables in auth, webhooks, and migrations schema                                                                                                             
      for rec in                                                                                                                                                              
        select *                                                                                                                                                              
        from pg_class c                                                                                                                                                       
        where                                                                                                                                                                 
          (c.relnamespace::regnamespace::name = 'auth' and c.relname != 'schema_migrations'                                                                                   
          or c.relnamespace::regnamespace::name = 'supabase_functions' and c.relname != 'migrations'                                                                          
          or c.relnamespace::regnamespace::name = 'supabase_migrations')                                                                                                      
          and c.relkind = 'r'                                                                                                                                                 
      loop                                                                                                                                                                    
        execute format('truncate %I.%I restart identity cascade', rec.relnamespace::regnamespace::name, rec.relname);                                                         
      end loop;                                                                                                                                                               
                                                                                                                                                                              
      -- sequences                                                                                                                                                            
      for rec in                                                                                                                                                              
        select *                                                                                                                                                              
        from pg_class c                                                                                                                                                       
        where                                                                                                                                                                 
          c.relnamespace::regnamespace::name = 'public'                                                                                                                       
          and c.relkind = 's'                                                                                                                                                 
      loop                                                                                                                                                                    
        execute format('drop sequence if exists %I.%I cascade', rec.relnamespace::regnamespace::name, rec.relname);                                                           
      end loop;                                                                                                                                                               
                                                                                                                                                                              
      -- types                                                                                                                                                                
      for rec in                                                                                                                                                              
        select *                                                                                                                                                              
        from pg_type t                                                                                                                                                        
        where                                                                                                                                                                 
          t.typnamespace::regnamespace::name = 'public'                                                                                                                       
          and typtype != 'b'                                                                                                                                                  
      loop                                                                                                                                                                    
        execute format('drop type if exists %I.%I cascade', rec.typnamespace::regnamespace::name, rec.typname);                                                               
      end loop;                                                                                                                                                               
                                                                                                                                                                              
      -- policies                                                                                                                                                             
      for rec in                                                                                                                                                              
        select *                                                                                                                                                              
        from pg_policies p                                                                                                                                                    
      loop                                                                                                                                                                    
        execute format('drop policy if exists %I on %I.%I cascade', rec.policyname, rec.schemaname, rec.tablename);                                                           
      end loop;                                                                                                                                                               
                                                                                                                                                                              
      -- publications                                                                                                                                                         
      for rec in                                                                                                                                                              
        select *                                                                                                                                                              
        from pg_publication p                                                                                                                                                 
        where                                                                                                                                                                 
          p.pubname not like 'supabase_realtime%' and p.pubname not like 'realtime_messages%'                                                                                 
      loop                                                                                                                                                                    
        execute format('drop publication if exists %I', rec.pubname);                                                                                                         
      end loop;                                                                                                                                                               
    end $$;
    

    For reference, here is the part of the GH Actions Workflow file concerning Supabase:

    name: 'Deploy and Test Staging Branch'
    
    on:
      push:
        branches:
          - staging
    
    env:
      SUPABASE_PROJECT_ID: ${{ secrets.SUPABASE_PROJECT_ID }}
    
    jobs:
      reset-db:
        runs-on: ubuntu-latest
        environment: staging
        env:
          SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }}
          SUPABASE_DB_PASSWORD: ${{ secrets.SUPABASE_DB_PASSWORD }}
    
        steps:
          - uses: actions/checkout@v4
          - uses: supabase/setup-cli@v1
            with:
              version: latest
    
          - run: supabase link --project-ref $SUPABASE_PROJECT_ID
          - run: supabase db reset --linked <<< 'y'
    

    Additionally, I should note that the staging branch Supabase project had been paused due to inactivity for some time.

    Any advice or help would be much appreciated, thank you!

  7. jsonMartin commented on May 9, 2025

    @jsonMartin

    Our team is also having the same exact issue as @dvorakjt above when resetting databases:

    ERROR: must be owner of sequence refresh_tokens_id_seq (SQLSTATE 42501)

    Summary:

    • This error occurs on ALL newly created databases, including feature branches.
    • Resets still work fine on older pre-existing databases before this issue was introduced.
    • Investigation suggests new databases are missing the ⁠supabase_auth_admin role, which owns ⁠refresh_tokens_id_seq. This causes reset operations to fail because the role performing the reset operation does not have appropriate permissions for the Auth schema.
    • Caveat: Resetting newly created feature branches with branch type to persistent DOES allow resets directly via Supabase UI (even though it is still missing role permissions).
      • HOWEVER: resetting persistent feature branches via CLI still fails, which throws exactly the same error message as when resetting primary databases (which require CLI).
      • Resetting persistent feature branches via Supabase UI seems to be performing the action with different context or set of permissions than Ephemeral branches or via CLI (primary or feature branch)

    Key Findings

    • Older, pre-existing databases & feature branches which still have the supabase_auth_admin role work when resetting database (both via UI & CLI).
    • New databases & feature branches lack the supabase_auth_admin role, causing all reset operations to fail.
    • Cannot manually grant or alter ownership for the required role.
    • Issue affects ALL projects, including brand new ones with no migrations.
    • Changing a feature branch to persistent allows resets ONLY via UI, even though the role still doesn't appear in permissions.

    Evidence

    Role Query Results Comparison

    Query used to check roles

    SELECT rolname
    FROM pg_roles
    WHERE pg_has_role(current_user, oid, 'member')
    ORDER BY rolname ASC;

    Pre-existing database results:

    rolname
    pg_database_owner
    pg_read_all_data
    pg_monitor
    pg_read_all_settings
    pg_read_all_stats
    pg_stat_scan_tables
    pg_signal_backend
    pgsodium_keyiduser
    pgsodium_keyholder
    pgsodium_keymaker
    authenticated
    anon
    service_role
    postgres
    authenticator
    supabase_auth_admin
    supabase_storage_admin
    supabase_realtime_admin

    New database results:

    rolname
    pg_database_owner
    pg_read_all_data
    pg_monitor
    pg_read_all_settings
    pg_read_all_stats
    pg_stat_scan_tables
    pg_signal_backend
    authenticated
    anon
    service_role
    postgres
    supabase_realtime_admin

    Failed Role Creation Attempts

    GRANT supabase_auth_admin TO postgres;
    -- ERROR: 42501: "supabase_auth_admin" role memberships are reserved, only superusers can grant them
    
    ALTER SEQUENCE auth.refresh_tokens_id_seq OWNER TO postgres;
    -- ERROR: permission denied

    Workarounds (only for feature branches)

    This below workarounds will NOT work for primary database (i.e. non-feature), and only work for databases created via feature branches:

    • Use persistent branches (remember to delete after merge)
    • Delete and recreate feature branches whenever reset is needed
    • Only reset feature branches through Supabase UI, not CLI

    Hypothesis

    This appears to be a platform-level issue with how Supabase provisions roles in feature branches rather than a project-specific configuration problem. I'm not sure exactly when this started happening, but it's happening for me in all projects now. The fact that changing to persistent branch type allows resets suggests there may be additional permissions/configurations applied to persistent branches and running the reset in the Supabase UI vs the CLI.

    I'd love to know a proper fix this issue, because the workarounds are all not optimal, don't work for new projects, and our workflow heavily depends on branching.

  8. dvorakjt commented on May 9, 2025

    @dvorakjt

    @jsonMartin Maybe we should open a new issue for this?

  9. jsonMartin commented on May 9, 2025

    @jsonMartin

    Sure @dvorakjt , that sounds like a good idea. I already opened a support ticket with Supabase, but last time it took me weeks to hear a resolution so I'm hoping for something faster this time 😆

    Just curious - are you using feature branches? Or is this happening to you on your primary database?

  10. jsonMartin commented on May 9, 2025

    @jsonMartin

    I can actually confirm that this is happening on brand new projects, NOT just feature branches.

    I just spun up a brand new project. Running this SQL command on this new database does NOT show supabase_auth_admin role:

    SELECT rolname
    FROM pg_roles
    WHERE pg_has_role(current_user, oid, 'member')
    ORDER BY rolname ASC;
    

    Then, running the reset command fails with this exact same error (ERROR: must be owner of sequence refresh_tokens_id_seq (SQLSTATE 42501)):

    supabase db reset --db-url postgresql://postgres.[DB_URL]:[DB_PASS]@aws-0-us-east-2.pooler.supabase.com:5432/postgres
    

    So, it looks like ANY new database Supabase is spinning up now is lacking the proper permissions, causing any database resets to fail. Looks like this problem is even more severe since it's not limited to the branching feature.

  11. jsonMartin commented on May 9, 2025

    @jsonMartin

    @dvorakjt just created a new issue for this: #3551

    Also just realized this issue was only created inside supabase/cli so I created another issue on the main supabase repo (this is happening on the entire Supabase platform, not just CLI): #3782

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions