Skip to main content
pgstream is a command-line tool for streaming PostgreSQL data changes to various targets. This document covers all available commands and their usage.

Installation

Global Flags

These flags are available for all commands:
Description: .env or .yaml config file to use with pgstream if any
Default: -
Description: Log level for the application. One of trace, debug, info, warn, error, fatal, panic
Default: debug
Description: Log output format. One of console (human-readable) or json (structured)
Default: console
Description: Disable ANSI color codes in console log output (ignored when --log-format=json)
Default: false
Description: Show help information
Default: -
Every global flag also has an equivalent PGSTREAM_-prefixed environment variable (for example, PGSTREAM_LOG_FORMAT=json, PGSTREAM_LOG_NO_COLOR=true). Use --log-format=json when shipping logs to cloud aggregators like GCP Cloud Logging or Datadog that parse structured JSON.

Commands

init

Initialises pgstream, creating the replication slot and the relevant tables/functions/triggers under the configured internal pgstream schema. It performs the same operations as the --init flag on the run command.
Description: The init command prepares your PostgreSQL database for streaming by:
  • Creating a logical replication slot with the specified name
  • Creating the internal pgstream schema for tracking changes
  • Setting up necessary functions and triggers for change data capture
  • Configuring the database objects required for logical replication
Prerequisites:
  • PostgreSQL must have wal_level = logical
  • User must have replication privileges (REPLICATION role)
  • max_replication_slots must allow for additional slots
  • User must have privileges to create schemas, functions, and triggers
Flags:
  • --postgres-url - Source postgres URL where pgstream setup will be run
  • --replication-slot - Name of the postgres replication slot to be created by pgstream on the source url
  • --with-injector - Whether to initialize pgstream with the injector database migrations. Required for search targets (OpenSearch/Elasticsearch)
  • --migrations-only - Whether to only run the database migrations without creating the replication slot
  • --slot-only - Whether to only create the replication slot, without running the database migrations
Examples:
Note: The --migrations-only flag runs only the database migrations (creating the pgstream schema, tables, functions, and triggers) without creating the replication slot. This is useful when you want to set up the schema separately or when using different database credentials for migrations versus replication. Note: The --slot-only flag is the complement: it creates only the replication slot, skipping the schema and migrations entirely. This is what a read replica source needs, since a standby is read only and cannot take the migrations, while the schema and event trigger reach it through physical replication from the primary. See Running pgstream from a read replica. The two flags are mutually exclusive.

run

Run starts a continuous data stream from the configured source to the configured target.
Description: The run command is the main operation mode for pgstream. It:
  • Establishes a connection to the source database
  • Connects to the existing replication slot (created by init)
  • Continuously reads WAL events from the replication stream
  • Processes and transforms data according to configuration
  • Streams changes to configured targets (Kafka, PostgreSQL, Elasticsearch, OpenSearch)
  • Runs continuously until interrupted (Ctrl+C) or receives a termination signal
  • Gracefully shuts down on SIGTERM/SIGINT
  • Resumes from the last confirmed WAL position
Prerequisites:
  • Database must be initialized with pgstream init
  • Replication slot must exist and be available
  • Target systems must be accessible and properly configured
  • Source database must have logical replication enabled
Flags:
  • --source - Source type. One of postgres, kafka
  • --source-url - Source URL
  • --target - Target type. One of postgres, opensearch, elasticsearch, kafka
  • --target-url - Target URL
  • --replication-slot - Name of the postgres replication slot for pgstream to connect to
  • --snapshot-tables - List of tables to snapshot if initial snapshot is required, in the format <schema>.<table>. If not specified, the schema public will be assumed. Wildcards are supported
  • --reset - Whether to reset the target before snapshotting (only for postgres target). ⚠️ Destructive: the target objects are dropped before the table data is copied, so a snapshot that fails afterwards leaves the target partially rebuilt with the previous contents gone. See resetting the target
  • --profile - Whether to expose a /debug/pprof endpoint on localhost:6060
  • --init - Whether to initialize pgstream before starting replication
  • --dump-file - File where the pg_dump output will be written if initial snapshot is enabled when using pgdump/restore
  • --data-only - When used with --snapshot-tables, skip schema restore and only snapshot data. Use this when the schema is already present on the target. Defaults to false
  • --with-injector - Whether to enable the injection of pgstream metadata to the WAL events. Required for search targets (OpenSearch/Elasticsearch)
Examples:
Output Files (when --profile is enabled):
  • cpu.prof - CPU profiling data for performance analysis
  • mem.prof - Memory allocation profiling data

snapshot

Snapshot performs a one-time data snapshot of a PostgreSQL database. For continuous replication or combined snapshot+replication, use the run command with --snapshot-tables flag.
Description: The snapshot command creates a point-in-time copy of database tables. It:
  • Connects to the source PostgreSQL database
  • Reads all existing data from specified tables/schemas
  • Transforms and streams the data to configured targets
  • Exits after completing the snapshot operation
Prerequisites:
  • Source PostgreSQL database must be accessible
  • Target system must be accessible and properly configured
  • User must have SELECT privileges on tables to be snapshotted
  • For PostgreSQL targets: user must have write privileges
