Sub topic 6
Subscribing with `CREATE SUBSCRIPTION`
This chapter assumes you know basic SQL, can run psql, understand PostgreSQL roles and privileges, and have already configured the publisher for logical replication. You should also have a publication on the publisher. A subscription connects another PostgreSQL database to that publication.
What a subscription does
A subscription is the subscriber-side definition of a logical replication connection. It tells PostgreSQL:
- Where the publisher is.
- Which publication to follow.
- Which replication slot to use.
- Whether to copy existing rows.
- Whether to start applying changes immediately.
When you create a subscription, PostgreSQL normally creates a replication slot with the same name on the publisher. The slot records how far the subscriber has consumed the change stream.
The subscriber must have compatible tables before synchronization begins. Logical replication does not copy table definitions, indexes, constraints, or other schema objects.
sequenceDiagram
participant S as Subscriber
participant P as Publisher
participant W as Subscription workers
S->>P: Connect using CONNECTION
S->>P: Create replication slot
S->>P: Read publication table list
S->>P: Copy existing rows
P-->>W: Stream later row changes
W->>S: Apply INSERT, UPDATE, DELETE, TRUNCATECreate a subscription
First, create the table on the subscriber with compatible columns and types:
-- Run on the subscriber
CREATE TABLE orders (
id bigint PRIMARY KEY,
customer_id bigint NOT NULL,
status text NOT NULL,
total numeric(10,2) NOT NULL
);Then create the subscription. Run this command on the subscriber:
CREATE SUBSCRIPTION orders_sub
CONNECTION 'host=publisher.example.com
port=5432
dbname=shop
user=repl_user
password=change_me'
PUBLICATION orders_pub;The connection string identifies the publisher database. The PUBLICATION clause names the publication to consume. After the command succeeds, PostgreSQL starts subscription workers. One worker receives changes, and table synchronization workers may copy existing rows.
The replication user needs permission to connect to the publisher database and read the published tables. The subscriber-side role needs permission to create the subscription and write to the target tables.
| Option | Purpose |
|---|---|
| `copy_data = false` | Skip copying rows that already exist on the publisher |
| `create_slot = false` | Do not create a publisher slot automatically |
| `slot_name = 'name'` | Use a specific existing or new slot |
| `enabled = false` | Create the subscription without starting workers |
For example, use copy_data = false only when the subscriber already contains the required initial data or you plan to load it separately:
CREATE SUBSCRIPTION orders_sub
CONNECTION 'host=publisher.example.com dbname=shop user=repl_user password=change_me'
PUBLICATION orders_pub
WITH (copy_data = false);Check the subscription
Run this query on the subscriber:
SELECT subname,
pid,
received_lsn,
latest_end_lsn,
latest_end_time
FROM pg_stat_subscription;A row with a worker process indicates that PostgreSQL has a running subscription worker. During initial synchronization, additional workers can appear for individual tables.
If the subscription stops, inspect the PostgreSQL server log on the subscriber. Common causes include missing tables, permission failures, incompatible columns, and duplicate-key conflicts. Fix the underlying problem, then the worker can usually continue from the replication slot.
Disable or remove a subscription
To pause replication without removing its metadata:
ALTER SUBSCRIPTION orders_sub DISABLE;To resume it:
ALTER SUBSCRIPTION orders_sub ENABLE;Dropping a subscription normally drops its publisher-side replication slot. If the publisher is unreachable, detach the slot first so cleanup does not remain dependent on that connection.
Exercise
What must exist on the subscriber before CREATE SUBSCRIPTION can copy rows from a published table?
Show answer
A compatible table definition must already exist. Logical replication copies row data and later changes, but it does not copy the table's schema.