一、獲取安裝包
posegresql安裝界面找到*Advanced users* can also download a [tar.gz archive](https://www.enterprisedb.com/download-postgresql-binaries) of the binaries, without the installer.后點(diǎn)擊連接[tar.gz archive]即可進(jìn)入下載界面,下載后不需要編譯直接解壓即可。
二、配置文件
1、解壓后目錄:/opt/pgsql
2、創(chuàng)建目錄:/opt/pgsql/data/pg_root
3、查看版本:postgres --version
4、切換到postgres用戶初始化數(shù)據(jù)庫:
su postgres
echo "123456" >> /data/pgpass
initdb -D /data/pg10/pg_root -E UTF8 --locale=C -U postgres --pwfile=/data/pgpass
5、修改主庫/opt/pgsql/data/pg_root/postgresql.conf文件(主備庫保持一致即可):
listen_addresses = '*'
wal_level = hot_standby
archive_mode = on
archive_command = '/bin/date'
max_wal_senders = 10
wal_keep_segments = 512
hot_standby = on
6、(備庫先執(zhí)行1-4步驟)修改備庫上的配置文件開啟遠(yuǎn)程訪問/opt/pgsql/data/pg_root/pg_hba.conf
host all all 192.168.169.0/24 trust
host replication repuser 192.168.169.0/24 trust
三、創(chuàng)建備份用戶
1、啟動(dòng)主庫:pg_ctl start -D $PGDATA -l logfile
2、使用管理員用戶創(chuàng)建備份用戶:
[root@master pgsql]:psql -U postgres -p 5432
postgres=# CREATE USER repuser REPLICATION LOGIN CONNECTION LIMIT 5 ENCRYPTED PASSWORD 'domac123';
四、使用 pg_basebackup 方式部署流復(fù)制
1、在備服務(wù)器上停止psql服務(wù):pg_ctl stop -m fast
2、刪除/opt/pgsql/data/pg_root目錄:rm -rf /opt/pgsql/data/pg_root
3、使用pg_basebackup觸發(fā)基準(zhǔn)備份:
pg_basebackup -D $PGDATA -Fp -Xs -v -P -h 192.168.169.91 -p 5432 -U repuser -W
4、備庫配置recovery.conf
recovery_target_timeline = 'latest'
standby_mode = on
primary_conninfo = 'host=192.168.169.91 port=5432 user=repuser'
五、測試
1、主庫上創(chuàng)建test表,并插入了一條數(shù)據(jù):
postgres=# create table test(id int4);
postgres=# insert into test values(6);
2、在備庫上進(jìn)行查詢觀察是否同步成功:
postgres=# select * from test;
3、主庫刪除數(shù)據(jù):
postgres=# delete from test where id=6;
postgres=# insert into test values(9);
4、備庫查詢:postgres=# select * from test;
5、備庫插入數(shù)據(jù)提示:cannot execute INSERT in a read-only transaction
六、主備切換
1、關(guān)閉主庫:pg_ctl stop -m fast
2、備庫上執(zhí)行pg_ctl promote命令激活備庫:pg_ctl promote -D $PGDATA,命令執(zhí)行后,如果原來的 recovery.conf 更名為 recovery.done, 表示切換成功
3、在老的主庫的$PGDATA目錄下也創(chuàng)建recovery.conf文件內(nèi)容和原備庫內(nèi)容一致,更換下IP地址即可。
4、啟動(dòng)主庫
5、檢測主備情況,分別在主備機(jī)上執(zhí)行:select pg_is_in_recovery();,返回值f代表主庫,返回值t代表備庫
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。