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


当前回答

你可以这样做:

pg_dump -h <host ip address> -U <host db user name> -t <host table> > <host database> | psql -h localhost -d <local database> -U <local db user>

其他回答

使用pg_dump转储表数据,然后使用psql恢复。

如果两个db (from & to)都有密码保护,在这种情况下,终端不会要求输入两个db的密码,密码提示只会出现一次。 因此,要解决这个问题,请将密码与命令一起传递。

PGPASSWORD=<password> pg_dump -h <hostIpAddress> -U <hostDbUserName> -t <hostTable> > <hostDatabase> | PGPASSWORD=<pwd> psql -h <toHostIpAddress> -d <toDatabase> -U <toDbUser>

你可以通过两个简单的步骤做到:

# dump the database in custom-format archive
pg_dump -Fc mydb > db.dump

# restore the database
pg_restore -d newdb db.dump

如果是远程数据库:

# dump the database in custom-format archive
pg_dump -U mydb_user -h mydb_host -t table_name -Fc mydb > db.dump

# restore the database
pg_restore -U newdb_user -h newdb_host -d newdb db.dump

使用psql,在与两个服务器都有连接的linux主机上

( export PGPASSWORD=password1 
  psql -U user1 -h host1 database1 \
  -c "copy (select field1,field2 from table1) to stdout with csv" ) \
| 
( export PGPASSWORD=password2 
  psql -U user2 -h host2 database2 \ 
   -c "copy table2 (field1, field2) from stdin csv" )

对于DBeaver工具用户,您可以“导出数据”到另一个数据库中的表中。

我一直面临的唯一错误是由于错误的postgres司机。

SQL Error [34000]: ERROR: portal "c_2" does not exist
    ERROR: Invalid protocol sequence 'P' while in PortalSuspended state.

这是一个关于如何导出数据的官方wiki: https://github.com/dbeaver/dbeaver/wiki/Data-transfer