我试图在Postgres中从一个数据库复制整个表到另一个数据库。有什么建议吗?


当前回答

如果你有两个远程服务器,那么你可以这样做:

pg_dump -U Username -h DatabaseEndPoint -a -t TableToCopy SourceDatabase | psql -h DatabaseEndPoint -p portNumber -U Username -W TargetDatabase

它会将源数据库中提到的表复制到目标数据库中同名的表,如果您已经有了模式。

其他回答

必须使用DbLink将一个表数据复制到不同数据库中的另一个表中。 您必须安装和配置DbLink扩展才能执行跨数据库查询。

我已经在这个话题上创建了详细的帖子。 请访问此链接

提取表并将其直接输送到目标数据库:

pg_dump -t table_to_copy source_db | psql target_db

注意:如果其他数据库已经设置了表,你应该使用-a标志只导入数据,否则你可能会看到奇怪的错误,如“Out of memory”:

pg_dump -a -t table_to_copy source_db | psql target_db

与user5542464和Piyush S. Wanare的回答相同,但分为两步:

pg_dump -U Username -h DatabaseEndPoint -a -t TableToCopy SourceDatabase > dump
cat dump | psql -h DatabaseEndPoint -p portNumber -U Username -W TargetDatabase

否则,管道将同时询问两个密码。

您还可以使用pgAdmin II中的备份功能。只需遵循以下步骤:

In pgAdmin, right click the table you want to move, select "Backup" Pick the directory for the output file and set Format to "plain" Click the "Dump Options #1" tab, check "Only data" or "only Schema" (depending on what you are doing) Under the Queries section, click "Use Column Inserts" and "User Insert Commands". Click the "Backup" button. This outputs to a .backup file Open this new file using notepad. You will see the insert scripts needed for the table/data. Copy and paste these into the new database sql page in pgAdmin. Run as pgScript - Query->Execute as pgScript F6

工作良好,可以做多个表在一个时间。

如果你有两个远程服务器,那么你可以这样做:

pg_dump -U Username -h DatabaseEndPoint -a -t TableToCopy SourceDatabase | psql -h DatabaseEndPoint -p portNumber -U Username -W TargetDatabase

它会将源数据库中提到的表复制到目标数据库中同名的表,如果您已经有了模式。