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


当前回答

以下是对我有效的方法。 第一次转储到一个文件:

pg_dump -h localhost -U myuser -C -t my_table -d first_db>/tmp/table_dump

然后加载转储文件:

psql -U myuser -d second_db</tmp/table_dump

其他回答

首先安装dblink

然后,你可以这样做:

INSERT INTO t2 select * from 
dblink('host=1.2.3.4
 user=*****
 password=******
 dbname=D1', 'select * t1') tt(
       id int,
  col_1 character varying,
  col_2 character varying,
  col_3 int,
  col_4 varchar 
);

对于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

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

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

我使用的是DataGrip (By Intellij Idea)。将数据从一个表(在不同的数据库中)复制到另一个表非常容易。

首先,确保你在Data Grip中连接了两个数据源。

选择源表并按F5或(右键单击->选择复制表到。)

这将显示所有表的列表(您也可以在弹出窗口中使用表名进行搜索)。选择你的目标,然后按OK。

DataGrip将为您处理其他一切。

您还可以使用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

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