在同一个实例(cluster)的两个不同库(database)中进行逻辑同步

news/2024/7/9 19:39:48 标签: 数据库, postgresql

文章目录

  • 配置文件 pg_hba.conf
  • 实例
  • 实际操作过程
  • 参考:

说明: 默认创建发布时会创建对应的复制槽, 但是根据官方文档, 使用一般的方式在同一个 cluster 中创建发布之后, 再在另一个 database 中创建订阅时会hang 住,除非单独使用 pg_create_logical_replication_slot 创建独立的复制槽,并且
在创建订阅的时候, 使用创建的复制槽并使用参数 create_slot=false

Creating a subscription that connects to the same database cluster 
(for example, to replicate between databases in the same cluster or to 
replicate within the same database) will only succeed if the replication slot 
is not created as part of the same command. Otherwise, the CREATE SUBSCRIPTION 
call will hang. To make this work, create the replication slot separately 
(using the function pg_create_logical_replication_slot with the plugin name 
pgoutput) and create the subscription using the parameter create_slot = false.

配置文件 pg_hba.conf

local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

实例

# test
-- drop owned by repl;
-- drop user repl;
-- 创建逻辑复制用户
create user repl with replication password 'repl';

-- 创建测试表
create table t1(id bigserial primary key, info  text);
insert into t1(info) values('info1'),('info2'),('info3');
select * from t1;
-- 授权给逻辑复制用户
grant select on t1 to repl;

-- 创建发布
create publication pub1 for table t1;
-- 创建复制槽
select * from pg_create_logical_replication_slot('slot1','pgoutput') ;


# test2
-- test2 库中创建同名表
create table t1(id bigserial primary key, info  text);
-- 创建订阅
create subscription sub1 connection 'dbname=test host=127.0.0.1 port=1921 user=repl' publication pub1 with (slot_name=slot1,create_slot=false);
select * from t1;

# test
delete from t1 where id=1;
select * from t1;
\c test2
select * from t1;
\c test
update t1 set info='xxx' where id=2;
select * from t1;
\c test2
select * from t1;

实际操作过程

test=# create user repl with replication password 'repl';
CREATE ROLE
test=# create table t1(id bigserial primary key, info  text);
CREATE TABLE
test=# insert into t1(info) values('info1'),('info2'),('info3');
INSERT 0 3
test=# select * from t1;
 id | info  
----+-------
  1 | info1
  2 | info2
  3 | info3
(3 rows)

test=# grant select on t1 to repl;
GRANT
test=# 
test=# 
test=# create publication pub1 for table t1;
CREATE PUBLICATION
test=# 
test=# select * from pg_create_logical_replication_slot('slot1','pgoutput') ;
 slot_name |    lsn    
-----------+-----------
 slot1     | 0/1A33080
(1 row)

test=# \c test2
You are now connected to database "test2" as user "postgres".
test2=# create table t1(id bigserial primary key, info  text);
CREATE TABLE
test2=# create subscription sub1 connection 'dbname=test host=127.0.0.1 port=1921 user=repl' publication pub1 with (slot_name=slot1,create_slot=false);
CREATE SUBSCRIPTION
test2=# select * from t1;
 id | info  
----+-------
  1 | info1
  2 | info2
  3 | info3
(3 rows)

test2=# \c test
You are now connected to database "test" as user "postgres".
test=# 
test=# 
test=# select * from t1;
 id | info  
----+-------
  1 | info1
  2 | info2
  3 | info3
(3 rows)

test=# delete from t1 where id=1;
DELETE 1
test=# select * from t1;
 id | info  
----+-------
  2 | info2
  3 | info3
(2 rows)

test=# \c test2
You are now connected to database "test2" as user "postgres".
test2=# select * from t1;
 id | info  
----+-------
  2 | info2
  3 | info3
(2 rows)

test2=# \c test
You are now connected to database "test" as user "postgres".

test=# update t1 set info='xxx' where id=2;
UPDATE 1
test=# select * from t1;
 id | info  
----+-------
  3 | info3
  2 | xxx
(2 rows)

test=# \c test2
You are now connected to database "test2" as user "postgres".
test2=# select * from t1;
 id | info  
----+-------
  3 | info3
  2 | xxx
(2 rows)

参考:

https://stackoverflow.com/questions/59492957/can-i-have-a-logical-replication-between-2-databases-in-the-same-postgres-server
https://www.postgresql.org/docs/14/sql-altersubscription.html


http://www.niftyadmin.cn/n/5341657.html

相关文章

1411111111

第一张 用户管理表(客户) sys_user user_id 用户ID username 用户名 password 密码 phonenumber 电话 open_id 微信openID session_key 微信key user_type 管理员 维修员 客户 dept_id 部门ID 第五 维修人员表(维修员)sys_mainta…

大数据基础设施搭建 - Hbase

文章目录 一、解压压缩包二、配置环境变量三、修改配置文件3.1 修改hbase-env.sh3.2 修改hbase-site.xml3.3 修改regionservers 四、解决HBase和Hadoop的log4j兼容性问题,使用Hadoop的jar包五、HBase远程发送到其他集群六、启动七、停止八、基本操作8.1 进入Hbase客…

GO 的 Web 开发系列(一)—— Java 与 Go 做 Web 程序的区别

小玖是奋斗一线的 Java 开发,准备自己开发一个建站程序,出于某些原因,小玖最终将建站程序的开发语言选择了 Go。 对于 Go 小玖其实之前并没有太多接触过,也只能算是个初学者吧,所以对 Go 的 WEB 开发从初学的角度总结…

MATLAB字符串编辑常用代码

1.字符串赋值 % 字符串赋值 sabcdefg 2.字符串属性和操作 (1)获取字符串长度 sabcdefg;% 字符串赋值 length(a) % 获取字符串长度 (2)连接字符串 % 连接两个字符串,每个字符串最右边的空格被裁切 s1a s2b s3strcat(s1,s2) 3.字符串比较 % strcmp 比较两个字符串是…

数据结构(更新至链表)

数组 数组的代码 public class Myarr<T> {private int capacity0;private int size0;private T[]arr;//如果是空的则强制将容量扩充为10public Myarr(int capacity) {if (this.size 0) {this.capacity 10;this.arr(T[]) new Object[this.capacity];}}//按顺序添加数据…

Maven工程继承和聚合关系

1. Maven工程继承关系 1.1 继承概念 Maven 继承是指在 Maven 的项目中&#xff0c;让一个项目从另一个项目中继承配置信息的机制。继承可以让我们在多个项目中共享同一配置信息&#xff0c;简化项目的管理和维护工作。 1.2 继承作用 在父工程中统一管理项目中的依赖信息。 …

C++ Webserver从零开始:基础知识(五)——信号

Linux信号概述 Linux系统中&#xff0c;信号是一种通信的方式&#xff0c;其通常用作用户&#xff0c;系统或进程给目标进程发送的信息。 信号的作用&#xff1a;通知目标进程某个状态的改变或系统异常。 产生的条件&#xff1a; 对于终端程序&#xff1a;可以是用户输入的…

JS-WebAPIs-本地存储(五)

• 本地存储介绍 以前我们页面写的数据一刷新页面就没有了&#xff0c;是不是&#xff1f;随着互联网的快速发展&#xff0c;基于网页的应用越来越普遍&#xff0c;同时也变的越来越复杂&#xff0c;为了满足各种各样的需求&#xff0c;会经常 性在本地存储大量的数据&#xf…