Postgres: Best way to move data from public schema of one DB to new schema of another DB

前端 未结 2 686
一向
一向 2021-02-07 20:31

I am new to Postgres and just discovered that I cannot access data of different databases in one SQL query. And also learned the concept of schema in Postgres.

Now, I ha

2条回答
  •  生来不讨喜
    2021-02-07 21:18

    The simplest way to do that is to rename schemas. However you must be sure you are a sole user of db1 database.

    First, hide your schema public in db1:

    alter schema public rename to original_public;
    create schema public;
    

    Next, do the backup and restore:

    $ pg_dump --format custom --file "my_backup" --schema "public" "db2"
    $ pg_restore --dbname "db1" "my_backup"
    

    Finally, recreate appropriate schema names:

    alter schema public rename to my_schema;
    alter schema original_public rename to public;
    

    Another option is to use dblink. It enables accessing data of different databases.

提交回复
热议问题