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

标签 database postgresql postgresql-9.3 pg-dump

我是 Postgres 的新手,刚刚发现我无法在一个 SQL 查询中访问不同数据库的数据。并且还学习了 Postgres 中模式的概念。

现在,我有两个数据库

db1 和 db2

两者在其公共(public)模式中都有同名的表。

现在,我想在 db1 中创建一个名为 new_schema 的新模式

并将数据从 db2.public 移动到 db1.new_schema

最简单的方法是什么?

最佳答案

最简单的方法是重命名模式。但是,您必须确保您是 db1 数据库的唯一用户。

首先,在 db1 中隐藏您的公开模式:

alter schema public rename to original_public;
create schema public;

接下来,做备份和恢复:

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

最后,重新创建适当的架构名称:

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

另一种选择是使用 dblink.它允许访问不同数据库的数据。

关于database - Postgres : Best way to move data from public schema of one DB to new schema of another DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24080832/

相关文章:

MySQL查询: GROUP BY - get specific row (not always the first one)

java - 组织.postgresql.util.PSQLException : ERROR: syntax error at or near ":"

postgresql - 根据请求更改 PostgreSQL 日期语言

postgresql - 如何将 Postgres SQL 数据读入 Grafana 图中

python - 导入 psycopg2 库未加载 : libssl. 1.0.0.dylib

database - 比较 Access SQL 查询中的日期

sql - Flask/Sqlalchemy 的初始数据

mysql - 同一个表中相关项的 SQL 查询

database - 拒绝连接到 PostgreSQL 服务器

sql - PostgreSQL - 如何动态转换?