RELEASE=$(lsb_release -cs)
echo "deb http://apt.postgresql.org/pub/repos/apt/ ${RELEASE}"-pgdg main | sudo tee /etc/apt/sources.list.d/pgdg.list
apt-get install dirmngr --install-recommends
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 7FCC7D46ACCC4CF8
apt update
apt -y install postgresql-11
now install the repmgr
apt install repmgr
ensure passwordless ssh between postgres users of primary and secondary nodes: In primary:
su - postgres
ssh-keygen -t rsa
select default in each step then:
ssh-copy-id root@seconadry
In secondary:
su - postgres
ssh-keygen -t rsa
ssh-copy-id root@primary
Now ensure service won’t start automatically: Run it in both of them:
systemctl disable postgresql
Primary server:
root@debian:~# su - postgres
postgres@debian:~$ createuser -s repmgr
create repmgr.conf and add these line in it:
root@debian:~#nano /etc/repmgr.conf
node_id=1
node_name=pg1
conninfo='host={primaryip} port=5432 user=repmgr dbname=repmgr connect_timeout=2'
data_directory='/var/lib/postgresql/11/main'
pg_bindir='/usr/lib/postgresql/11/bin'
failover=automatic
promote_command='repmgr standby promote -f /etc/repmgr.conf --log-to-file --cluster 10/main'
follow_command='repmgr standby follow -f /etc/repmgr.conf --log-to-file --upstream-node-id=%n --cluster 10/main’
Edit postgres.conf like this:
nano /etc/postgres/11/main/postgres.conf
listen_addresses = '*'
shared_preload_libraries = 'repmgr'
wal_keep_segments = 64
Edit pg_hbs.conf:
local replication repmgr trust
host replication repmgr 127.0.0.1/32 trust
host replication repmgr primaryIP trust
host replication repmgr secondaryIP trust
local repmgr repmgr trust
host repmgr repmgr 127.0.0.1/32 trust
host replication repmgr primaryIP trust
host replication repmgr secondaryIP trust
Finally:
su - postgres
postgres@debian:~$ createdb repmgr -O repmgr
postgres@debian:~$ repmgr -f /etc/repmgr.conf primary register
INFO: connecting to primary database...
NOTICE: attempting to install extension "repmgr"
NOTICE: "repmgr" extension successfully installed
NOTICE: primary node record (ID: 1) registered
Secondary server:
nano /etc/repmgr.conf
node_id=2
node_name=pg2
conninfo='host=10.104.26.72 port=5432 user=repmgr dbname=repmgr connect_timeout=2'
data_directory='/var/lib/postgresql/11/main'
pg_bindir='/usr/lib/postgresql/11/bin'
failover=automatic
promote_command='repmgr standby promote -f /etc/repmgr.conf --log-to-file --cluster 11/main'
follow_command='repmgr standby follow -f /etc/repmgr.conf --log-to-file --upstream-node-id=%n --cluster 11/main’
then:
su - postgres
repmgr -h IPprimary -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --dry-run ------> This is a test
repmgr -h IPprimary -U repmgr -d repmgr -f /etc/repmgr.conf standby clone
NOTICE: destination directory "/var/lib/postgresql/11/main" provided
ERROR: specified data directory "/var/lib/postgresql/11/main" appears to contain a running PostgreSQL instance
HINT: ensure the target data directory does not contain a running PostgreSQL instance
repmgr standby register
INFO: connecting to local node "pg2" (ID: 2)
INFO: connecting to primary database
WARNING: --upstream-node-id not supplied, assuming upstream node is primary (node ID 1)
INFO: standby registration complete
NOTICE: standby node "pg2" (ID: 2) successfully registered
PSQL should be start in this step
root@debian:~# ps aux | grep -i pos
postgres 15944 0.0 0.1 288708 25812 ? S 22:13 0:00 /usr/lib/postgresql/11/bin/postgres -D /var/lib/postgresql/11/main -c config_file=/etc/postgresql/11/main/postgresql.conf
postgres 15945 0.0 0.0 288840 5612 ? Ss 22:13 0:00 postgres: 11/main: startup recovering 000000010000000000000003
postgres 15947 0.0 0.0 288708 3904 ? Ss 22:13 0:00 postgres: 11/main: checkpointer
postgres 15948 0.0 0.0 288708 3904 ? Ss 22:13 0:00 postgres: 11/main: background writer
postgres 15949 0.0 0.0 143624 3148 ? Ss 22:13 0:00 postgres: 11/main: stats collector
postgres 15950 0.0 0.0 299576 11668 ? Ss 22:13 0:00 postgres: 11/main: walreceiver streaming 0/3000228
root 15989 0.0 0.0 12784 904 pts/0 S+ 22:14 0:00 grep -i pos