我试图在Postgres中从一个数据库复制整个表到另一个数据库。有什么建议吗?
当前回答
Pg_dump并不总是有效。
假设在两个dbs中有相同的表ddl 你可以从stdout和stdin中破解它,如下所示:
# grab the list of cols straight from bash
psql -d "$src_db" -t -c \
"SELECT column_name
FROM information_schema.columns
WHERE 1=1
AND table_name='"$table_to_copy"'"
# ^^^ filter autogenerated cols if needed
psql -d "$src_db" -c \
"copy ( SELECT col_1 , col2 FROM table_to_copy) TO STDOUT" |\
psql -d "$tgt_db" -c "\copy table_to_copy (col_1 , col2) FROM STDIN"
其他回答
对于DBeaver工具用户,您可以“导出数据”到另一个数据库中的表中。
我一直面临的唯一错误是由于错误的postgres司机。
SQL Error [34000]: ERROR: portal "c_2" does not exist
ERROR: Invalid protocol sequence 'P' while in PortalSuspended state.
这是一个关于如何导出数据的官方wiki: https://github.com/dbeaver/dbeaver/wiki/Data-transfer
提取表并将其直接输送到目标数据库:
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 localhost -U myuser -C -t my_table -d first_db>/tmp/table_dump
然后加载转储文件:
psql -U myuser -d second_db</tmp/table_dump
要在本地设置中将一个表从数据库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
没有任何管道,在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)。
推荐文章
- 查询JSON类型内的数组元素
- 获得PostgreSQL数据库中当前连接数的正确查询
- 纬度和经度的数据类型是什么?
- 如何在PostgreSQL中临时禁用触发器?
- 输入文件似乎是一个文本格式转储。请使用psql
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N
- 货币应该使用哪种数据类型?
- 如何添加列,如果不存在PostgreSQL?
- 如何在Postgres中获得两个字段的MIN() ?
- 截断Postgres数据库中的所有表
- 如何连接列在Postgres选择?
- 将varchar字段的类型更改为整数:"不能自动转换为整数类型"
- PostgreSQL可以索引数组列吗?
- PostgreSQL:角色不允许登录