### Set Up Virtual Environment and Install Dependencies Source: https://github.com/gunnarmorling/pgoutput-cli/blob/main/README.md Creates a Python virtual environment and activates it, then installs project dependencies from requirements.txt. ```bash python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt ``` -------------------------------- ### Install Dependencies on macOS Source: https://github.com/gunnarmorling/pgoutput-cli/blob/main/README.md Installs necessary libraries and sets environment variables for building Psycopg from source on macOS. ```bash brew install libpq brew install openssl export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib" export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include" export PATH="/opt/homebrew/opt/libpq/bin:$PATH" ``` -------------------------------- ### pgoutput-cli: Connect and Stream CDC Events Source: https://context7.com/gunnarmorling/pgoutput-cli/llms.txt Example of running pgoutput-cli with connection and replication parameters to stream CDC events as JSON. Includes example output for an INSERT event. ```bash # Basic usage pgoutput-cli \ --host=localhost \ --port=5432 \ --database=mydb \ --user=replication_user \ --password=secret \ --publication=my_publication \ --slot=my_slot # Example output for an INSERT event: # { # "op": "I", # "message_type": "INSERT", # "relation_id": 16384, # "new": { # "id": 42, # "name": "Alice", # "email": "alice@example.com" # } # } # Display help pgoutput-cli --help ``` -------------------------------- ### pgoutput-cli: Missing Host Argument Example Source: https://context7.com/gunnarmorling/pgoutput-cli/llms.txt Demonstrates the error output and exit code when the --host argument is omitted, showing the tool's requirement for all connection parameters. ```bash # If --host or --port is missing, the tool exits with code 1 and prints usage: pgoutput-cli --database=mydb --user=postgres --password=secret \ --publication=my_pub --slot=my_slot # Usage: pgoutput-cli --host= --port= ... # exit code: 1 ``` -------------------------------- ### Build a Custom Docker Image for pgoutput-cli Source: https://context7.com/gunnarmorling/pgoutput-cli/llms.txt Build a custom Docker image from the provided Dockerfile. This image uses python:3.13-slim as the base and installs necessary dependencies for compiling psycopg2. ```bash # Build and tag the image docker build -t gunnarmorling/pgoutput-cli:latest . ``` ```bash # Build with a specific version tag docker build -t gunnarmorling/pgoutput-cli:1.0.0 . ``` ```dockerfile # Dockerfile overview: # FROM python:3.13-slim # RUN apt-get install libpq-dev gcc git && pip install -r requirements.txt # COPY pgoutput-cli.py /usr/bin/pgoutput-cli ``` -------------------------------- ### Run pgoutput-cli as a Docker Container Source: https://context7.com/gunnarmorling/pgoutput-cli/llms.txt Use the pre-built Docker image to run pgoutput-cli without a local Python setup. Connect to a PostgreSQL container running on the same Docker network using the --network flag. ```bash # Connect to a Postgres container on a shared Docker network docker run -it --rm \ --network my_docker_network \ gunnarmorling/pgoutput-cli \ pgoutput-cli \ --host=postgres \ --port=5432 \ --database=mydb \ --user=replication_user \ --password=secret \ --publication=my_publication \ --slot=my_slot ``` ```bash # Stream output can be piped to jq for filtering: docker run -it --rm \ --network my_docker_network \ gunnarmorling/pgoutput-cli \ pgoutput-cli --host=postgres --port=5432 --database=mydb \ --user=replication_user --password=secret \ --publication=my_publication --slot=my_slot \ | jq 'select(.message_type == "INSERT")' ``` -------------------------------- ### pgoutput-cli Usage Help Source: https://context7.com/gunnarmorling/pgoutput-cli/llms.txt Displays the usage instructions for the pgoutput-cli command, outlining required arguments. ```bash Usage: pgoutput-cli --host= --port= --database= --user= --password= --publication= --slot= ``` -------------------------------- ### Build Docker Image Source: https://github.com/gunnarmorling/pgoutput-cli/blob/main/README.md Builds a Docker image for the pgoutput-cli application, tagging it with a specified version. ```bash docker build -t gunnarmorling/pgoutput-cli: . ``` -------------------------------- ### Run pgoutput-cli via Docker Source: https://github.com/gunnarmorling/pgoutput-cli/blob/main/README.md Launches pgoutput-cli as a Docker container, connecting to a specified Docker network and providing connection parameters. ```bash docker run -it --rm --network gunnarmorling/pgoutput-cli \ pgoutput-cli --host= --port= \ --database= --user= --password= \ --publication= --slot= ``` -------------------------------- ### PostgreSQL: Create Publication and Replication Slot Source: https://context7.com/gunnarmorling/pgoutput-cli/llms.txt SQL commands to create a logical replication publication and a replication slot using the 'pgoutput' plugin in PostgreSQL. ```sql -- Create a publication for all tables (or specify tables explicitly) CREATE PUBLICATION my_publication FOR ALL TABLES; -- Create a logical replication slot using the pgoutput plugin SELECT pg_create_logical_replication_slot('my_slot', 'pgoutput'); -- Verify the slot was created SELECT slot_name, plugin, slot_type, active FROM pg_replication_slots; ``` -------------------------------- ### Run pgoutput-cli Source: https://github.com/gunnarmorling/pgoutput-cli/blob/main/README.md Command-line interface for running pgoutput-cli with connection and publication details. ```bash pgoutput-cli --host= --port= \ --database= --user= --password= \ --publication= --slot= ``` -------------------------------- ### Verify Replication Slot Creation Source: https://context7.com/gunnarmorling/pgoutput-cli/llms.txt SQL query to check the details of the created replication slot, confirming its name, plugin, type, and active status. ```sql SELECT slot_name, plugin, slot_type, active FROM pg_replication_slots; ``` -------------------------------- ### requirements.txt Contents Source: https://context7.com/gunnarmorling/pgoutput-cli/llms.txt Lists the Python dependencies for the pgoutput-cli project, including specific versions and git sources. ```plaintext pypgoutput @ git+https://github.com/gunnarmorling/pypgoutput@master pypgoutput @ git+https://github.com/gunnarmorling/pypgoutput@63a814022349e51f27f269d1135458dc0b417898 annotated-types==0.7.0 psycopg2==2.9.10 pydantic==2.11.7 pydantic_core==2.33.2 typing_extensions==4.12.2 ``` -------------------------------- ### Core Python Logic: LogicalReplicationReader Loop Source: https://context7.com/gunnarmorling/pgoutput-cli/llms.txt This Python snippet demonstrates the core logic of pgoutput-cli using pypgoutput.LogicalReplicationReader to stream decoded replication messages as Pydantic model objects, serialized to JSON. Ensure the reader is always stopped to release the replication slot connection. ```python import pypgoutput # Initialize the replication reader cdc_reader = pypgoutput.LogicalReplicationReader( publication_name="my_publication", slot_name="my_slot", host="localhost", database="mydb", port=5432, user="replication_user", password="secret", ) # Stream messages — blocks until the slot is consumed or interrupted for message in cdc_reader: # Each message is a Pydantic model; serialize to indented JSON print(message.model_dump_json(indent=2)) # Example INSERT output: # { # "op": "I", # "message_type": "INSERT", # "relation_id": 16400, # "new": { "id": 1, "username": "bob" } # } # Always stop the reader to release the replication slot connection cdc_reader.stop() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.