前面我们已经搭建和安装好一台配置有 PostgreSQL 服务的实例,按照这个方法安装好另外一台服务器,其 IP 地址为 192.168.30.141。接下来我们配置 PostgreSQL 的流复制。
在服务器 141 上
[root@localhost anolisos]# cd ~
[root@localhost ~]# su - postgres
[postgres@localhost ~]$ cd /usr/local/pgsql-14/data/
[postgres@localhost data]$ cp postmaster.opts ../
[postgres@localhost data]$ rm -rf ./*
[postgres@localhost data]$ pg_basebackup -h 192.168.30.140 -p 5432 -U postgres -Fp -P -X stream -R -D /usr/local/pgsql-14/data/
Password:
67659/67659 kB (100%), 1/1 tablespace
[postgres@localhost data]$ mv ../postmaster.opts ./
[postgres@localhost data]$ pg_ctl restart -D /usr/local/pgsql-14/data/
在服务器 140 上
[postgres@localhost data]$ ps -ef | grep wal
postgres 3456 3451 0 14:22 ? 00:00:00 postgres: walwriter
postgres 3515 3451 0 14:27 ? 00:00:00 postgres: walsender postgres 192.168.30.141(34042) streaming 0/4000060
postgres 3559 3208 0 14:29 pts/0 00:00:00 grep --color=auto wal
[postgres@localhost data]$
在服务器 141 上
[postgres@localhost data]$ ps -ef | grep wal
postgres 3396 3390 0 14:27 ? 00:00:00 postgres: walreceiver streaming 0/4000060
postgres 3416 3086 0 14:29 pts/0 00:00:00 grep --color=auto wal
[postgres@localhost data]$
在服务器 140 上
[postgres@localhost data]$ psql
psql (14.7)
Type "help" for help.
postgres=# \x
Expanded display is on.
postgres=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid | 3515
usesysid | 10
usename | postgres
application_name | walreceiver
client_addr | 192.168.30.141
client_hostname |
client_port | 34042
backend_start | 2024-08-12 14:27:47.202992+08
backend_xmin |
state | streaming
sent_lsn | 0/4000060
write_lsn | 0/4000060
flush_lsn | 0/4000060
replay_lsn | 0/4000060
write_lag |
flush_lag |
replay_lag |
sync_priority | 0
sync_state | async
reply_time | 2024-08-12 14:31:07.686188+08
postgres=#
到这一步 PostgreSQL 流复制搭建成功。可以自己创建库、表然后分别在 140 141 上观察。