PostgreSQL部署邏輯復(fù)制過程詳解
1.環(huán)境準(zhǔn)備
| 角色 | 主機(jī)名 | IP | 端口 | 數(shù)據(jù)庫名 | 用戶名 | 版本 |
|---|---|---|---|---|---|---|
| 發(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ù)說明 wal_level設(shè)置為logical,才支持邏輯復(fù)制,低于這個(gè)級別邏輯復(fù)制不能工作。 max_replication_slots設(shè)置值必須大于訂閱的數(shù)量。 max_wal_senders設(shè)置值必須大于max_replication_slots參數(shù)值加上物理備庫數(shù),因?yàn)槊總€(gè)訂閱在主庫上都會占用主庫一個(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ù)說明 max_replication_slots設(shè)置數(shù)據(jù)庫復(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會消耗后臺進(jìn)程數(shù),并且從max_worker_processes參數(shù)設(shè)置的后臺進(jìn)程數(shù)中消費(fèi),因此max_worker_processes需要設(shè)置的大一些。
5.發(fā)布端創(chuàng)建邏輯復(fù)制用戶,并具備replication復(fù)制權(quán)限(可選)
如不創(chuàng)建,可以使用默認(rèn)的管理員用戶postgres。
postgres=# create user replic replication login connection limit 8 password 'replic'; CREATE ROLE limit 8:為新用戶設(shè)置最大數(shù)目連接數(shù)。默認(rèn)無限制。
6.發(fā)布端創(chuàng)建發(fā)布
## 創(chuàng)建復(fù)制數(shù)據(jù)庫 postgres=# create database pubdb; CREATE DATABASE ## 授予復(fù)制用戶權(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ā)布多張表使用逗號隔開,如果發(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ù)說明: pubname:發(fā)布名稱。 pubowner:發(fā)布的屬主,可以和pg_user視圖的usesysid字段關(guān)聯(lián)查詢屬主的具體信息。 puballtables:是否發(fā)布數(shù)據(jù)庫中的所有表,t 表示發(fā)布數(shù)據(jù)庫中所有已存在的表和以后新建的表。 pubinsert:t 表示僅發(fā)布表上的insert操作。 pubupdate:t 表示僅發(fā)布表上的update操作。 pubdelete:t 表示僅發(fā)布表上的delete操作。 pubtruncate:t 表示僅發(fā)布表上的truncate操作。
7.發(fā)布端給復(fù)制用戶授權(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ù)制用戶授權(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.測試邏輯復(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)
## 添加新表測試,發(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)
## 如果沒有看到新表,可在訂閱端刷新訂閱
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- docker快速部署postgresql的完整步驟記錄
- Docker 部署 PostgreSQL數(shù)據(jù)庫的兩種方式
- Docker部署PostgreSQL數(shù)據(jù)庫及操作方法詳解
- Docker PostgreSQL容器化部署方式
- Docker Compose安裝部署PostgreSQL數(shù)據(jù)庫的實(shí)現(xiàn)步驟
- 在docker上部署postgreSQL主從的超詳細(xì)步驟
- 一步步教你用docker部署postgreSQL數(shù)據(jù)庫
- PostgreSQL數(shù)據(jù)庫遷移部署實(shí)戰(zhàn)教程
- postgresql 12版本搭建及主備部署操作
- postgresql數(shù)據(jù)庫安裝部署搭建主從節(jié)點(diǎn)的詳細(xì)過程(業(yè)務(wù)庫)
- RockyLinux9.5部署PostgreSQL的實(shí)現(xiàn)步驟
相關(guān)文章
PostgreSQL教程(四):數(shù)據(jù)類型詳解
這篇文章主要介紹了PostgreSQL教程(四):數(shù)據(jù)類型詳解,本文講解了數(shù)值類型、字符類型、布爾類型、位串類型、數(shù)組、復(fù)合類型等數(shù)據(jù)類型,需要的朋友可以參考下2015-05-05
PostgreSQL 16 搭配 PgVector:Windows 11&nbs
PgVector助力PostgreSQL實(shí)現(xiàn)向量數(shù)據(jù)庫功能,簡化AI檢索架構(gòu);支持多種向量類型與距離算法,輕松實(shí)現(xiàn)多媒體檢索與個(gè)性化推薦;適合Windows及Linux環(huán)境部署,感興趣的朋友跟隨小編一起看看吧2026-05-05
PostgreSQL中擴(kuò)展moddatetime的使用
PostgreSQL的moddatetime擴(kuò)展通過觸發(fā)器自動維護(hù)時(shí)間戳字段,輕量高效,適用于審計(jì)日志和多租戶系統(tǒng),具有一定的參考價(jià)值,感興趣的可以了解一下2025-06-06
PostgreSQL regexp_matches替換like模糊查詢的操作
這篇文章主要介紹了PostgreSQL regexp_matches替換like模糊查詢的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
pg中replace和translate的用法說明(數(shù)據(jù)少的中文排序)
這篇文章主要介紹了pg中replace和translate的用法說明(數(shù)據(jù)少的中文排序),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
PostgreSQL數(shù)據(jù)庫備份與恢復(fù)的四種辦法
在數(shù)據(jù)為王的時(shí)代,數(shù)據(jù)庫中存儲的信息堪稱企業(yè)的生命線,而PostgreSQL作為一款廣泛應(yīng)用的開源數(shù)據(jù)庫,學(xué)會如何妥善進(jìn)行備份與恢復(fù)操作,是每個(gè)開發(fā)者與運(yùn)維人員必備的技能,今天,咱們就深入探究一下PostgreSQL相關(guān)的備份恢復(fù)策略,并附上豐富的代碼示例2025-01-01
PostgreSQL中查看當(dāng)前時(shí)間和日期的幾種常用方法
在 PostgreSQL 中,有多個(gè)函數(shù)可以用來查看當(dāng)前時(shí)間和日期,這些函數(shù)在處理時(shí)間戳、日期和時(shí)間的計(jì)算時(shí)非常有用,以下是幾種常用的查看當(dāng)前時(shí)間和日期的函數(shù)及示例,需要的朋友可以參考下2024-10-10

