安装包下载地址
wget https://ftp.postgresql.org/pub/source/v14.2/postgresql-14.2.tar.gz
一、安装服务
1、上传安装包,解压(主从)
tar -zxvf postgresql-14.2.tar
2、编译安装(主从)
yum -y install gcc-c++ zlib zlib-devel readline readline-devel
#创建安装目录
mkdir-p /data/postgresql
#切换到解压得postgresql目录下:
./configure --prefix=/data/postgresql
#编译安装
make && make install
3、创建用户组和用户,赋予权限(主从)
groupadd postgres
useradd -g postgres postgres
4、创建数据库文件存储目录,给postgres赋予权限(主从)
mkdir /data/postgresql/data
chown-R postgres.postgres /data/postgresql
chmod750 /data/postgresql
5、配置环境变量(主从)
#进入编辑:vim /etc/profile
exportPGDATA=/data/postgresql/data
exportPGHOME=/data/postgresql
exportPATH=$PGHOME/bin:$PATH
#更新配置
source /etc/profile
6、切换到postgres用户,初始化数据库(主)
su- postgres
/data/postgresql/bin/initdb -D /data/postgresql/data
7、配置监听地址和端口(主)
vim /data/postgresql/data/postgresql.conf
#修改内容
listen_addresses ='*'
port =5432
8、允许远程连接(主)
vim /data/postgresql/data/pg_hba.conf
#配置网段
#IPv4
host all all 192.168.76.0/24 md5
9、启动数据库(主)
/data/postgresql/bin/pg_ctl -D /data/postgresql/data/ start
psql
二、主库配置
1、修改配置文件postgresql.conf
listen_addresses ='*'
port =5432
max_connections =100
max_worker_processes =16
wal_level = replica
archive_mode = on
archive_command ='test ! -f /data/postgresql/data/log/%f && cp %p /data/postgresql/data/log/%f'
max_wal_senders =16
wal_sender_timeout = 60s
hot_standby = on
max_logical_replication_workers =10
autovacuum_max_workers =2
#日志设置
wal_log_hints = on
log_destination ='stderr'
logging_collector = on
log_directory ='/data/postgresql/log'
log_filename ='postgresql-%w.log'
log_file_mode =0600
log_rotation_age = 1d
log_rotation_size = 500MB
log_truncate_on_rotation = on
log_min_duration_statement =300
log_checkpoints = on
log_connections = on
log_disconnections = on
log_error_verbosity = verbose
log_hostname = on
log_line_prefix ='%m [%p] '
log_lock_waits = on
log_statement ='ddl'
2、修改配置文件pg_hba.conf
#添加从库的网段
#IPv4
host replication repl 192.168.76.0/24 md5
# replication privilege.
host replication replica 192.168.76.0/24 trust
3、重启主库,主库创建具有流复制权限的用户replica
su- postgres
/data/postgresql/bin/pg_ctl -D /data/postgresql/data/ restart
#创建用户
psql
create user replica login replication encrypted password'replica';
#查看
\du;
三、从库配置(从库不需要初始化数据库)
1、编译安装后不需要初始化
#创建日志目录
mkdir /data/postgresql/log
chown-R postgres.postgres /data/postgresql/log/
2、拉去主库配置
pg_basebackup -h192.168.76.10 -p5432-U replica -W-R-Fp-Xs-Pv-D /data/postgresql/data/
passowrd:#密码就是replica用户的密码
3、配置postgresql.conf
#配置文件是从主上面拉取的,所以有些配置需要改动,除了下面几条配置,其他配置和主库一样
#需要开启
restore_command ='cp /data/postgresql/data/%f %p'
max_standby_streaming_delay = 30s
wal_receiver_status_interval = 10s
hot_standby_feedback = off`
#需要注释掉的
wal_log_hints = on
4、重启从库
su- postgres
/data/postgresql/bin/pg_ctl -D /data/postgresql/data/ restart
四、验证主从同步,在主库上面查询
su- postgres
psql
selectclient_addr,usename,backend_start,application_name,sync_state,sync_priority FROM pg_stat_replication;
#查询结果会出现从库的ip client_addr | usename | backend_start | application_name | sync_state | sync_priority---------------+---------+------------------------------+------------------+------------+---------------
192.168.76.20 | replica | 2023-01-0512:38:23.99815+08 | walreceiver | async | 0(1 row)
#下一步验证主从是否同步
#在主库创建一个名为test库
create database test;
#查看test库
\l
#在从库上面查询test库
\l
五、验证主从切换
1、查看主库状态
pg_controldata | grep'Database cluster state'
Database cluster state: in production
2、查看从库状态
pg_controldata | grep'Database cluster state'
Database cluster state: in archive recovery
3、停掉主库
pg_ctl stop
#再次查看主库的状态
pg_controldata | grep'Database cluster state'
Database cluster state: shut down
4、提升从库为主
#在从库上面执行
pg_ctl promote
#查看从库的状态
pg_controldata | grep'Database cluster state'
Database cluster state: in production
5、验证主从是否切换成功
#在新主库上插入数据
#进入库
\c test
#创建表
create table student(
test(# id serial primary key,
test(# name varchar(12) not null,
test(# age integer not null);
#插入数据
insert into student (id,name,age) values (1,'小明',12);
#查看数据
select * from student;
6、在原主库上面创建standby.signal文件,在编辑postgresql.conf配置文件
touch /data/postgresql/data/standby.signal
#编辑原主 vim postgresql.conf
primary_conninfo ='host=新主库的ip port=5432 user=replica password=replica'
#编辑新主 postgresql.conf
primary_conninfo ='host=原主库的ip port=5432 user=replica password=replica'
7、启动原主库
pg_ctl start
#查看数据
\c test
select * from student;