我试图在Postgres中从一个数据库复制整个表到另一个数据库。有什么建议吗?
当前回答
我使用的是DataGrip (By Intellij Idea)。将数据从一个表(在不同的数据库中)复制到另一个表非常容易。
首先,确保你在Data Grip中连接了两个数据源。
选择源表并按F5或(右键单击->选择复制表到。)
这将显示所有表的列表(您也可以在弹出窗口中使用表名进行搜索)。选择你的目标,然后按OK。
DataGrip将为您处理其他一切。
其他回答
提取表并将其直接输送到目标数据库:
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
您还可以使用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
工作良好,可以做多个表在一个时间。
如果你想将数据从一个服务器数据库复制到另一个服务器数据库,那么你必须创建dblink连接两个数据库,否则你可以导出CSV中的表数据,并导入其他数据库表中的数据,表字段应该与主表相同。
使用psql,在与两个服务器都有连接的linux主机上
( export PGPASSWORD=password1
psql -U user1 -h host1 database1 \
-c "copy (select field1,field2 from table1) to stdout with csv" ) \
|
( export PGPASSWORD=password2
psql -U user2 -h host2 database2 \
-c "copy table2 (field1, field2) from stdin csv" )
我尝试了这里的一些解决方案,它们真的很有帮助。根据我的经验,最好的解决方案是使用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
)
)
这种方法的问题是,必须写入字段的名称及其要复制的表的类型。
推荐文章
- 查询JSON类型内的数组元素
- 获得PostgreSQL数据库中当前连接数的正确查询
- 纬度和经度的数据类型是什么?
- 如何在PostgreSQL中临时禁用触发器?
- 输入文件似乎是一个文本格式转储。请使用psql
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N
- 货币应该使用哪种数据类型?
- 如何添加列,如果不存在PostgreSQL?
- 如何在Postgres中获得两个字段的MIN() ?
- 截断Postgres数据库中的所有表
- 如何连接列在Postgres选择?
- 将varchar字段的类型更改为整数:"不能自动转换为整数类型"
- PostgreSQL可以索引数组列吗?
- PostgreSQL:角色不允许登录