PostgreSQL:如何将数据从一个数据库表复制到另一个数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39487478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:23:25  来源:igfitidea点击:

PostgreSQL: How to copy data from one database table to another database

databasepostgresqlpgadmin

提问by Norbertas Brazaitis

I need simple example how to copy data from database DB1table T1to database DB2table T2.

我需要一个简单的例子,如何将数据从数据库DB1T1复制到数据库DB2T2

T2has identical structure like T1(same column names, properties. Just different data) DB2 running on same server like DB1, but on different port.

T2具有与T1相同的结构(相同的列名、属性。只是不同的数据) DB2 运行在与 DB1 相同的服务器上,但在不同的端口上。

回答by aleroot

In the case the two databases are on two different server instances, you could export in CSV from db1and then import the data in db2:

如果两个数据库位于两个不同的服务器实例上,您可以从 CSV 导出db1,然后将数据导入db2

COPY (SELECT * FROM t1) TO '/home/export.csv';

and then load back into db2:

然后加载回db2

COPY t2 FROM '/home/export.csv';

Again, the two tables on the two different database instances must have the same structure.

同样,两个不同数据库实例上的两个表必须具有相同的结构。

Using the command line tools : pg_dump and psql , you could do even in this way :

使用命令行工具: pg_dump 和 psql ,您甚至可以这样做:

pg_dump -U postgres -t t1 db1 | psql -U postgres -d db2

You can specify command line arguments to both pg_dumpand psqlto specify the address and/or port of the server .

您可以为两者指定命令行参数pg_dumppsql指定服务器的地址和/或端口。

Another option would be to use an external tool like : openDBcopy, to perform the migration/copy of the table.

另一种选择是使用像openDBcopy这样的外部工具来执行表的迁移/复制。

回答by Mohammad Aarif

You can try this one -

你可以试试这个——

 pg_dump -t table_name_to_copy source_db | psql target_db