PostgreSQL部署逻辑复制过程详解

 更新时间:2024年04月29日 12:14:18   作者:Floating warm sun  
这篇文章主要介绍了PostgreSQL部署逻辑复制过程详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

1.环境准备

角色主机名IP端口数据库名用户名版本
发布端postgresql192.168.80.2395432pubdbreplicpostgresql 15
订阅端postgresql2192.168.80.2405432subdbreplicpostgresql 15

2.发布端配置参数

## 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;
## 参数说明
wal_level设置为logical,才支持逻辑复制,低于这个级别逻辑复制不能工作。
max_replication_slots设置值必须大于订阅的数量。
max_wal_senders设置值必须大于max_replication_slots参数值加上物理备库数,因为每个订阅在主库上都会占用主库一个wal发送进程。

3.发布端配置pg_hba.conf

vi pg_hba.conf
host    replication     test       0/0         md5

4.订阅端配置参数

## 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;
## 参数说明
max_replication_slots设置数据库复制槽数量。
max_logical_replication_workers设置逻辑复制进程数,应大于订阅节点的数量,并且给表同步预留一些进程数量。
注意:max_logical_replication_workers会消耗后台进程数,并且从max_worker_processes参数设置的后台进程数中消费,因此max_worker_processes需要设置的大一些。

5.发布端创建逻辑复制用户,并具备replication复制权限(可选)

如不创建,可以使用默认的管理员用户postgres。

postgres=# create user replic replication login connection limit 8 password 'replic';
CREATE ROLE
limit 8:为新用户设置最大数目连接数。默认无限制。

6.发布端创建发布

## 创建复制数据库
postgres=# create database pubdb;
CREATE DATABASE
## 授予复制用户权限
postgres=# \c pubdb postgres
You are now connected to database "pubdb" as user "postgres".
pubdb=# grant all on schema public to replic;
GRANT
## 创建复制表
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)
## 创建发布
pubdb=> \c pubdb postgres
You are now connected to database "pubdb" as user "postgres".
pubdb=# create publication pub1 for table c1;
CREATE PUBLICATION
注意:如果发布多张表使用逗号隔开,如果发布所有表则将 for table 修改为 for all tables。
##查看创建的发布
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)
参数说明:
pubname:发布名称。
pubowner:发布的属主,可以和pg_user视图的usesysid字段关联查询属主的具体信息。
puballtables:是否发布数据库中的所有表,t 表示发布数据库中所有已存在的表和以后新建的表。
pubinsert:t 表示仅发布表上的insert操作。
pubupdate:t 表示仅发布表上的update操作。
pubdelete:t 表示仅发布表上的delete操作。
pubtruncate:t 表示仅发布表上的truncate操作。

7.发布端给复制用户授权

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.订阅端创建表

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.订阅端创建订阅

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
## 查看创建的订阅
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.订阅端给复制用户授权

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.配置完成,发布端查看信息

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.测试逻辑复制

## 发布端向表中插入数据
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)
## 订阅端查看结果
subdb=# select * from c1;
 id | name 
----+------
  2 | tt
(1 row)
## 添加新表测试,发布端创建表结构
pubdb=> create table c2 (id int primary key,addr varchar(100));
CREATE TABLE
## 订阅端创建表结构
subdb=> create table c2 (id int primary key,addr varchar(100));
CREATE TABLE
## 发布端授权
pubdb=> grant select on c2 to replic;
GRANT
## 将新表c2,添加到发布列表中
pubdb=> \c pubdb postgres 
You are now connected to database "pubdb" as user "postgres".
pubdb=# alter publication pub1 add table c2;
ALTER PUBLICATION
## 发布端查看发布列表
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
## 删除复制设置
drop subscription sub1;

到此这篇关于PostgreSQL部署逻辑复制的文章就介绍到这了,更多相关PostgreSQL部署内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • pgsql的UUID生成函数实例

    pgsql的UUID生成函数实例

    这篇文章主要介绍了pgsql的UUID生成函数实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • PostgreSQL教程(四):数据类型详解

    PostgreSQL教程(四):数据类型详解

    这篇文章主要介绍了PostgreSQL教程(四):数据类型详解,本文讲解了数值类型、字符类型、布尔类型、位串类型、数组、复合类型等数据类型,需要的朋友可以参考下
    2015-05-05
  • PostgreSQL 远程连接配置操作

    PostgreSQL 远程连接配置操作

    这篇文章主要介绍了PostgreSQL 远程连接配置操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • PostgreSQL 16 搭配 PgVector:Windows 11 完整安装教程

    PostgreSQL 16 搭配 PgVector:Windows 11&nbs

    PgVector助力PostgreSQL实现向量数据库功能,简化AI检索架构;支持多种向量类型与距离算法,轻松实现多媒体检索与个性化推荐;适合Windows及Linux环境部署,感兴趣的朋友跟随小编一起看看吧
    2026-05-05
  • PostgreSQL中扩展moddatetime的使用

    PostgreSQL中扩展moddatetime的使用

    PostgreSQL的moddatetime扩展通过触发器自动维护时间戳字段,轻量高效,适用于审计日志和多租户系统,具有一定的参考价值,感兴趣的可以了解一下
    2025-06-06
  • PostgreSQL regexp_matches替换like模糊查询的操作

    PostgreSQL regexp_matches替换like模糊查询的操作

    这篇文章主要介绍了PostgreSQL regexp_matches替换like模糊查询的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • pg中replace和translate的用法说明(数据少的中文排序)

    pg中replace和translate的用法说明(数据少的中文排序)

    这篇文章主要介绍了pg中replace和translate的用法说明(数据少的中文排序),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • PostgreSQL数据库备份与恢复的四种办法

    PostgreSQL数据库备份与恢复的四种办法

    在数据为王的时代,数据库中存储的信息堪称企业的生命线,而PostgreSQL作为一款广泛应用的开源数据库,学会如何妥善进行备份与恢复操作,是每个开发者与运维人员必备的技能,今天,咱们就深入探究一下PostgreSQL相关的备份恢复策略,并附上丰富的代码示例
    2025-01-01
  • Postgresql的docker部署及图形化界面管理

    Postgresql的docker部署及图形化界面管理

    PostgreSQL 是一款功能强大的开源关系型数据库,以其高度的标准兼容性和可扩展性而闻名,本文将从介绍postgresql的部署,包括nas服务端与windows可视化软件,感兴趣的朋友跟随小编一起看看吧
    2026-04-04
  • PostgreSQL中查看当前时间和日期的几种常用方法

    PostgreSQL中查看当前时间和日期的几种常用方法

    在 PostgreSQL 中,有多个函数可以用来查看当前时间和日期,这些函数在处理时间戳、日期和时间的计算时非常有用,以下是几种常用的查看当前时间和日期的函数及示例,需要的朋友可以参考下
    2024-10-10

最新评论