我试图在Postgres中从一个数据库复制整个表到另一个数据库。有什么建议吗?
当前回答
没有任何管道,在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扩展才能执行跨数据库查询。
我已经在这个话题上创建了详细的帖子。 请访问此链接
您还可以使用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 localhost -U myuser -C -t my_table -d first_db>/tmp/table_dump
然后加载转储文件:
psql -U myuser -d second_db</tmp/table_dump
与user5542464和Piyush S. Wanare的回答相同,但分为两步:
pg_dump -U Username -h DatabaseEndPoint -a -t TableToCopy SourceDatabase > dump
cat dump | psql -h DatabaseEndPoint -p portNumber -U Username -W TargetDatabase
否则,管道将同时询问两个密码。
我尝试了这里的一些解决方案,它们真的很有帮助。根据我的经验,最好的解决方案是使用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
)
)
这种方法的问题是,必须写入字段的名称及其要复制的表的类型。
推荐文章
- 对于PostgreSQL表来说,多大才算太大?
- 将一列的多个结果行连接为一列,按另一列分组
- 使用pgadmin连接到heroku数据库
- 在PostgreSQL中快速发现表的行数
- 更改varchar列的大小为较低的长度
- 如何首次配置postgresql ?
- 数据库性能调优有哪些资源?
- 如何在PostgreSQL中自动更新时间戳
- 当使用JDBC连接到postgres时,是否可以指定模式?
- SQL:从时间戳日期减去1天
- PostgreSQL删除所有内容
- 为什么PostgreSQL要对索引列进行顺序扫描?
- PostgreSQL INSERT ON冲突更新(upsert)使用所有排除的值
- 如何检查一个表是否存在于给定的模式中
- 如何将整数转换为字符串作为PostgreSQL查询的一部分?