CREATE_SUBSCRIPTION(7pg13)
π CREATE SUBSCRIPTION(7), aka the pg13 manual page π
Hii bestie! This is the CREATE SUBSCRIPTION(7) page from the PostgreSQL 13.23 documentation β the pg13 manual β and we are learning how to define a new subscription today. Itβs giving main character energy. π
β¨ NAME β¨
CREATE_SUBSCRIPTION β define a new subscription. Thatβs the whole vibe. π
π SYNOPSIS π
CREATE SUBSCRIPTION subscription_name
CONNECTION 'conninfo'
PUBLICATION publication_name [, ...]
[ WITH ( subscription_parameter [= value] [, ... ] ) ]
π¦ DESCRIPTION π¦
Okay so CREATE SUBSCRIPTION adds a new subscription for the current database. The subscription name has to be distinct from the name of any existing subscription in the database β no duplicate names, bestie, itβs not that kind of party. π
The subscription represents a replication connection to the publisher. Because of that, this command does not only add definitions in the local catalogs but also creates a replication slot on the publisher. So itβs doing the local paperwork and the remote setup. Multitasking icon. β¨
A logical replication worker will be started to replicate data for the new subscription at the commit of the transaction where this command is run. Commit the transaction, start the worker β itβs all connected. π«Ά
Additional information about subscriptions and logical replication as a whole is available at Section 30.2 and Chapter 30. Go read those if you want the full lore. π
πΈ PARAMETERS πΈ
subscription_nameβ the name of the new subscription. Pick something cute but valid. πCONNECTION 'conninfo'β the connection string to the publisher. For details seeSection 33.1.1. This is how your subscription reaches the publisher. πPUBLICATION publication_nameβ names of the publications on the publisher to subscribe to. Yes, you can name more than one, because sharing is caring. πWITH ( subscription_parameter [= value] [, ... ] )β this clause specifies optional parameters for a subscription. The following parameters are supported:copy_data(boolean) β specifies whether the existing data in the publications that are being subscribed to should be copied once the replication starts. The default istrue. So yes, by default it copies the existing data. π¦create_slot(boolean) β specifies whether the command should create the replication slot on the publisher. The default istrue. πenabled(boolean) β specifies whether the subscription should be actively replicating, or whether it should be just setup but not started yet. The default istrue. So by default itβs on and ready to go. π₯slot_name(string) β name of the replication slot to use. The default behavior is to use the name of the subscription for the slot name.
When slot_name is set to NONE, there will be no replication slot associated with the subscription. This can be used if the replication slot will be created later manually. Such subscriptions must also have both enabled and create_slot set to false. No slot, no automatic slot creation, no active replication β itβs a whole coordinated outfit. π
synchronous_commit(enum) β the value of this parameter overrides thesynchronous_commitsetting. The default value isoff.
It is safe to use off for logical replication: If the subscriber loses transactions because of missing synchronization, the data will be sent again from the publisher. So off is lowkey fine here. π
A different setting might be appropriate when doing synchronous logical replication. The logical replication workers report the positions of writes and flushes to the publisher, and when using synchronous replication, the publisher will wait for the actual flush. This means that setting synchronous_commit for the subscriber to off when the subscription is used for synchronous replication might increase the latency for COMMIT on the publisher. In this scenario, it can be advantageous to set synchronous_commit to local or higher. So if youβre doing synchronous replication, maybe donβt leave it on off unless you know what youβre doing. π¨
connect(boolean) β specifies whether theCREATE SUBSCRIPTIONshould connect to the publisher at all. Setting this tofalsewill change default values ofenabled,create_slotandcopy_datatofalse.
It is not allowed to combine connect set to false and enabled, create_slot, or copy_data set to true. That combo is a no-go. π
ββοΈ
Since no connection is made when this option is set to false, the tables are not subscribed, and so after you enable the subscription nothing will be replicated. It is required to run ALTER SUBSCRIPTION ... REFRESH PUBLICATION in order for tables to be subscribed. So you will need that refresh step. Donβt skip it. π
π§ NOTES π§
See Section 30.7 for details on how to configure access control between the subscription and the publication instance. Permissions matter, bestie. π
When creating a replication slot (the default behavior), CREATE SUBSCRIPTION cannot be executed inside a transaction block. Periodt. π«
Creating a subscription that connects to the same database cluster β for example, to replicate between databases in the same cluster or to replicate within the same database β will only succeed if the replication slot is not created as part of the same command. Otherwise, the CREATE SUBSCRIPTION call will hang. π
To make this work, create the replication slot separately β using the function pg_create_logical_replication_slot with the plugin name pgoutput β and create the subscription using the parameter create_slot = false. This is an implementation restriction that might be lifted in a future release. So for now, thatβs the move. π
β EXAMPLES β
Create a subscription to a remote server that replicates tables in the publications mypublication and insert_only and starts replicating immediately on commit:
CREATE SUBSCRIPTION mysub
CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'
PUBLICATION mypublication, insert_only;
Create a subscription to a remote server that replicates tables in the insert_only publication and does not start replicating until enabled at a later time.
CREATE SUBSCRIPTION mysub
CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'
PUBLICATION insert_only
WITH (enabled = false);
π COMPATIBILITY π
CREATE SUBSCRIPTION is a PostgreSQL extension. Itβs PostgreSQLβs own thing, and we respect it. π
π₯ SEE ALSO π₯
ALTER SUBSCRIPTION(ALTER_SUBSCRIPTION(7))DROP SUBSCRIPTION(DROP_SUBSCRIPTION(7))CREATE PUBLICATION(CREATE_PUBLICATION(7))ALTER PUBLICATION(ALTER_PUBLICATION(7))
π Thatβs the whole page, bestie β brought to you by PostgreSQL 13.23, dated 2025, and filed under CREATE SUBSCRIPTION(7). Mwah! π«Ά
π¦ where this page comes from
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms