在Debian系统下配置PostgreSQL复制,通常涉及设置一个主服务器(Master)和一个或多个从服务器(Slave)。以下是基本的步骤指南:
首先,确保在主服务器和从服务器上都安装了PostgreSQL。
sudo apt update
sudo apt install postgresql postgresql-contrib
编辑主服务器的postgresql.conf
文件,通常位于/etc/postgresql/<version>/main/
目录下。
sudo nano /etc/postgresql/<version>/main/postgresql.conf
找到并修改以下参数:
listen_addresses = '*' # 允许所有IP连接
wal_level = replica # 设置为replica以启用流复制
max_wal_senders = 10 # 最大WAL发送者数量
wal_keep_segments = 64 # 保留的WAL段数量
保存并退出编辑器。
编辑pg_hba.conf
文件以允许从服务器连接。
sudo nano /etc/postgresql/<version>/main/pg_hba.conf
添加以下行以允许从服务器连接:
host replication replica_user <slave_ip>/32 md5
将<slave_ip>
替换为从服务器的IP地址。
在主服务器上创建一个用于复制的用户。
sudo -u postgres psql
在psql命令行中执行:
CREATE USER replica_user WITH REPLICATION PASSWORD 'your_password';
ALTER USER replica_user WITH SUPERUSER CREATEDB CREATEROLE;
将your_password
替换为你选择的密码。
在主服务器上重启PostgreSQL服务以应用更改。
sudo systemctl restart postgresql
在从服务器上编辑postgresql.conf
文件。
sudo nano /etc/postgresql/<version>/main/postgresql.conf
找到并修改以下参数:
listen_addresses = '*' # 允许所有IP连接
hot_standby = on # 启用热备份模式
保存并退出编辑器。
在从服务器上创建或编辑recovery.conf
文件。
sudo nano /etc/postgresql/<version>/main/recovery.conf
添加以下内容:
standby_mode = 'on'
primary_conninfo = 'host=<master_ip> dbname=postgres user=replica_user password=your_password'
restore_command = 'cp /var/lib/postgresql/<version>/main/wal_archive/%f %p'
trigger_file = '/tmp/postgresql.trigger.5432'
将<master_ip>
替换为主服务器的IP地址,<version>
替换为PostgreSQL版本号。
在从服务器上启动PostgreSQL服务。
sudo systemctl start postgresql
在主服务器上创建一个数据库或表,然后在从服务器上检查是否同步。
-- 在主服务器上
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 Data');
-- 在从服务器上
sudo -u postgres psql
\l # 列出数据库
\dt # 列出表
SELECT * FROM test_table;
如果从服务器上能看到相同的数据,说明复制配置成功。
max_wal_senders
和wal_keep_segments
参数。通过以上步骤,你应该能够在Debian系统下成功配置PostgreSQL复制。