我想复制一个生产PostgreSQL数据库到一个开发服务器。最快最简单的方法是什么?
当前回答
如果你想在版本之间迁移(例如你更新了postgres,在localhost:5432上运行9.1,在localhost:5434上运行9.3),你可以运行:
pg_dumpall -p 5432 -U myuser91 | psql -U myuser94 -d postgres -p 5434
查看迁移文档。
其他回答
如果你想在版本之间迁移(例如你更新了postgres,在localhost:5432上运行9.1,在localhost:5434上运行9.3),你可以运行:
pg_dumpall -p 5432 -U myuser91 | psql -U myuser94 -d postgres -p 5434
查看迁移文档。
pg_dump the_db_name > the_backup.sql
然后将备份复制到您的开发服务器,使用以下方法进行恢复:
psql the_new_dev_db < the_backup.sql
接受的答案是正确的,但如果你想避免交互式输入密码,你可以使用这个:
PGPASSWORD={{export_db_password}} pg_dump --create -h {{export_db_host}} -U {{export_db_user}} {{export_db_name}} | PGPASSWORD={{import_db_password}} psql -h {{import_db_host}} -U {{import_db_user}} {{import_db_name}}
以数据库名称运行此命令,如果要备份,则转储数据库。
pg_dump -U {user-name} {source_db} -f {dumpfilename.sql}
eg. pg_dump -U postgres mydbname -f mydbnamedump.sql
现在将这个转储文件scp到您想要复制DB的远程机器。
eg. scp mydbnamedump.sql user01@remotemachineip:~/some/folder/
在远程机器上,在~/some/文件夹中运行以下命令来恢复DB。
psql -U {user-name} -d {desintation_db}-f {dumpfilename.sql}
eg. psql -U postgres -d mynewdb -f mydbnamedump.sql
您不需要创建中间文件。你可以这样做
pg_dump -C -h localhost -U localuser dbname | psql -h remotehost -U remoteuser dbname
or
pg_dump -C -h remotehost -U remoteuser dbname | psql -h localhost -U localuser dbname
使用PSQL或pg_dump连接到远程主机。
对于大数据库或慢速连接,转储文件和传输压缩文件可能更快。
正如Kornel所说,没有必要转储到中间文件,如果你想压缩工作,你可以使用压缩隧道
pg_dump -C dbname | bzip2 | ssh remoteuser@remotehost "bunzip2 | psql dbname"
or
pg_dump -C dbname | ssh -C remoteuser@remotehost "psql dbname"
但是这个解决方案还需要在两端都获得一个会话。
注:pg_dump用于备份,psql用于恢复。因此,这个答案中的第一个命令是从本地复制到远程,第二个命令是从远程复制到本地。更多-> https://www.postgresql.org/docs/9.6/app-pgdump.html
推荐文章
- 查询JSON类型内的数组元素
- 获得PostgreSQL数据库中当前连接数的正确查询
- MySQL数据库表中的最大记录数
- 从现有模式生成表关系图(SQL Server)
- HyperLogLog算法是如何工作的?
- 纬度和经度的数据类型是什么?
- 数据库和模式的区别
- 如何在PostgreSQL中临时禁用触发器?
- 输入文件似乎是一个文本格式转储。请使用psql
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N
- 如何从命令行在windows中找到mysql数据目录
- 货币应该使用哪种数据类型?
- 如何添加列,如果不存在PostgreSQL?
- 如何找到MySQL的根密码