for this setup, i wanted a streaming replication between two servers. the base os for the vm is ubuntu 24.04.
install postgresql on the both servers
sudo apt install postgresql -y
on the master node:
sudo -u postgres psql
create role repuser with replication password ‘secure-password’ login;
\q
sudo nano /etc/postgresql/(ver)/main/postgresql.conf
listen_addresses = ‘*’
wal_level = replica
max_wal_senders = 5
wal_keep_size = 4096mb
sudo nano /etc/postgresql/(ver)/main/pg_hba.conf
host replication repuser 192.168.10.x/32 scram-sha-256
sudo systemctl restart postgresql
on the slave node:
sudo systemctl stop postgresql
sudo rm -rf /var/lib/postgresql/(ver)/main
sudo -u postgres pg_basebackup -h 192.168.10.x -D /var/lib/postgresql/16/main/ -U repuser-P -v -R -X stream
enter password
sudo systemctl start postgresql
verification commands:
master – sudo -u postgres psql -c “SELECT application_name, client_addr, state, sync_state FROM pg_stat_replication;”
slave – sudo -u postgres psql -c “SELECT pg_is_in_recovery();”
to allow external connections to the master node, the pg_hba.conf needs to be edited.
host database user address method
sudo systemctl restart postgresql
the reason why is that on the db vm’s, there is ufw installed to restrict which servers can communicate on postgresql’s port.