ThingWorx High Availability > Example Deployment of PostgreSQL HA with Pgpool-II > 4. Configure database for streaming replication as node0
4. Configure database for streaming replication as node0
Best practice is to have a dedicated user for replication access.
Step 1
Create user replicator.
psql
CREATE USER replicator REPLICATION LOGIN CONNECTION LIMIT 5
PASSWORD 'trUf6yuz2?_Gub';
\q
Step 2
Modify postgresql.conf in order to support replication.
vim /db/postgres/postgresql.conf
Configure the following parameters:
postgres.conf parameters for node0
row
Parameter
Value
Node
1
listen_addresses
*
* includes all addresses. Modify this value based your security requirement.
2
port
5432
3
max_connections
150
4
archive_mode
on
5
wal_level
hot_standy
6
Fsync
on
7
synchronous_commit
on
8
wal_sync_method
open_sync
9
max_wal_sendors
10
10
synchronous_standby_names
'node1,node2'
11
hot_standby
on
12
hot_standby_feedback
on
13
wal_keep_segments
500
14
max_replication_slots
10
15
archive_command
'cp -f %p /db/node0archive/%f</dev/null'
16
checkpoint_segments
3
Step 3
Run the start_replication script.
The start_replication.sh script is used to initiate replication process on node0.
vim /db/bin/start_replication.sh
with following content:
cp /db/postgres/postgresql.conf /db/bin/postgresql.conf
cp /db/postgres/pg_hba.conf /db/bin/pg_hba.conf
rm -rf /db/postgres
rm -rf /db/thingworx/*
pg_basebackup -h $1 -D /db/postgres -U replicator --xlog-method=stream
-v -P
cp /db/bin/postgresql.conf /db/postgres/postgresql.conf
cp /db/bin/pg_hba.conf /db/postgres/pg_hba.conf
Step 4
Run the retargetMaster.sh script for node0.
Create the script retargetMaster.sh:
vim /db/bin/retargetMaster.sh
with the following content:
#!/bin/bash -x
NEW_MASTER_HOSTNAME=$1
NEW_ARCHIVE_FOLDER=$2
if grep -q "$1" /db/postgres/recovery.conf
then
echo $1 is current target,server does not need a restart, done!
else
#service postgresql stop
/usr/pgsql-10.x/bin/pg_ctl -D /db/postgres stop -m fast

#/db/bin/start_replication.sh $NEW_MASTER_HOSTNAME
# Write out new recovery.conf file
rm /db/postgres/recovery.conf
echo "standby_mode = 'on'" >> /db/postgres/recovery.conf
echo "primary_conninfo = 'port=5432 host=$NEW_MASTER_HOSTNAME user=replicator password=trUf6yuz2?_Gub sslmode=disable sslcompression=1 application_name=node0'" >> /db/postgres/recovery.conf
echo "trigger_file='/db/trigger'" >> /db/postgres/recovery.conf
echo "restore_command = 'cp -f $NEW_ARCHIVE_FOLDER/%f %p>/db/postgres/recovery.conf
echo "archive_cleanup_command = '/usr/bin/pg_archivecleanup $NEW_ARCHIVE_FOLDER %r>/db/postgres/recovery.conf
echo "recovery_target_timeline = 'latest'" >> /db/postgres/recovery.conf
/usr/pgsql-10.x/bin/pg_ctl -D /db/postgres -l /db/postgres.log start
echo target is $1 now
fi
Then, make the scripts executable:
chmod a+x /db/bin/start_replication.sh
chmod a+x /db/bin/reargetMaster.sh
Step 5
Update the pg_hba setting.
vim /db/postgres/pg_hba.conf
and update following lines:
host all all 10.10.0.0/16 trust
host replicator replicator 10.10.0.0/16 trust
The above host settings can vary depending on your security requirement. For example, md5 may be used for authentication instead of trust.
Was this helpful?