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


当前回答

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

# 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

其他回答

你可以这样做:

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>

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

如果你想将数据从一个服务器数据库复制到另一个服务器数据库,那么你必须创建dblink连接两个数据库,否则你可以导出CSV中的表数据,并导入其他数据库表中的数据,表字段应该与主表相同。

如果你在Windows上运行pgAdmin(备份:pg_dump,恢复:pg_restore),默认情况下,它会尝试将文件输出到c:\Windows\System32,这就是为什么你会得到拒绝权限/访问的错误,而不是因为用户postgres不够高。以管理员身份运行pgAdmin,或者直接选择Windows系统文件夹以外的输出位置。

使用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);