在Debian中配置PostgreSQL复制涉及几个步骤,包括设置主服务器和从服务器。以下是一个基本的指南:
1. 安装PostgreSQL
首先,在主服务器和从服务器上安装PostgreSQL。
sudo apt update
sudo apt install postgresql postgresql-contrib
2. 配置主服务器
编辑主服务器的postgresql.conf文件,通常位于/etc/postgresql/目录下。
sudo nano /etc/postgresql//main/postgresql.conf
找到并修改以下参数:
listen_addresses = '*' # 允许所有IP连接
wal_level = replica # 设置为replica以启用流复制
max_wal_senders = 10 # 允许的最大WAL发送者数量
wal_keep_segments = 64 # 保留的WAL段数量
保存并退出编辑器。
3. 配置pg_hba.conf
编辑pg_hba.conf文件以允许从服务器连接。
sudo nano /etc/postgresql//main/pg_hba.conf
添加以下行以允许从服务器连接:
host replication replicator <从服务器IP>/32 md5
保存并退出编辑器。
4. 创建复制用户
在主服务器上创建一个用于复制的用户。
sudo -u postgres psql
CREATE USER replicator WITH REPLICATION PASSWORD 'your_password' LOGIN;
GRANT ALL PRIVILEGES ON DATABASE your_database TO replicator;
\q
5. 重启PostgreSQL服务
在主服务器上重启PostgreSQL服务以应用更改。
sudo systemctl restart postgresql
6. 配置从服务器
在从服务器上编辑postgresql.conf文件。
sudo nano /etc/postgresql//main/postgresql.conf
找到并修改以下参数:
listen_addresses = '*' # 允许所有IP连接
hot_standby = on # 启用热备份模式
保存并退出编辑器。
7. 配置recovery.conf
在从服务器上创建或编辑recovery.conf文件。
sudo nano /etc/postgresql//main/recovery.conf
添加以下内容:
standby_mode = 'on'
primary_conninfo = 'host=<主服务器IP> dbname=your_database user=replicator password=your_password'
restore_command = 'cp /var/lib/postgresql//main/wal_archive/%f %p'
trigger_file = '/tmp/postgresql.trigger.5432'
确保wal_archive目录存在并且有适当的权限。
sudo mkdir -p /var/lib/postgresql//main/wal_archive
sudo chown postgres:postgres /var/lib/postgresql//main/wal_archive
保存并退出编辑器。
8. 重启PostgreSQL服务
在从服务器上重启PostgreSQL服务以应用更改。
sudo systemctl restart postgresql
9. 验证复制
在主服务器上创建一个测试数据库或表,并在从服务器上检查是否同步。
-- 在主服务器上
sudo -u postgres psql
CREATE DATABASE test_db;
CREATE TABLE test_table (id SERIAL PRIMARY KEY, name VARCHAR(100));
INSERT INTO test_table (name) VALUES ('Test');
\q
在从服务器上检查:
-- 在从服务器上
sudo -u postgres psql
\l
\dt
SELECT * FROM test_table;
\q
如果一切配置正确,你应该能够在从服务器上看到主服务器上的数据。
注意事项
- 确保防火墙允许PostgreSQL端口(默认5432)的流量。
- 定期检查复制状态,确保没有错误。
- 根据需要调整
max_wal_senders和wal_keep_segments等参数。
通过以上步骤,你应该能够在Debian上成功配置PostgreSQL复制。
以上就是关于“如何在Debian中配置PostgreSQL复制”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm