Skip to content

Commit 0f86445

Browse files
feat: add second postgres instance for local testing (#4281)
For testing Postgres to Postgres mirrors, it would be good to have a second Postgres instance rather than have source and destination be same, for testing things like roles being copied over
1 parent be2e1ba commit 0f86445

5 files changed

Lines changed: 103 additions & 6 deletions

File tree

.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ PG_USER=postgres
2929
PG_PASSWORD=postgres
3030
PG_DATABASE=postgres
3131

32+
PG2_HOST=host.docker.internal
33+
PG2_PORT=5437
34+
PG2_USER=postgres
35+
PG2_PASSWORD=postgres
36+
PG2_DATABASE=postgres
37+
3238
PEERDB_CATALOG_HOST=host.docker.internal
3339
PEERDB_CATALOG_PORT=9901
3440
PEERDB_CATALOG_USER=postgres

.github/workflows/flow.yml

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ jobs:
5454
--health-interval 10s
5555
--health-timeout 5s
5656
--health-retries 5
57+
catalog2:
58+
image: imresamu/postgis:${{ matrix.db-version.pg }}-3.5-alpine
59+
ports:
60+
- 5437:5432
61+
env:
62+
PGUSER: postgres
63+
POSTGRES_PASSWORD: postgres
64+
POSTGRES_DB: postgres
65+
POSTGRES_INITDB_ARGS: --locale=C.UTF-8
66+
options: >-
67+
--health-cmd pg_isready
68+
--health-interval 10s
69+
--health-timeout 5s
70+
--health-retries 5
5771
redpanda:
5872
image: redpandadata/redpanda@sha256:12ddbd8e5572210a48d39afecac44db61503ce1cbdae50ee9b1ff5998a8d91e1
5973
ports:
@@ -111,11 +125,23 @@ jobs:
111125
go-version: '1.26.2'
112126
cache-dependency-path: flow/go.sum
113127

114-
- name: install lib-geos
128+
- name: install lib-geos and pg_dump
115129
run: |
116130
# No need to update man pages on package install
117131
sudo apt-get remove --purge man-db
118-
sudo apt-get install libgeos-dev
132+
# Add PGDG apt repo for latest PostgreSQL client packages
133+
sudo install -d /usr/share/postgresql-common/pgdg
134+
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc \
135+
--fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
136+
echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] \
137+
https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" \
138+
| sudo tee /etc/apt/sources.list.d/pgdg.list
139+
sudo apt-get update
140+
sudo apt-get install -y libgeos-dev
141+
# pg_dump must be >= the server major version; install v18 so it
142+
# can dump PG 16, 17, and 18 (backward compatible).
143+
sudo apt-get install -y postgresql-client-18
144+
echo /usr/lib/postgresql/18/bin >> $GITHUB_PATH
119145
120146
- name: install retry tool
121147
run: |
@@ -275,6 +301,17 @@ jobs:
275301
env:
276302
PGPASSWORD: postgres
277303

304+
- name: prepare secondary postgres for cross-cluster schema-dump tests
305+
run: >
306+
docker exec "${{ job.services.catalog2.id }}" psql -U postgres
307+
-c "ALTER SYSTEM SET wal_level=logical;"
308+
-c "ALTER SYSTEM SET max_replication_slots=192;"
309+
-c "ALTER SYSTEM SET max_wal_senders=256;"
310+
-c "ALTER SYSTEM SET max_connections=2048;" &&
311+
docker restart "${{ job.services.catalog2.id }}"
312+
env:
313+
PGPASSWORD: postgres
314+
278315
- name: set ClickHouse version
279316
id: ch-version
280317
run: |
@@ -561,6 +598,11 @@ jobs:
561598
PG_USER: postgres
562599
PG_PASSWORD: postgres
563600
PG_DATABASE: postgres
601+
PG2_HOST: localhost
602+
PG2_PORT: 5437
603+
PG2_USER: postgres
604+
PG2_PASSWORD: postgres
605+
PG2_DATABASE: postgres
564606
PEERDB_SWITCHBOARD_ENABLED: "true"
565607
PEERDB_QUEUE_FORCE_TOPIC_CREATION: "true"
566608
ELASTICSEARCH_TEST_ADDRESS: http://localhost:9200

Tiltfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ local_resource(
113113
resource_deps=['postgres']
114114
)
115115

116+
local_resource(
117+
'provision-postgres2',
118+
cmd='./local_provision_scripts/postgres.sh peerdb-postgres2',
119+
labels=['Ancillary-DB-Provisioning'],
120+
resource_deps=['postgres2']
121+
)
122+
116123
# This is not defined as a resource as we need the file to be present
117124
# when `docker_compose` loads the configuration (next line).
118125
local('./generate-test-environment.sh')
@@ -150,6 +157,10 @@ dc_resource('postgres', labels=['Ancillary-DB'], links=[
150157
link('http://localhost:5432', 'PostgreSQL'),
151158
], auto_init=False)
152159

