Created
June 7, 2022 14:04
-
-
Save lukasz-kaniowski/c11b5ada311187356656af88f4e6b180 to your computer and use it in GitHub Desktop.
Setup replication slot for postgres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3.1' | |
services: | |
db: | |
image: postgres:13-alpine | |
restart: always | |
environment: | |
- POSTGRES_USER=postgres | |
- POSTGRES_PASSWORD=password | |
- POSTGRES_DB=test | |
command: | |
- postgres | |
- -c | |
- max_wal_senders=20 | |
- -c | |
- wal_sender_timeout=0 | |
- -c | |
- max_replication_slots=10 | |
- -c | |
- wal_level=logical | |
ports: | |
- "5432:5432" | |
volumes: | |
- ./postgres_init.sql:/docker-entrypoint-initdb.d/postgres_init.sql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE logs | |
( | |
id bigserial primary key, | |
log text, | |
created_at timestamp default now() not null | |
); | |
CREATE USER replication_user PASSWORD 'pass123'; | |
GRANT USAGE ON SCHEMA "public" TO replication_user; | |
GRANT SELECT ON ALL TABLES IN SCHEMA "public" TO replication_user; | |
ALTER DEFAULT PRIVILEGES IN SCHEMA "public" GRANT SELECT ON TABLES TO replication_user; | |
ALTER USER replication_user WITH replication; | |
CREATE PUBLICATION logs_pub FOR TABLE logs; | |
SELECT pg_create_logical_replication_slot('pgoutput_slot', 'pgoutput'); | |
-- tests | |
SELECT * FROM pg_publication_tables; | |
SELECT count(*) FROM pg_logical_slot_peek_binary_changes('pgoutput_slot', null, null, 'proto_version', '1', 'publication_names', 'logs_pub'); | |
SELECT * FROM pg_logical_slot_get_binary_changes('pgoutput_slot', null, null, 'proto_version', '1', 'publication_names', 'logs_pub'); | |
-- insert into logs(log) values ('test'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment