PostgreSQL部署邏輯復(fù)制過(guò)程詳解
1.環(huán)境準(zhǔn)備
角色 | 主機(jī)名 | IP | 端口 | 數(shù)據(jù)庫(kù)名 | 用戶(hù)名 | 版本 |
---|---|---|---|---|---|---|
發(fā)布端 | postgresql | 192.168.80.239 | 5432 | pubdb | replic | postgresql 15 |
訂閱端 | postgresql2 | 192.168.80.240 | 5432 | subdb | replic | postgresql 15 |
2.發(fā)布端配置參數(shù)
## vi postgressql.conf(重啟生效) listen_addresses = '*' wal_level=logical max_replication_slots=8 max_wal_senders=10 ## alter system set alter system set wal_level=logical; ## 參數(shù)說(shuō)明 wal_level設(shè)置為logical,才支持邏輯復(fù)制,低于這個(gè)級(jí)別邏輯復(fù)制不能工作。 max_replication_slots設(shè)置值必須大于訂閱的數(shù)量。 max_wal_senders設(shè)置值必須大于max_replication_slots參數(shù)值加上物理備庫(kù)數(shù),因?yàn)槊總€(gè)訂閱在主庫(kù)上都會(huì)占用主庫(kù)一個(gè)wal發(fā)送進(jìn)程。
3.發(fā)布端配置pg_hba.conf
vi pg_hba.conf host replication test 0/0 md5
4.訂閱端配置參數(shù)
## vi postgresql.conf(重啟生效) listen_addresses = '*' wal_level=logical max_replication_slots=8 max_logical_replication_workers=8 ## alter system set alter system set wal_level=logical; ## 參數(shù)說(shuō)明 max_replication_slots設(shè)置數(shù)據(jù)庫(kù)復(fù)制槽數(shù)量。 max_logical_replication_workers設(shè)置邏輯復(fù)制進(jìn)程數(shù),應(yīng)大于訂閱節(jié)點(diǎn)的數(shù)量,并且給表同步預(yù)留一些進(jìn)程數(shù)量。 注意:max_logical_replication_workers會(huì)消耗后臺(tái)進(jìn)程數(shù),并且從max_worker_processes參數(shù)設(shè)置的后臺(tái)進(jìn)程數(shù)中消費(fèi),因此max_worker_processes需要設(shè)置的大一些。
5.發(fā)布端創(chuàng)建邏輯復(fù)制用戶(hù),并具備replication復(fù)制權(quán)限(可選)
如不創(chuàng)建,可以使用默認(rèn)的管理員用戶(hù)postgres。
postgres=# create user replic replication login connection limit 8 password 'replic'; CREATE ROLE limit 8:為新用戶(hù)設(shè)置最大數(shù)目連接數(shù)。默認(rèn)無(wú)限制。
6.發(fā)布端創(chuàng)建發(fā)布
## 創(chuàng)建復(fù)制數(shù)據(jù)庫(kù) postgres=# create database pubdb; CREATE DATABASE ## 授予復(fù)制用戶(hù)權(quán)限 postgres=# \c pubdb postgres You are now connected to database "pubdb" as user "postgres". pubdb=# grant all on schema public to replic; GRANT ## 創(chuàng)建復(fù)制表 pubdb=> create table c1 (id int4 primary key,name text); CREATE TABLE pubdb=> insert into c1 values (1,'a'); INSERT 0 1 pubdb=> select * from c1; id | name ----+------ 1 | a (1 row) ## 創(chuàng)建發(fā)布 pubdb=> \c pubdb postgres You are now connected to database "pubdb" as user "postgres". pubdb=# create publication pub1 for table c1; CREATE PUBLICATION 注意:如果發(fā)布多張表使用逗號(hào)隔開(kāi),如果發(fā)布所有表則將 for table 修改為 for all tables。 ##查看創(chuàng)建的發(fā)布 pubdb=# select * from pg_publication; oid | pubname | pubowner | puballtables | pubinsert | pubupdate | pubdelete | pubtruncate | pubviaroot -------+---------+----------+--------------+-----------+-----------+-----------+-------------+------------ 33177 | pub1 | 10 | f | t | t | t | t | f (1 row) 參數(shù)說(shuō)明: pubname:發(fā)布名稱(chēng)。 pubowner:發(fā)布的屬主,可以和pg_user視圖的usesysid字段關(guān)聯(lián)查詢(xún)屬主的具體信息。 puballtables:是否發(fā)布數(shù)據(jù)庫(kù)中的所有表,t 表示發(fā)布數(shù)據(jù)庫(kù)中所有已存在的表和以后新建的表。 pubinsert:t 表示僅發(fā)布表上的insert操作。 pubupdate:t 表示僅發(fā)布表上的update操作。 pubdelete:t 表示僅發(fā)布表上的delete操作。 pubtruncate:t 表示僅發(fā)布表上的truncate操作。
7.發(fā)布端給復(fù)制用戶(hù)授權(quán)
pubdb=# grant connect on database pubdb to replic; GRANT pubdb=# grant usage on schema public to replic; GRANT pubdb=# grant select on c1 to replic; GRANT
8.訂閱端創(chuàng)建表
postgres=# create database subdb; CREATE DATABASE postgres=# create user replic replication login connection limit 8 password 'replic'; CREATE ROLE subdb=> \c subdb postgres You are now connected to database "subdb" as user "postgres". subdb=# grant all on schema public to replic; GRANT subdb=> create table c1 (id int4 primary key,name text); CREATE TABLE
9.訂閱端創(chuàng)建訂閱
subdb=> \c subdb postgres You are now connected to database "subdb" as user "postgres". subdb=# create subscription sub1 connection 'host=192.168.80.239 port=5432 dbname=pubdb user=replic password=replic' publication pub1; NOTICE: created replication slot "sub1" on publisher CREATE SUBSCRIPTION ## 查看創(chuàng)建的訂閱 subdb=# \x Expanded display is on. subdb=# select * from pg_subscription; -[ RECORD 1 ]----+----------------------------------------------------------------------- oid | 41374 subdbid | 41361 subskiplsn | 0/0 subname | sub1 subowner | 10 subenabled | t subbinary | f substream | f subtwophasestate | d subdisableonerr | f subconninfo | host=192.168.80.239 port=5432 dbname=pubdb user=replic password=replic subslotname | sub1 subsynccommit | off subpublications | {pub1}
10.訂閱端給復(fù)制用戶(hù)授權(quán)
subdb=# grant connect on database subdb to replic; GRANT subdb=# grant usage on schema public to replic; GRANT subdb=# grant select on c1 to replic; GRANT
11.配置完成,發(fā)布端查看信息
postgres=# select slot_name,plugin,slot_type,database,active,restart_lsn from pg_replication_slots where slot_name='sub1'; slot_name | plugin | slot_type | database | active | restart_lsn -----------+----------+-----------+----------+--------+------------- sub1 | pgoutput | logical | pubdb | t | 0/3F45C840 (1 row)
12.測(cè)試邏輯復(fù)制
## 發(fā)布端向表中插入數(shù)據(jù) pubdb=> insert into c1 values (2,'tt'); INSERT 0 1 pubdb=> select * from c1; id | name ----+------ 1 | a 2 | tt (2 rows) pubdb=> delete from c1 where id=1; DELETE 1 pubdb=> select * from c1; id | name ----+------ 2 | tt (1 row) ## 訂閱端查看結(jié)果 subdb=# select * from c1; id | name ----+------ 2 | tt (1 row) ## 添加新表測(cè)試,發(fā)布端創(chuàng)建表結(jié)構(gòu) pubdb=> create table c2 (id int primary key,addr varchar(100)); CREATE TABLE ## 訂閱端創(chuàng)建表結(jié)構(gòu) subdb=> create table c2 (id int primary key,addr varchar(100)); CREATE TABLE ## 發(fā)布端授權(quán) pubdb=> grant select on c2 to replic; GRANT ## 將新表c2,添加到發(fā)布列表中 pubdb=> \c pubdb postgres You are now connected to database "pubdb" as user "postgres". pubdb=# alter publication pub1 add table c2; ALTER PUBLICATION ## 發(fā)布端查看發(fā)布列表 pubdb=# select * from pg_publication_tables; pubname | schemaname | tablename | attnames | rowfilter ---------+------------+-----------+-----------+----------- pub1 | public | c1 | {id,name} | pub1 | public | c2 | {id,addr} | (2 rows) ## 如果沒(méi)有看到新表,可在訂閱端刷新訂閱 subdb=> \c subdb postgres You are now connected to database "subdb" as user "postgres". subdb=# alter subscription sub1 refresh publication; ALTER SUBSCRIPTION ## 刪除復(fù)制設(shè)置 drop subscription sub1;
到此這篇關(guān)于PostgreSQL部署邏輯復(fù)制的文章就介紹到這了,更多相關(guān)PostgreSQL部署內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- PostgreSQL數(shù)據(jù)庫(kù)遷移部署實(shí)戰(zhàn)教程
- 關(guān)于Docker部署postgresql數(shù)據(jù)庫(kù)的問(wèn)題
- postgresql 12版本搭建及主備部署操作
- postgresql數(shù)據(jù)庫(kù)安裝部署搭建主從節(jié)點(diǎn)的詳細(xì)過(guò)程(業(yè)務(wù)庫(kù))
- PostgreSQL中Slony-I同步復(fù)制部署教程
- Windows?環(huán)境搭建?PostgreSQL?邏輯復(fù)制高可用架構(gòu)數(shù)據(jù)庫(kù)服務(wù)
- PostgreSQL邏輯復(fù)制解密原理解析
- PostgreSQL 邏輯復(fù)制 配置操作
- postgresql流復(fù)制原理以及流復(fù)制和邏輯復(fù)制的區(qū)別說(shuō)明
相關(guān)文章
Postgre數(shù)據(jù)庫(kù)Insert 、Query性能優(yōu)化詳解
這篇文章主要介紹了Postgre數(shù)據(jù)庫(kù)Insert和Query性能優(yōu)化的步驟,大家可以參考使用2013-11-11PostgreSQL實(shí)現(xiàn)一個(gè)通用標(biāo)簽系統(tǒng)
這篇文章主要給大家介紹了關(guān)于利用PostgreSQL實(shí)現(xiàn)一個(gè)通用標(biāo)簽系統(tǒng)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01postgresql SQL語(yǔ)句變量的使用說(shuō)明
這篇文章主要介紹了postgresql SQL語(yǔ)句變量的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01解決PostgreSQL Array使用中的一些小問(wèn)題
這篇文章主要介紹了解決PostgreSQL Array使用中的一些小問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01PostgreSQL數(shù)據(jù)庫(kù)的基本查詢(xún)操作
這篇文章采用詳細(xì)的代碼示例為大家介紹了PostgreSQL數(shù)據(jù)庫(kù)的基本查詢(xún)操作使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04PostgreSQL中實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)監(jiān)控和預(yù)警的步驟詳解
在 PostgreSQL 中實(shí)現(xiàn)數(shù)據(jù)的實(shí)時(shí)監(jiān)控和預(yù)警是確保數(shù)據(jù)庫(kù)性能和數(shù)據(jù)完整性的關(guān)鍵任務(wù),以下將詳細(xì)討論如何實(shí)現(xiàn)此目標(biāo),并提供相應(yīng)的解決方案和具體示例,需要的朋友可以參考下2024-07-07PostgreSQL實(shí)現(xiàn)按年、月、日、周、時(shí)、分、秒的分組統(tǒng)計(jì)
這篇文章介紹了PostgreSQL實(shí)現(xiàn)按年、月、日、周、時(shí)、分、秒分組統(tǒng)計(jì)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06