Streams PostgreSQL logical replication events as JSON to RabbitMQ Streams in real-time.
909
A lightweight containerized service that streams PostgreSQL logical replication events to RabbitMQ Streams in real-time.
PgHook captures changes from PostgreSQL using logical replication and forwards them to RabbitMQ Streams for event-driven architecture, data synchronization, and analytics pipelines.
postgresql.conf:wal_level = logical
# If needed, increase the number of WAL senders and replication slots.
# The default is 10 for both.
max_wal_senders = 10
max_replication_slots = 10
Note: PostgreSQL must be restarted after modifying this setting.
CREATE PUBLICATION test_publication FOR TABLE table1, table2;
Use RabbitMQ CLI or management UI to create the target stream (PgHook will not create it automatically).
Note: Super Streams are also supported - see optional environment variables below.
docker run --rm --network=host \
-e PGH_POSTGRES_CONN="Server=localhost;Username=postgres;Password=postgres;Database=test_db;ApplicationName=PgHook;Trust Server Certificate=true" \
-e PGH_PUBLICATION_NAMES="test_publication" \
-e PGH_RMQ_ENDPOINTS="localhost:5552" \
-e PGH_RMQ_USERNAME="guest" \
-e PGH_RMQ_PASSWORD="guest" \
-e PGH_RMQ_STREAM_NAME="test_stream" \
pghook/rabbitmq-streams
| Variable | Description |
|---|---|
PGH_POSTGRES_CONN | PostgreSQL connection string |
PGH_PUBLICATION_NAMES | Comma-separated list of PostgreSQL publications to replicate |
PGH_RMQ_ENDPOINTS | RabbitMQ Stream broker endpoints (format: host:port or host1:port,host2:port) |
PGH_RMQ_USERNAME | RabbitMQ username |
PGH_RMQ_PASSWORD | RabbitMQ password |
PGH_RMQ_STREAM_NAME | Target RabbitMQ Stream name (must already exist) |
| Variable | Default | Description |
|---|---|---|
PGH_USE_PERMANENT_SLOT | false | Use a permanent replication slot (survives service restart) |
PGH_REPLICATION_SLOT | Auto-generated | PostgreSQL replication slot name (required if PGH_USE_PERMANENT_SLOT=true) |
PGH_RMQ_VHOST | / | RabbitMQ virtual host |
PGH_RMQ_IS_SUPER_STREAM | false | Use RabbitMQ super stream for partitioning |
PGH_RMQ_PARTITION_KEY_FIELDS_* | PK fields | Define custom partition keys for super streams (e.g., PGH_RMQ_PARTITION_KEY_FIELDS_1="schema.table|column1,column2") |
By default, replication slots are temporary and lost when PgHook stops. For stable replication positions across restarts, create a permanent replication slot in PostgreSQL:
SELECT * FROM pg_create_logical_replication_slot('my_slot', 'pgoutput');
Then configure PgHook with both flags:
-e PGH_USE_PERMANENT_SLOT=true \
-e PGH_REPLICATION_SLOT=my_slot
Note: PGH_REPLICATION_SLOT is required when PGH_USE_PERMANENT_SLOT=true.
⚠️ Important: Permanent replication slots persist across crashes and prevent removal of required resources. This consumes storage because WAL records and catalog rows required by the slot cannot be removed by VACUUM. In extreme cases, this could cause the database to shut down to prevent transaction ID wraparound.
Always drop slots that are no longer needed:
SELECT * FROM pg_drop_replication_slot('my_slot');
When using super streams, partition keys determine how messages are distributed across partitions. By default, table primary key fields are used. Override this with optional PGH_RMQ_PARTITION_KEY_FIELDS_* variables:
docker run --network=host --rm \
-e PGH_POSTGRES_CONN="..." \
-e PGH_PUBLICATION_NAMES="test_publication" \
-e PGH_RMQ_ENDPOINTS="localhost:5552" \
-e PGH_RMQ_USERNAME="guest" \
-e PGH_RMQ_PASSWORD="guest" \
-e PGH_RMQ_STREAM_NAME="test_stream" \
-e PGH_RMQ_IS_SUPER_STREAM=true \
-e PGH_RMQ_PARTITION_KEY_FIELDS_1="public.table_a|last_name,first_name" \
-e PGH_RMQ_PARTITION_KEY_FIELDS_2="public.table_b|postal_code,address" \
pghook/rabbitmq-streams
Format: schema.table|column1,column2 (comma-separated columns for composite partition keys)
docker run --rm --network=host \
-e PGH_POSTGRES_CONN="Server=localhost;Username=postgres;Password=postgres;Database=test_db;ApplicationName=PgHook;Trust Server Certificate=true" \
-e PGH_PUBLICATION_NAMES="test_publication" \
-e PGH_REPLICATION_SLOT="test_slot" \
-e PGH_USE_PERMANENT_SLOT=true \
-e PGH_RMQ_ENDPOINTS="localhost:5552" \
-e PGH_RMQ_USERNAME="guest" \
-e PGH_RMQ_PASSWORD="guest" \
-e PGH_RMQ_STREAM_NAME="test_stream" \
pghook/rabbitmq-streams
docker run --rm --network=host \
-e PGH_POSTGRES_CONN="Server=localhost;Username=postgres;Password=postgres;Database=test_db;ApplicationName=PgHook;Trust Server Certificate=true" \
-e PGH_PUBLICATION_NAMES="test_publication" \
-e PGH_RMQ_ENDPOINTS="localhost:5552" \
-e PGH_RMQ_USERNAME="guest" \
-e PGH_RMQ_PASSWORD="guest" \
-e PGH_RMQ_STREAM_NAME="test_stream" \
-e PGH_RMQ_IS_SUPER_STREAM=true \
-e PGH_RMQ_PARTITION_KEY_FIELDS_1="public.test_table|last_name" \
pghook/rabbitmq-streams
--network=host for local development; adjust for production networkingwal_level = logical is set and PostgreSQL is restartedContent type
Image
Digest
sha256:f77e79ab7…
Size
43.6 MB
Last updated
4 months ago
docker pull pghook/rabbitmq-streams