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


当前回答

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

其他回答

我使用的是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

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

你可以这样做:

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恢复。

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