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


当前回答

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

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

其他回答

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

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

使用dblink会更方便!

truncate table tableA;

insert into tableA
select *
from dblink('hostaddr=xxx.xxx.xxx.xxx dbname=mydb user=postgres',
            'select a,b from tableA')
       as t1(a text,b text);

你可以这样做:

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>

首先安装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 
);

要在本地设置中将一个表从数据库a移动到数据库B,使用以下命令:

pg_dump -h localhost -U owner-name -p 5432 -C -t table-name database1 | psql -U owner-name -h localhost -p 5432 database2