Skip to content

Improve migration between projects to include super user role update #1416

Closed

Description

@cdedreuille

Improve documentation

Link

https://supabase.io/docs/guides/database#migrating-between-projects

Describe the problem

It took me quite a while to understand it but when I was doing a migration from a project to another one replication disappeared and even though my data was correctly imported into the new project, all foreign keys weirdly brought bugs.

After a couple of tests, I found out that the problem was that the user postgres was not a super user.
I fixed the issue by temporarily set the postgres user as super user with ALTER USER postgres WITH SUPERUSER;
I could now see way less errors and the import worked correctly.
I switch the user postgres back to not being a super user with ALTER USER postgres WITH NOSUPERUSER;

Describe the improvement

I'm not 100% sure this is the right approach but it worked for me. If it is then perhaps this could be added to the doc. In the doc, you're saying "You may see some errors". But in my case, I saw a lot of errors due to lack of permissions and I thought it was ok.

Activity

  1. added
    documentation
    on May 4, 2021
  2. steve-chavez commented on May 5, 2021

    @steve-chavez
    Member

    I could now see way less errors and the import worked correctly.

    Can you share what were the errors?

    We're tightening security and SUPERUSER privilege won't be available soon(ish).

  3. cdedreuille commented on May 5, 2021

    @cdedreuille
    Author

    Thanks for your reply @steve-chavez. To reproduce the issue:

    1. Create a blank new project
    2. Export database structure from the old project with this code:
    pg_dump \
    -h db.PROJECT_REF.supabase.co \
    -U postgres \
    --clean \
    --schema-only \
    > supabase_schema.sql
    
    1. Import the structure to the new project (It's asking for the password for user postgres - I used the project password)
    psql \
    -h db.NEW_PROJECT_REF.supabase.co \
    -U postgres \
    < supabase_schema.sql
    
    1. I get a long list of results in the console including these errors:
    ERROR:  must be owner of table objects
    ERROR:  relation "public.searchUpdates" does not exist
    ERROR:  must be owner of index name_prefix_search
    ERROR:  must be owner of table objects
    ERROR:  cannot drop constraint users_pkey on table auth.users because other objects depend on it
    DETAIL:  constraint buckets_owner_fkey on table storage.buckets depends on index auth.users_pkey
    constraint objects_owner_fkey on table storage.objects depends on index auth.users_pkey
    HINT:  Use DROP ... CASCADE to drop the dependent objects too.
    ERROR:  permission denied to create extension "pgcrypto"
    HINT:  Must be superuser to create this extension.
    ERROR:  extension "pgcrypto" does not exist
    ERROR:  required extension "pgcrypto" is not installed
    HINT:  Use CREATE EXTENSION ... CASCADE to install required extensions too.
    ERROR:  extension "pgjwt" does not exist
    CREATE EXTENSION
    COMMENT
    CREATE TYPE
    ERROR:  must be member of role "supabase_admin"
    CREATE FUNCTION
    ERROR:  must be member of role "supabase_admin"
    ERROR:  function "extension" already exists with same argument types
    CREATE TABLE
    ERROR:  must be member of role "supabase_admin"
    ERROR:  must be member of role "supabase_admin"
    ERROR:  must be member of role "supabase_admin"
    ERROR:  must be member of role "supabase_admin"
    ERROR:  must be member of role "supabase_admin"
    ERROR:  must be member of role "supabase_admin"
    ERROR:  permission denied to create event trigger "api_restart"
    HINT:  Must be superuser to create an event trigger.
    ERROR:  event trigger "api_restart" does not exist
    

    These are just snippets. If you want the full log please let me know.

    On the new project I also couldn't delete a user from the dashboard. I could only do that if postgres is a super user. Not sure what's the right approach here.

  4. AWaselnuk commented on May 6, 2021

    @AWaselnuk

    all foreign keys weirdly brought bugs

    I'm struggling with foreign keys after a project migration. I wonder if this is the cause.

    Details here: https://twitter.com/AWaselnuk/status/1390307727583371264?s=20

  5. cdedreuille commented on May 6, 2021

    @cdedreuille
    Author

    @AWaselnuk Yes that seems to be the same issue. If you use the workaround I described in the problem it should do the trick. @kiwicopple also talked about it in your tweet.

  6. steve-chavez commented on May 6, 2021

    @steve-chavez
    Member

    @AWaselnuk Can you try creating/dropping a fake table on your db, and then retry the failing requests?

    Do:

    create table sample();
    drop table sample;
  7. steve-chavez commented on May 6, 2021

    @steve-chavez
    Member

    These are just snippets. If you want the full log please let me know.

    @cdedreuille Yes, that would be really helpful. Thank you.

  8. AWaselnuk commented on May 6, 2021

    @AWaselnuk

    @cdedreuille thanks for the assist :)

    @steve-chavez I tried the table create/drop and still seeing same error from query.

    Steps I took:

    1. Go to sequel dashboard on PROD project (the one with messed up foreign keys)
    2. Run create table sample(); drop table sample;
    3. Tried the problem request again from supabase client
       await supabase
        .from<Purchase>("purchases")
        .select("*, product:stripe_product_id (name, stripe_product_id)"
    

    Error message same as before:

    'Could not find foreign keys between these entities. No relationship found between purchases and stripe_product_id'

    I need a way to fix these keys without running a second import because I shipped with a workaround so I now have actual user data coming in on PROD.

  9. steve-chavez commented on May 6, 2021

    @steve-chavez
    Member

    @AWaselnuk Hm. Could you send me your project ref(https://app.supabase.io/project/<project_ref>/editor/sql) to [email protected]?

  10. cdedreuille commented on May 6, 2021

    @cdedreuille
    Author

    @steve-chavez I sent the logs by email.

  11. AWaselnuk commented on May 7, 2021

    @AWaselnuk

    @steve-chavez sent. Thanks very much 🙏

  12. steve-chavez commented on May 7, 2021

    @steve-chavez
    Member

    I think the main problem with the current procedure in the docs is that it dumps the fulll db, and that includes internal schemas, such as auth, storage, extensions - those require high privileges to set up.

    If we restrict to just dump the public schema(which is where users store their app schema), it's possible to restore without a SUPERUSER. Like this:

    pg_dump \
      -h db.ref.supabase.co \
      -U postgres --schema-only \
      --clean --if-exists --schema=public \
      > supabase_schema.sql

    The restore command stays the same, I've tried it on a new project and it's working.

    @cdedreuille Perhaps you can confirm the above works without a superuser?

  13. cdedreuille commented on May 10, 2021

    @cdedreuille
    Author

    @steve-chavez Ah I'll try it out. That would make sense. Perhaps it would be great to document the 2 approaches as in my case I actually wanted to move auth users as well.

  14. GaryAustin1 commented on Feb 8, 2023

    @GaryAustin1
    Collaborator

    Closing this issue as stale and likely impacted by the new security patch removing super user role.
    #9314

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

    documentation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions