Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:2ndQuadrant Pglogical Create Subscriber CLI

From Leeroopedia
Knowledge Sources
Domains Database, Replication, Deployment
Last Updated 2026-10-13 00:00 GMT

Overview

Standalone CLI tool (pglogical_create_subscriber) that initializes a new pglogical subscriber by cloning the provider via physical base backup, modifying system identifiers, starting the new instance, and creating the subscription.

Description

The pglogical_create_subscriber command-line tool automates the entire subscriber initialization process from a physical base backup. It is a standalone C program (not a SQL function) that runs outside the PostgreSQL server.

The tool's main() function:

  1. Parses command-line arguments (getopt_long): subscriber name, provider/subscriber DSNs, data directory, replication sets, config files, apply delay, force text transfer
  2. Discovers external binaries (pg_basebackup, pg_ctl) via find_other_exec_or_die()
  3. Runs pg_basebackup to copy the provider's data directory
  4. Verifies system identifier match between backup and provider
  5. Creates a replication slot on the provider
  6. Writes recovery configuration and starts the new instance via pg_ctl
  7. Promotes the instance to primary
  8. Installs the pglogical extension via install_extension()
  9. Creates the subscriber node and subscription via pglogical_subscribe()
  10. Verifies subscription status and reports success

Usage

Use this CLI tool when:

  • Setting up a new subscriber from a large provider database (faster than per-table COPY)
  • Creating a subscriber with identical schema and data
  • Automating subscriber deployment in scripts

Code Reference

Source Location

  • Repository: pglogical
  • File: pglogical_create_subscriber.c, pglogical_fe.c
  • Lines: pglogical_create_subscriber.c:L165-600+ (main), L138 (find_other_exec_or_die); pglogical_fe.c:L107 (find_other_exec_version)

Signature

pglogical_create_subscriber [OPTIONS]

/* CLI Options: */
--subscriber-name=NAME, -n NAME    # Subscriber node name (required)
--provider-dsn=DSN                  # Provider connection string (required)
--subscriber-dsn=DSN               # Subscriber connection string (required)
--pgdata=DIR, -D DIR               # Target data directory (required)
--replication-sets=SETS, -r SETS  # Comma-separated replication set names
--databases=DBS                    # Databases to replicate
--postgresql-conf=FILE             # Custom postgresql.conf to copy
--hba-conf=FILE                    # Custom pg_hba.conf to copy
--recovery-conf=FILE               # Custom recovery.conf template
--apply-delay=SECONDS              # Apply delay in seconds
--text-types                       # Force text transfer
--drop-slot-if-exists              # Drop existing replication slot
--stop, -s                         # Stop server after setup
--verbose, -v                      # Verbose output
--debug                            # Debug output

Import

# Run as a shell command:
pglogical_create_subscriber --subscriber-name=sub1 --provider-dsn="..." --pgdata=/var/lib/pg/sub1

I/O Contract

Inputs

Name Type Required Description
subscriber-name string Yes Subscriber node name
provider-dsn string Yes Provider connection string
subscriber-dsn string Yes Subscriber connection string
pgdata path Yes Target data directory
replication-sets string No Comma-separated set names (default: default,insert_only,ddl_sql)
apply-delay int No Apply delay in seconds (default: 0)
text-types flag No Force text transfer (default: false)

Outputs

Name Type Description
data directory directory Physical base backup written to pgdata
PostgreSQL instance running server Started and promoted to primary
pglogical extension installed CREATE EXTENSION pglogical executed
subscription catalog entry Subscription created with sync=false (data from backup)
exit code int 0 on success, nonzero on error

Usage Examples

Basic Subscriber Creation

pglogical_create_subscriber \
    --subscriber-name=subscriber1 \
    --provider-dsn="host=provider.example.com port=5432 dbname=mydb" \
    --subscriber-dsn="host=subscriber.example.com port=5432 dbname=mydb" \
    --pgdata=/var/lib/postgresql/subscriber1 \
    --replication-sets=default,insert_only

With Custom Config Files

pglogical_create_subscriber \
    --subscriber-name=subscriber1 \
    --provider-dsn="host=provider.example.com dbname=mydb" \
    --subscriber-dsn="host=subscriber.example.com dbname=mydb" \
    --pgdata=/var/lib/postgresql/subscriber1 \
    --postgresql-conf=/etc/postgresql/custom.conf \
    --hba-conf=/etc/postgresql/custom_pg_hba.conf \
    --apply-delay=30 \
    --verbose

Cross-Version with Text Transfer

pglogical_create_subscriber \
    --subscriber-name=subscriber1 \
    --provider-dsn="host=pg14-provider.example.com dbname=mydb" \
    --subscriber-dsn="host=pg16-subscriber.example.com dbname=mydb" \
    --pgdata=/var/lib/postgresql/subscriber1 \
    --text-types \
    --verbose

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment