[英]PostgreSQL: How to copy data from one database table to another database
I need simple example how to copy data from database DB1 table T1 to database DB2 table T2 . 我需要简单的示例如何将数据从数据库DB1表T1复制到数据库DB2表T2 。
T2 has 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,但在不同的端口上。
In the case the two databases are on two different server instances, you could export in CSV from db1
and then import the data in db2
: 如果两个数据库位于两个不同的服务器实例上,则可以从
db1
导出CSV,然后在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_dump
and psql
to specify the address and/or port of the server . 您可以为
pg_dump
和psql
指定命令行参数,以指定服务器的地址和/或端口。
Another option would be to use an external tool like : openDBcopy , to perform the migration/copy of the table. 另一种选择是使用外部工具,如: openDBcopy ,来执行表的迁移/复制。
你可以尝试这个 -
pg_dump -t table_name_to_copy source_db | psql target_db
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.