我试图在Postgres中从一个数据库复制整个表到另一个数据库。有什么建议吗?
当前回答
如果你有两个远程服务器,那么你可以这样做:
pg_dump -U Username -h DatabaseEndPoint -a -t TableToCopy SourceDatabase | psql -h DatabaseEndPoint -p portNumber -U Username -W TargetDatabase
它会将源数据库中提到的表复制到目标数据库中同名的表,如果您已经有了模式。
其他回答
使用pg_dump转储表数据,然后使用psql恢复。
我使用的是DataGrip (By Intellij Idea)。将数据从一个表(在不同的数据库中)复制到另一个表非常容易。
首先,确保你在Data Grip中连接了两个数据源。
选择源表并按F5或(右键单击->选择复制表到。)
这将显示所有表的列表(您也可以在弹出窗口中使用表名进行搜索)。选择你的目标,然后按OK。
DataGrip将为您处理其他一切。
没有任何管道,在Windows上,你可以使用:
转储-编辑这是在一行
"C:\Program Files\PostgreSQL\14\bin\pg_dump.exe"
--host="host-postgres01"
--port="1234"
--username="user01"
-t "schema01.table01"
--format=c
-f "C:\Users\user\Downloads\table01_format_c.sql"
"DB-01"
恢复-编辑这是在一行
"C:\Program Files\PostgreSQL\14\bin\pg_restore.exe"
--host="host-postgres02"
--port="5678"
--username="user02"
-1
--dbname="DB-02"
"C:\Users\user\Downloads\table01_format_c.sql"
系统将提示您输入用户密码。
这个解决方案将把新表放在具有相同名称的模式中(schema01)。
必须使用DbLink将一个表数据复制到不同数据库中的另一个表中。 您必须安装和配置DbLink扩展才能执行跨数据库查询。
我已经在这个话题上创建了详细的帖子。 请访问此链接
你可以通过两个简单的步骤做到:
# 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
推荐文章
- 查询JSON类型内的数组元素
- 获得PostgreSQL数据库中当前连接数的正确查询
- 纬度和经度的数据类型是什么?
- 如何在PostgreSQL中临时禁用触发器?
- 输入文件似乎是一个文本格式转储。请使用psql
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N
- 货币应该使用哪种数据类型?
- 如何添加列,如果不存在PostgreSQL?
- 如何在Postgres中获得两个字段的MIN() ?
- 截断Postgres数据库中的所有表
- 如何连接列在Postgres选择?
- 将varchar字段的类型更改为整数:"不能自动转换为整数类型"
- PostgreSQL可以索引数组列吗?
- PostgreSQL:角色不允许登录