Skip to content

Latest commit

 

History

History
99 lines (67 loc) · 1.91 KB

File metadata and controls

99 lines (67 loc) · 1.91 KB

The simplest way to run a PostgreSQL cluster in Docker

Docker Compose

Start

docker-compose --project-name="habr-pg-ha-17" up -d

Stop

docker-compose --project-name="habr-pg-ha-17" down

Explore volumes

List all volumes

docker volume ls

Delete specified volume if need

docker volume rm habr-pg-ha-17_pg_1_data
docker volume rm habr-pg-ha-17_pg_2_data

Run psql

psql -U postgres -d habrdb
psql -U habrpguser -d habrdb

See:

show wal_level;

Should be wal_level = 'replica'

How to inspect where the primary is

select case when pg_is_in_recovery() then 'secondary' else 'primary' end as host_status;

Without log in to psql:

docker exec postgres_1 psql -c "select case when pg_is_in_recovery() then 'secondary' else 'primary' end as host_status;" "dbname=habrdb user=habrpguser password=pgpwd4habr"
docker exec postgres_2 psql -c "select case when pg_is_in_recovery() then 'secondary' else 'primary' end as host_status;" "dbname=habrdb user=habrpguser password=pgpwd4habr"

DB metrics

Open in the browser:

How to manually init failover

Stop container with the current primary

docker stop postgres_1

Ensure replica has been promoted to primary

See container logs and wait for

LOG:  database system was not properly shut down; automatic recovery in progress
…
LOG:  database system is ready to accept connections

Return the first host to the cluster

docker start postgres_1

repmgr docs

https://repmgr.org/docs/repmgr.html

switchover

/opt/bitnami/scripts/postgresql-repmgr/entrypoint.sh repmgr standby switchover -f /opt/bitnami/repmgr/conf/repmgr.conf --siblings-follow --dry-run