160+
dc_resource('postgres2', labels=['Ancillary-DB'], links=[
161+
link('http://localhost:5437', 'PostgreSQL (secondary)'),
162+
], auto_init=False)
163+
153164
local_resource(
154165
'all-test-resources',
155166
cmd=' '.join([

ancillary-docker-compose.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,27 @@ services:
125125
timeout: 10s
126126
retries: 5
127127

128+
postgres2:
129+
container_name: peerdb-postgres2
130+
image: ${POSTGRES_IMAGE}
131+
restart: unless-stopped
132+
volumes:
133+
- postgres2_data:/var/lib/postgresql/data
134+
ports:
135+
- "${PG2_PORT}:5432"
136+
environment:
137+
PGUSER: ${PG2_USER}
138+
POSTGRES_PASSWORD: ${PG2_PASSWORD}
139+
POSTGRES_DB: ${PG2_DATABASE}
140+
POSTGRES_INITDB_ARGS: --locale=C.UTF-8
141+
extra_hosts:
142+
- "host.docker.internal:host-gateway"
143+
healthcheck:
144+
test: ["CMD", "pg_isready"]
145+
interval: 2s
146+
timeout: 10s
147+
retries: 5
148+
128149
dozzle:
129150
container_name: dozzle-compose-monitor
130151
image: amir20/dozzle:latest
@@ -185,6 +206,7 @@ services:
185206

186207
volumes:
187208
postgres_data:
209+
postgres2_data:
188210
clickhouse_data:
189211
mongo_data:
190212
mysql_gtid_data:

local_provision_scripts/postgres.sh

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,23 @@ SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
66
. "$SCRIPT_DIR/../.env"
77

88
DOCKER="docker"
9-
CONTAINER="peerdb-postgres"
9+
CONTAINER="${1:-peerdb-postgres}"
10+
11+
# select per-instance vars based on container name
12+
case "$CONTAINER" in
13+
peerdb-postgres2)
14+
PG_INSTANCE_USER="$PG2_USER"
15+
PG_INSTANCE_DATABASE="$PG2_DATABASE"
16+
PG_INSTANCE_HOST="$PG2_HOST"
17+
PG_INSTANCE_PORT="$PG2_PORT"
18+
;;
19+
*)
20+
PG_INSTANCE_USER="$PG_USER"
21+
PG_INSTANCE_DATABASE="$PG_DATABASE"
22+
PG_INSTANCE_HOST="$PG_HOST"
23+
PG_INSTANCE_PORT="$PG_PORT"
24+
;;
25+
esac
1026

1127
echo "install pgvector extension"
1228
if ! $DOCKER exec "$CONTAINER" test -d /tmp/pgvector; then
@@ -16,7 +32,7 @@ if ! $DOCKER exec "$CONTAINER" test -d /tmp/pgvector; then
1632
fi
1733

1834
echo "create extensions and configure replication"
19-
$DOCKER exec "$CONTAINER" psql -U "$PG_USER" -d "$PG_DATABASE" \
35+
$DOCKER exec "$CONTAINER" psql -U "$PG_INSTANCE_USER" -d "$PG_INSTANCE_DATABASE" \
2036
-c "CREATE EXTENSION IF NOT EXISTS hstore;" \
2137
-c "CREATE EXTENSION IF NOT EXISTS vector;" \
2238
-c "ALTER SYSTEM SET wal_level=logical;" \
@@ -25,9 +41,9 @@ $DOCKER exec "$CONTAINER" psql -U "$PG_USER" -d "$PG_DATABASE" \
2541
-c "ALTER SYSTEM SET max_connections=2048;"
2642

2743
echo "restart postgres to apply config changes"
28-
CURRENT_WAL=$($DOCKER exec "$CONTAINER" psql -U "$PG_USER" -d "$PG_DATABASE" -tAc "SHOW wal_level;")
44+
CURRENT_WAL=$($DOCKER exec "$CONTAINER" psql -U "$PG_INSTANCE_USER" -d "$PG_INSTANCE_DATABASE" -tAc "SHOW wal_level;")
2945
if [ "$CURRENT_WAL" != "logical" ]; then
3046
$DOCKER restart "$CONTAINER"
3147
fi
3248

33-
echo "PostgreSQL is ready at ${PG_HOST}:${PG_PORT}"
49+
echo "PostgreSQL is ready at ${PG_INSTANCE_HOST}:${PG_INSTANCE_PORT}"

0 commit comments

Comments
 (0)