-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·69 lines (58 loc) · 2.5 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·69 lines (58 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env bash
# Primary + streaming replica on PostgreSQL 19beta3, in Docker.
# Primary on localhost:55432, replica on localhost:55433.
set -euo pipefail
IMG=${IMG:-postgres:19beta3}
NET=wf-net
PRIMARY=wf-primary
REPLICA=wf-replica
VOL=wf-replica-data
# The entrypoint runs a temporary unix-socket-only server during initdb, so
# pg_isready inside the container goes true too early. Wait on the TCP port
# instead: it only opens once the real server starts.
wait_tcp() {
local port=$1
for _ in $(seq 1 60); do
PGPASSWORD=postgres psql -h localhost -p "$port" -U postgres -Atqc 'SELECT 1' \
>/dev/null 2>&1 && return 0
sleep 1
done
echo "server on port $port never came up" >&2; exit 1
}
docker rm -f "$PRIMARY" "$REPLICA" >/dev/null 2>&1 || true
docker volume rm "$VOL" >/dev/null 2>&1 || true
docker network create "$NET" >/dev/null 2>&1 || true
echo "== primary"
docker run -d --name "$PRIMARY" --network "$NET" \
-e POSTGRES_PASSWORD=postgres -p 55432:5432 "$IMG" \
-c wal_level=replica -c max_wal_senders=10 -c hot_standby=on \
-c shared_buffers=256MB >/dev/null
# NB: nothing durability-related is passed on the command line. Command-line
# options outrank postgresql.auto.conf, so a `-c synchronous_commit=...` here
# would silently defeat every later ALTER SYSTEM in the experiments.
wait_tcp 55432
docker exec "$PRIMARY" psql -U postgres -q -c \
"CREATE ROLE repl WITH REPLICATION LOGIN PASSWORD 'repl';"
docker exec "$PRIMARY" bash -c \
"echo 'host replication all all scram-sha-256' >> \$PGDATA/pg_hba.conf"
docker exec "$PRIMARY" psql -U postgres -q -c "SELECT pg_reload_conf();" >/dev/null
echo "== base backup"
docker run --rm --network "$NET" -v "$VOL":/pgdata -e PGPASSWORD=repl "$IMG" \
bash -c "pg_basebackup -h $PRIMARY -U repl -D /pgdata -R -X stream -c fast \
&& chown -R 999:999 /pgdata && chmod 700 /pgdata" >/dev/null
echo "== replica"
docker run -d --name "$REPLICA" --network "$NET" \
-v "$VOL":/var/lib/postgresql/19/docker -p 55433:5432 "$IMG" >/dev/null
wait_tcp 55433
echo "== schema"
docker exec "$PRIMARY" psql -U postgres -q -c "
CREATE TABLE messages (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
author text NOT NULL,
body text NOT NULL,
posted timestamptz NOT NULL DEFAULT now()
);"
docker exec "$REPLICA" psql -U postgres -Atc "SELECT pg_is_in_recovery();" | \
grep -qx t && echo "replica in recovery: ok"
docker exec "$PRIMARY" psql -U postgres -Atc \
"SELECT application_name, state, sync_state FROM pg_stat_replication;"