talas-group/04_INFRA_DEPLOIEMENT/Notes_Operations/replication_postgresql.txt
senke 66471934af Initial commit: Talas Group project management & documentation
Knowledge base of ~80+ markdown files across 14 domains (00-13),
Logseq graph, hardware design files (KiCAD), infrastructure configs,
and talas-wiki static site.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 20:10:41 +02:00

75 lines
2.1 KiB
Text

both the primary and secondry server need the same postgreSQL version.
in my case :
-primary serv : 10.184.116.91
-secondary serv : 10.184.116.237
# 1 # on the primary server
Dans postgresql.conf :
wal_level = replica (ou logical)
max_wal_senders = X
1 par client par streaming
défaut : 10
wal_sender_timeout = 60s
#create replication user
CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'replicaPassword';
#inside of the pg_hba.conf file add the following line:
host replication replicator 10.184.116.137/24 scram-sha-256
#reload config
psql -c "select pg_reload_conf();"
#in case of issue, exit from postgres user to go back to root and use:
systemctl restart postgresql
#you can then switch back to postgres user
su -- postgres
# 2 # on the replica server
#verify connection between primary and secondary server
telnet 10.184.116.91 5432
#stop postgresql
systemctl stop postgresql
#backup secondary server existing data
sudo su postgres
cp -R /var/lib/postgresql/17/main/ /var/lib/postgresql/17/main_bak/
rm -rf /var/lib/postgresql/17/main/
#initialize the replica with pg_basebackup
pg_basebackup --host=10.184.116.91 --pgdata=/var/lib/postgresql/17/main/ --username=replicator --progress --verbose --write-recovery-conf --wal-method=stream --create-slot --slot=replicaslot1
#confirm replica creation by checking existence of standby.signal file:
ls -ltrh /var/lib/postgresql/17/main/
# 3 # you can then test the replication
#if there was already a database on the primary server, check on the secondary server if it exist as well
\l+
#you should see the same content when you use the same command on the primary server
#try creating a new database
# on the primary server, create and fill a db
create database replica_test;
\c replica_test
create table users (id serial primary key, username text);
insert into users (username) values ('Superman');
insert into users (username) values ('Batman');
insert into users (username) values ('Spiderman');
# on the replica server check existence of newly created db and check it's content
\l
\c replica_test
select * from users;