我想复制一个生产PostgreSQL数据库到一个开发服务器。最快最简单的方法是什么?
当前回答
Pg_basebackup现在似乎是更好的方式,特别是对于大型数据库。
可以从具有相同或较旧主版本的服务器复制数据库。或者更准确地说:
Pg_basebackup适用于相同或更老版本(低至9.1)的服务器。但WAL流模式(-X stream)仅适用于9.3及以后的服务器版本,当前版本的tar格式模式(——format=tar)仅适用于9.5及以后的服务器版本。
对于源服务器,您需要:
listen_addresses = '*' to be able to connect from the target server. Make sure port 5432 is open for that matter. At least 1 available replication connection: max_wal_senders = 1 (-X fetch), 2 for -X stream (the default in case of PostgreSQL 12), or more. wal_level = replica or higher to be able to set max_wal_senders > 0. host replication postgres DST_IP/32 trust in pg_hba.conf. This grants access to the pg cluster to anyone from the DST_IP machine. You might want to resort to a more secure option.
更改1、2、3需要重新启动服务器,更改4需要重新加载。
在目标服务器上:
# systemctl stop postgresql@VERSION-NAME
postgres$ pg_basebackup -h SRC_IP -U postgres -D VERSION/NAME --progress
# systemctl start postgresql@VERSION-NAME
其他回答
先使用pg_dump,然后再使用psql或pg_restore——这取决于您是选择- fp选项还是- fc选项来使用pg_dump。
用法示例:
ssh production
pg_dump -C -Fp -f dump.sql -U postgres some_database_name
scp dump.sql development:
rm dump.sql
ssh development
psql -U postgres -f dump.sql
如果你想在版本之间迁移(例如你更新了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
Pg_basebackup现在似乎是更好的方式,特别是对于大型数据库。
可以从具有相同或较旧主版本的服务器复制数据库。或者更准确地说:
Pg_basebackup适用于相同或更老版本(低至9.1)的服务器。但WAL流模式(-X stream)仅适用于9.3及以后的服务器版本,当前版本的tar格式模式(——format=tar)仅适用于9.5及以后的服务器版本。
对于源服务器,您需要:
listen_addresses = '*' to be able to connect from the target server. Make sure port 5432 is open for that matter. At least 1 available replication connection: max_wal_senders = 1 (-X fetch), 2 for -X stream (the default in case of PostgreSQL 12), or more. wal_level = replica or higher to be able to set max_wal_senders > 0. host replication postgres DST_IP/32 trust in pg_hba.conf. This grants access to the pg cluster to anyone from the DST_IP machine. You might want to resort to a more secure option.
更改1、2、3需要重新启动服务器,更改4需要重新加载。
在目标服务器上:
# systemctl stop postgresql@VERSION-NAME
postgres$ pg_basebackup -h SRC_IP -U postgres -D VERSION/NAME --progress
# systemctl start postgresql@VERSION-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
推荐文章
- 在MongoDB中查找重复的记录
- 对于PostgreSQL表来说,多大才算太大?
- 模式、表和数据库之间的区别是什么?
- 将一列的多个结果行连接为一列,按另一列分组
- 我看到VARCHAR(255)如此频繁地使用(而不是其他长度),有什么好的原因吗?
- 使用pgadmin连接到heroku数据库
- 在PostgreSQL中快速发现表的行数
- 更改varchar列的大小为较低的长度
- Delete_all vs destroy_all
- 我如何移动一个redis数据库从一个服务器到另一个?
- 如何首次配置postgresql ?
- 数据库性能调优有哪些资源?
- 如何在PostgreSQL中自动更新时间戳
- 当使用JDBC连接到postgres时,是否可以指定模式?
- 对象'DF__*'依赖于列'*' -将int改为double