ITPub博客

首页 > 数据库 > PostgreSQL > AWS Aurora postgresql12同一个实例之间做逻辑复制步骤

AWS Aurora postgresql12同一个实例之间做逻辑复制步骤

原创 PostgreSQL 作者:chenfeng 时间:2021-10-21 12:28:23 0 删除 编辑

版本:

AWS Aurora PostgreSQL 12.6


实现目标:

从prod库的mdm.test_user表数据逻辑复制至test库下的mdm.test_user表


操作说明:

需要在发布端先建好复制槽,然后订阅端指定使用该复制槽,,注意发布端和订阅端两边都必须创建同名schema


具体操作步骤如下:

#创建test库

create database test;

\c test


#创建同名schema:

test=> create schema mdm;


#创建同名表结构:

test=> CREATE TABLE "mdm"."test_user" (

  "id" int4 NOT NULL,

  "user_num" varchar(20) COLLATE "pg_catalog"."default",

  "resource_flag" int2,

  "syn_record_id" int4,

  "login_number" varchar(20) COLLATE "pg_catalog"."default",

  "employee_number" varchar(12) COLLATE "pg_catalog"."default",

  CONSTRAINT "test_user_pkey" PRIMARY KEY ("id")

)

;


#切换到prod库

\c prod


#发布端建立逻辑复制槽

prod=> select pg_create_logical_replication_slot('pg_logical_test_user_slot','pgoutput');


#发布端创建逻辑发布

prod=>  CREATE PUBLICATION pub_test_user for table mdm.test_user;




#订阅端创建订阅:

#切换到test库:

\c test


test=> CREATE SUBSCRIPTION sub_test_user CONNECTION

   'host=xxxxxxx port=5432 dbname=prod user=postgres password=123456'

   PUBLICATION pub_test_user with (create_slot=false,slot_name='pg_logical_test_user_slot');



##发布端和订阅端数据查看:

发布端:

prod=> select count(*) from mdm.test_user;

+-------+

| count |

+-------+

|  1000 |

+-------+

(1 行记录)


#订阅端:

test=> select count(*) from mdm.test_user;

+-------+

| count |

+-------+

|  1000 |

+-------+

(1 行记录)



#查看发布:

prod=> \dRp

                              发布列表

+-------------------+----------+--------+------+------+------+------+

|       名称        |  拥有者  | 所有表 | 插入 | 更新 | 删除 | 截断 |

+-------------------+----------+--------+------+------+------+------+

| pub_test_user      | postgres | f      | t    | t    | t    | t    |

+-------------------+----------+--------+------+------+------+------+

(1 行记录)



#查看发布的表:

prod=>  table pg_publication_tables;

+-------------------+------------+---------------+

|      pubname      | schemaname |   tablename   |

+-------------------+------------+---------------+

| pub_test_user      | mdm        | test_user      |

+-------------------+------------+---------------+

(1 行记录)


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15498/viewspace-2838522/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
交流MySQL,PolarDB/PolarDB-X,PostgreSQL,MongoDB和Redis技术。

注册时间:2015-12-07

  • 博文量
    784
  • 访问量
    2689187