Implementation:2ndQuadrant Pglogical Create Subscriber CLI
| 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:
- Parses command-line arguments (getopt_long): subscriber name, provider/subscriber DSNs, data directory, replication sets, config files, apply delay, force text transfer
- Discovers external binaries (pg_basebackup, pg_ctl) via find_other_exec_or_die()
- Runs pg_basebackup to copy the provider's data directory
- Verifies system identifier match between backup and provider
- Creates a replication slot on the provider
- Writes recovery configuration and starts the new instance via pg_ctl
- Promotes the instance to primary
- Installs the pglogical extension via install_extension()
- Creates the subscriber node and subscription via pglogical_subscribe()
- 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