Flags:
  • --postgres-url - Source postgres database to perform the snapshot from
  • --target - Target type. One of postgres, opensearch, elasticsearch, kafka
  • --target-url - Target URL
  • --tables - List of tables to snapshot, in the format <schema>.<table>. If not specified, the schema public will be assumed. Wildcards are supported
  • --reset - Whether to reset the target before snapshotting (only for postgres target). ⚠️ Destructive: the target objects are dropped before the table data is copied, so a snapshot that fails afterwards leaves the target partially rebuilt with the previous contents gone. See resetting the target
  • --profile - Whether to produce CPU and memory profile files, as well as exposing a /debug/pprof endpoint on localhost:6060
  • --dump-file - File where the pg_dump output will be written
Examples:
Use Cases:
  • Bulk data export for analytics
  • Creating test datasets
  • Backfilling data after system setup
Output Files (when --profile is enabled):
  • cpu.prof - CPU profiling data for performance analysis
  • mem.prof - Memory allocation profiling data

status

Checks the status of pgstream initialisation and provided configuration.
Description: The status command provides information about:
  • Replication slot status
  • Internal pgstream schema and objects status
  • Overall streaming infrastructure health
  • Configuration validation results
Prerequisites:
  • Access to the source PostgreSQL database
  • Replication slot should exist
Flags:
  • --postgres-url - Source postgres URL where pgstream has been initialised
  • --replication-slot - Name of the postgres replication slot created by pgstream on the source url
  • --json - Output the status in JSON format
Examples:
Sample Output:

validate

Validate different parts of the pgstream configuration.
Description: The validate command allows you to validate specific aspects of your pgstream configuration before running it. It currently supports validating transformation rules.

validate rules

Validates transformation rules against the provided Postgres database schema.
Description: The validate rules command checks your transformation rules for:
  • Column existence and type compatibility
  • Table and schema references
  • Rule syntax and structure
  • Compatibility with the source database schema
  • Overall validity before applying them in production
Prerequisites:
  • Access to the source PostgreSQL database
  • Transformation rules defined in configuration or separate rules file
Flags:
  • --postgres-url - Source postgres URL to validate the rules against
  • --rules-file, -f - Path to a YAML file containing the transformation rules to validate
  • --json - Output the validation status in JSON format
Examples:
Sample Output:
Use Cases:
  • Pre-deployment validation of transformation rules
  • Testing rule changes against production schema
  • CI/CD pipeline integration for rule validation
  • Debugging transformation rule issues

destroy

It destroys any pgstream setup, removing the replication slot and all the relevant tables/functions/triggers, along with the internal pgstream schema.
Description: The destroy command cleans up all resources created by pgstream init:
  • Drops the replication slot
  • Removes the internal pgstream schema and all its objects
  • Removes all pgstream-related functions and triggers
  • ⚠️ Warning: This is destructive and will lose replication position
Prerequisites:
  • Access to the source PostgreSQL database
  • User must have privileges to drop schemas, functions, and replication slots
  • pgstream should be initialized (objects should exist to be destroyed)
Flags:
  • --postgres-url - Source postgres URL where pgstream destroy will be run
  • --replication-slot - Name of the postgres replication slot to be deleted by pgstream from the source url
  • --with-injector - Whether to also destroy the injector related database objects
  • --migrations-only - Whether to only revert the database migrations without dropping the replication slot
  • --slot-only - Whether to only drop the replication slot, leaving the pgstream schema and migrations in place
Examples:
Note: The --migrations-only flag reverts only the database migrations (removing the pgstream schema, tables, functions, and triggers) without dropping the pgstream schema (with any tables that it might contain, such as the snapshot recorder), or dropping the replication slot. This is useful for minimal downtime migrations where you want to preserve the replication slot position. Note: The --slot-only flag is the complement: it drops only the replication slot and leaves the pgstream schema and the emit_ddl event trigger in place. Use it to remove a slot that is no longer consumed — an unused logical slot pins WAL indefinitely — without disturbing DDL replication for anything still streaming. The two flags are mutually exclusive. ⚠️ Important Notes:
  • Without --migrations-only:
    • It will drop the replication slot and you will lose the current replication position
    • It will drop the pgstream schema along with any tables within it (such as the snapshot recorder table)
  • With --migrations-only:
    • The replication slot is preserved, allowing you to maintain replication position across migrations
    • The pgstream schema is preserved, allowing non migration tables to remain
  • This will stop any running pgstream instances using these resources
  • All pgstream tracking data will be permanently removed
  • You can recreate resources later with pgstream init

version

Displays version information for pgstream.
Description: Shows the current version of the pgstream binary. Example:

Configuration

pgstream uses YAML or .env configuration files. The configuration can be specified via:
  1. Command-line flag: --config /path/to/config.yaml or -c /path/to/config.env
  2. Environment variable: PGSTREAM_CONFIG=/path/to/config.yaml
For more information about the configuration options, check out the configuration documentation.

Examples

Complete Setup Workflow

Configuration File Workflow

Development with Profiling

Multi-target Streaming

Environment Variable Configuration

Troubleshooting

Common Issues

1. Initialization Failures
2. Replication Slot Conflicts
3. Status Check Issues

Command-specific Troubleshooting

Init Command:
Run Command:
Snapshot Command:

Best Practices

  • Use run with the --init flag to ensure pgstream can properly replicate schema changes
  • snapshot only requires read access on your Postgres source, so it’s a good alternative for non invasive syncs
  • If you need a snapshot and replication, use run with initial snapshot to prevent data loss
  • Use the status command to validate your transformer configuration and your pgstream replication setup
  • Use destroy carefully, since it will remove everything used by pgstream, including the replication slot

Getting Help

For immediate help with any command: