There are two postgresql table in two different region, and I would like to sync just one table inside both database. Is it possible?
Asked
Active
Viewed 479 times
0
-
Perhaps so? https://www.educba.com/postgresql-logical-replication/ – Jim Jones Mar 18 '21 at 10:18
-
Use publication and subscription logical replication approach. – VynlJunkie Mar 18 '21 at 12:09
1 Answers
0
I figured out his with pgloical.
(Docker PostgreSQL) Bi-directional Replication with pglogical
- Launch two PostgreSQL container with different port
docker volume create sync_test_1
docker run --name sync_test_1 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=qwerty1234 -p 5433:5432 -d -v sync_test_1:/var/lib/postgresql/data postgres:9.5.23
docker volume create sync_test_2
docker run --name sync_test_2 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=qwerty1234 -p 5434:5432 -d -v sync_test_2:/var/lib/postgresql/data postgres:9.5.23
- Get into PostgreSQL container shell and update and install essential tools
docker exec -it sync_test_1 /bin/bash
apt -y update
apt install -y vim
apt install -y procps
- do step 1 to 3 with sync_test_2 too
- Install pglogical package
apt install -y postgresql-9.5-pglogical
- Change postgresql.conf
vi /var/lib/postgresql/data/postgresql.conf
wal_level = logical
track_commit_timestamp = on
enter code hereshared_preload_libraries = ‘pglogical’
max_replication_slots = 10
- Restart PostgreSQL
docker restart sync_test_1
- Use pglogical
create database localharvest
psql -U postgres localharvest
CREATE EXTENSION pglogical;
SELECT pglogical.create_node(node_name := 'db1_local', dsn := 'host=127.0.0.1 port=5432 dbname=localharvest user=postgres password=qwerty1234');
SELECT * FROM pglogical.replication_set;
SELECT pglogical.replication_set_add_table(‘default’, ‘users’);
ALTER SEQUENCE users_id_seq RESTART WITH 1 INCREMENT 20;
add ‘host replication postgres 172.17.0.1/32 trust’ in /var/lib/postgresql/data/pg_hba.conf
- subscribe each other
SELECT pglogical.create_subscription(subscription_name := 'db2_subscription', forward_origins := '{}', provider_dsn := 'host=host.docker.internal port=5434 dbname=localharvest user=postgres password=qwerty1234');
ref: https://eradman.com/posts/active-active-pglogical.html From inside of a Docker container, how do I connect to the localhost of the machine? https://github.com/2ndQuadrant/pglogical https://www.2ndquadrant.com/en/resources/pglogical/pglogical-docs/#Replication-sets

Young Hwang
- 3
- 2