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


当前回答

我尝试了这里的一些解决方案,它们真的很有帮助。根据我的经验,最好的解决方案是使用psql命令行,但有时我不喜欢使用psql命令行。这是pgAdminIII的另一个解决方案

create table table1 as(
 select t1.* 
 from dblink(
   'dbname=dbSource user=user1 password=passwordUser1',
   'select * from table1'  
  ) as t1(
    fieldName1 as bigserial,
    fieldName2 as text,
    fieldName3 as double precision 
  )
 )

这种方法的问题是,必须写入字段的名称及其要复制的表的类型。

其他回答

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

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

你可以这样做:

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>

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

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

要在本地设置中将一个表从数据库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

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

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