在Ubuntu上配置PostgreSQL复制涉及几个步骤,包括设置主服务器和从服务器。以下是一个基本的指南:
主服务器配置
-
安装PostgreSQL
sudo apt update sudo apt install postgresql postgresql-contrib -
配置
postgresql.conf编辑主服务器的postgresql.conf文件(通常位于/etc/postgresql/):/main/ sudo nano /etc/postgresql//main/postgresql.conf 找到并修改以下参数:
listen_addresses = '*' # 允许所有IP连接 wal_level = replica # 设置为replica以支持流复制 max_wal_senders = 10 # 最大WAL发送者数量 wal_keep_segments = 64 # 保留的WAL段数量 -
配置
pg_hba.conf编辑pg_hba.conf文件以允许从服务器连接:sudo nano /etc/postgresql//main/pg_hba.conf 添加以下行以允许从服务器连接:
host replication replicator <从服务器IP>/32 md5 -
重启PostgreSQL服务
sudo systemctl restart postgresql -
创建复制用户 在主服务器上创建一个用于复制的用户:
CREATE ROLE replicator WITH REPLICATION PASSWORD 'your_password' LOGIN; -
获取主服务器的LSN(Log Sequence Number) 连接到主服务器并获取当前的LSN:
sudo -u postgres psql SELECT pg_current_wal_lsn();
从服务器配置
-
安装PostgreSQL 确保从服务器上也安装了PostgreSQL。
-
配置
postgresql.conf编辑从服务器的postgresql.conf文件:sudo nano /etc/postgresql//main/postgresql.conf 找到并修改以下参数:
listen_addresses = '*' # 允许所有IP连接 hot_standby = on # 启用热备模式 -
配置
recovery.conf在从服务器上创建或编辑recovery.conf文件(通常位于/var/lib/postgresql/):/main/ sudo nano /var/lib/postgresql//main/recovery.conf 添加以下内容:
standby_mode = 'on' primary_conninfo = 'host=<主服务器IP> dbname=postgres user=replicator password=your_password' restore_command = 'cp /var/lib/postgresql/wal_archive/%f %p' trigger_file = '/tmp/postgresql.trigger.5432' -
重启PostgreSQL服务
sudo systemctl restart postgresql
验证复制
-
检查主服务器状态 在主服务器上运行:
SELECT * FROM pg_stat_replication; -
检查从服务器状态 在从服务器上运行:
SELECT * FROM pg_stat_replication;
如果一切配置正确,你应该能够看到从服务器正在接收来自主服务器的数据。
注意事项
- 确保防火墙允许PostgreSQL端口(默认是5432)的流量。
- 定期检查复制状态,并处理任何错误。
- 根据实际需求调整配置参数。
通过以上步骤,你应该能够在Ubuntu上成功配置PostgreSQL复制。
以上就是关于“如何在Ubuntu上配置PostgreSQL复制”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm