Postgres:将数据从一个数据库的公共模式移动到另一个数据库的新模式的最佳方式 [英] Postgres: Best way to move data from public schema of one DB to new schema of another DB
问题描述
我是Postgres的新用户,刚刚发现我无法在一个SQL查询中访问不同数据库的数据。并且还学习了Postgres中的模式概念。
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 have two databases
db1和db2
两个表在公共模式中具有相同的名称。
Both have tables with same name in their public schema.
现在,我想在db1中使用名称创建一个新模式:new_schema
Now, I want to create a new schema in db1 with name : new_schema
.public to db1.new_schema
And move data from db2.public to db1.new_schema
这是最简单的方法是什么?
What is the easiest way to do this ?
推荐答案
最简单的方法是重命名模式。但是,您必须确保您是db1数据库的唯一用户。
The simplest way to do that is to rename schemas. However you must be sure you are a sole user of db1 database.
首先,在db1中隐藏模式public:
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;
另一个选择是使用 dblink。它可以访问不同数据库的数据。
Another option is to use dblink. It enables accessing data of different databases.
这篇关于Postgres:将数据从一个数据库的公共模式移动到另一个数据库的新模式的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!