Skip to content

Commit 5717cc5

Browse files
committed
add sample cdc
1 parent b28902c commit 5717cc5

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

postgres-cdc-example/Dockerfile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM postgres:12.16
2+
3+
RUN apt-get update
4+
RUN apt-get install -y postgresql-12-wal2json
5+
RUN apt-get install -y redis-tools
6+
7+
COPY startup.sh .
8+
9+
ENV CDC_DB_NAME=CDC_DB_NAME
10+
11+
ENV REDIS_HOST=REDIS_HOST
12+
ENV REDIS_PORT=6379
13+
ENV REDIS_CHANNEL=REDIS_CHANNEL
14+
15+
USER postgres
16+
CMD ./startup.sh

postgres-cdc-example/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
This is a working CDC example for a local Postgres 12 / Redis.
2+
It relies on wal2json for change notifications, see https://github.com/eulerto/wal2json.
3+
It uses redis-cli to publish the notifications.
4+
5+
Demo usage:
6+
- the CDC'ed database must pre-exist, it should first be created as follows
7+
- run `cd cdc`
8+
- run `docker run -p 5432:5432 -v ./postgres-data:/var/lib/postgresql/data -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres postgres:12.16`
9+
- using a postgresql client, create the 'test_db' database and a table
10+
- Ctrl-C the docker container started above
11+
- now run `docker compose up -d`
12+
- checking configuration:
13+
14+
- from docker desktop, open a terminal to redis container
15+
- from that terminal, run `redis-cli SUBSCRIBE test_db_changes`
16+
- using a postgresql client, perform some inserts, updates or deletes in the 'test_db' database
17+
18+
Real usage:
19+
- you need to configure the env variables of the postgres service in the docker-compose.yml file
20+
- the database name should be set to the messages repository name configured for the mediator
21+
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: '3'
2+
3+
services:
4+
postgres:
5+
build: .
6+
image: pgsql-cdc/latest
7+
container_name: cdc-pgsql
8+
platform: linux/amd64
9+
ports:
10+
- 5432:5432
11+
volumes:
12+
- ./postgres-data:/var/lib/postgresql/data
13+
environment:
14+
- POSTGRES_USER=postgres
15+
- POSTGRES_PASSWORD=postgres
16+
- CDC_DB_NAME=test_db
17+
- REDIS_HOST=host.docker.internal
18+
- REDIS_PORT=6379
19+
- REDIS_CHANNEL=test_db_changes
20+
21+
redis:
22+
image: redis:7.2.3
23+
container_name: cdc-redis
24+
platform: linux/amd64
25+
ports:
26+
- 6379:6379
27+
28+

postgres-cdc-example/startup.sh

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
# run postgres in the background so we can run pg_recvlogical
4+
postgres -c wal_level=logical -c max_wal_senders=1 -c shared_preload_libraries=wal2json &
5+
# ensure postgres is fully initialized
6+
sleep 10s
7+
# create replication slot
8+
pg_recvlogical -d test_db --slot messages_slot --create-slot --if-not-exists -P wal2json
9+
# need a function for publishing otherwise bash shouts
10+
publish_to_redis() {
11+
redis-cli -h $REDIS_HOST -p $REDIS_PORT PUBLISH $REDIS_CHANNEL $1
12+
}
13+
# listen to replication messages and publish them to redis
14+
pg_recvlogical -d test_db --slot messages_slot --start -o pretty-print=0 -f - | while read message; do publish_to_redis $message ; done
15+
# uncomment the following if you comment the previous to keep the container running
16+
# tail -f /dev/null

0 commit comments

Comments
 (0)