PostgreSQL14.6主从模式配置

一、系统架构

roleip
master11.0.1.11
slave11.0.1.12

二、编译安装PostgreSQL
不多描述,参考:https://zhaoll.blog.csdn.net/article/details/128416411
注意:两台服务器的PG,master正常安装和初始化,slave无需初始化,编译安装完就行;

三、master配置:
修改ppostgresql.conf,下面只列出了需要修改的参数,其他未列出的参数使用默认值。

data_directory = '/data/pgsql'
hba_file = '/app/postgresql/conf/pg_hba.conf'
log_directory = '/app/postgresql/logs'
listen_addresses = '11.0.1.11'
port = 15432
max_connections = 10000 
unix_socket_directories = '/app/postgresql'
unix_socket_group = 'postgres'
unix_socket_permissions = 0777
wal_level = replica 
archive_mode = on
archive_command = 'test ! -f /app/postgresql/archive/%f && cp %p /app/postgresql/archive/%f'
max_wal_senders = 10
wal_keep_size = 256 
wal_sender_timeout = 60s 

注意上面“archive_command”参数中涉及到的目录,如果不存在,需要你手动创建,不然PG无法启动:

mkdir /app/postgresql/archive

修改pg_hba.conf,增加如下内容,允许slave进行数据同步:

host    replication     repuser         11.0.1.12/24         md5

然后启动PG,进行以下操作:
1、登录PG库:

psql -h 127.0.0.1 -p 15432

2、创建用户,用于进行主从同步操作

create role repuser login encrypted password 'repuser123' replication;

3、查看用户权限

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 itpux     |                                                            | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 repuser   | Replication                                                | {}

四、slave配置
1、同步数据
在slave服务器上执行:

[postgres@data3 postgresql]$ bin/pg_basebackup -h 11.0.1.11 -p 15432 -U repuser -F p -P -D /data/pgsql/
Password:                       <<<<此处输入我们在master上创建的repuser的密码
34772/34772 kB (100%), 1/1 tablespace

输入密码后就开始从master上同步数据到slave上,同步完成后可以看到同步后的文件:

[postgres@data3 postgresql]$ ll /data/pgsql/
总用量 240
-rw-------. 1 postgres postgres    225 5月   2 15:30 backup_label
-rw-------. 1 postgres postgres 180267 5月   2 15:30 backup_manifest
drwx------. 6 postgres postgres     54 5月   2 15:30 base
drwx------. 2 postgres postgres   4096 5月   2 15:30 global
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_commit_ts
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_dynshmem
-rw-------. 1 postgres postgres   4928 5月   2 15:30 pg_hba.conf
-rw-------. 1 postgres postgres   1636 5月   2 15:30 pg_ident.conf
drwx------. 4 postgres postgres     68 5月   2 15:30 pg_logical
drwx------. 4 postgres postgres     36 5月   2 15:30 pg_multixact
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_notify
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_replslot
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_serial
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_snapshots
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_stat
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_stat_tmp
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_subtrans
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_tblspc
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_twophase
-rw-------. 1 postgres postgres      3 5月   2 15:30 PG_VERSION
drwx------. 3 postgres postgres     60 5月   2 15:30 pg_wal
drwx------. 2 postgres postgres     18 5月   2 15:30 pg_xact
-rw-------. 1 postgres postgres     88 5月   2 15:30 postgresql.auto.conf
-rw-------. 1 postgres postgres  28868 5月   2 15:30 postgresql.conf

2、修改配置

su - postgres
cd /app/postgresql
mkdir conf && cd conf
ln -s /data/pgsql/postgresql.conf .
ln -s /data/pgsql/pg_hba.conf .

修改postgresql.conf,只需修改两处:

listen_addresses = '11.0.1.12'
primary_conninfo = 'host=11.0.1.11 port=15432 user=repuser password=Elk@95598'

然后启动slave上的PG库,查看日志有没有报错。

五、测试
在master上登录PG并创建testdb库,在slave上查看是否也存在testdb库。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dustin.Hoffman

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值