Last active
January 7, 2024 07:11
-
-
Save songpon/3805d511eb7da61d00ff7618825da7f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MASTER_IP= | |
SLAVE_IP= | |
# service postgresql stop | |
su - postgres | |
psql -c "CREATE ROLE replica WITH PASSWORD 'myreplpassword' LOGIN REPLICATION;" | |
#Security for master allow standby access | |
echo "host replication replica $SLAVE_IP/32 md5" >> /etc/postgresql/9.6/main/pg_hba.conf | |
# Postgres Master config | |
cat <<EOT >> /etc/postgresql/9.6/main/postgresql.conf | |
listen_addresses = '*' | |
wal_level = replica | |
max_wal_senders = 5 | |
wal_keep_segments = 32 | |
archive_mode = on | |
archive_command = 'cp %p /var/lib/postgresql/9.6/main/archive/%f' | |
EOT | |
mkdir -p /var/lib/postgresql/9.6/main/archive/ | |
chmod 700 /var/lib/postgresql/9.6/main/archive/ | |
chown -R postgres:postgres /var/lib/postgresql/9.6/main/archive/ | |
# service postgresql start | |
######################### | |
# Standby server config | |
su - postgres | |
echo "$MASTER_IP:*:*:replica:myreplpassword" > ~/.pgpass | |
chmod 600 ~/.pgpass | |
# service postgresql stop | |
# clear old db by rename | |
mv /var/lib/postgresql/9.6/main /var/lib/postgresql/9.6/main.bak | |
#inital download database from master | |
pg_basebackup -h $MASTER_IP -D /var/lib/postgresql/9.6/main -P -U replica --xlog-method=stream | |
echo "hot_standby = on" >> /etc/postgresql/9.6/main/postgresql.conf | |
cat <<EOT >> /var/lib/postgresql/9.6/main/recovery.conf | |
standby_mode = 'on' | |
primary_conninfo = 'host=$MASTER_IP port=5432 user=replica application_name=db2-standby' | |
trigger_file = '/tmp/postgresql.trigger.5432' | |
restore_command = 'cp /var/lib/postgresql/9.6/main/archive/%f %p' | |
archive_cleanup_command = 'pg_archivecleanup /var/lib/postgresql/9.6/main/archive %r' | |
recovery_target_timeline = 'latest' | |
EOT | |
# service postgresql start | |
# check standby server for standby mode | |
# tail -f /var/log/postgresql/postgresql-9.6-main.log | |
# references | |
# https://dba.stackexchange.com/questions/158901/creating-a-hot-standby-with-postgres-9-6/159035 | |
# https://blog.2ndquadrant.com/evolution-of-fault-tolerance-in-postgresql-replication-phase/ | |
# https://www.howtoforge.com/tutorial/how-to-set-up-master-slave-replication-for-postgresql-96-on-ubuntu-1604/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment