2 .\" Title: CREATE SUBSCRIPTION
3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "CREATE SUBSCRIPTION" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 CREATE_SUBSCRIPTION \- define a new subscription
35 CREATE SUBSCRIPTION \fIsubscription_name\fR
36 CONNECTION \*(Aq\fIconninfo\fR\*(Aq
37 PUBLICATION \fIpublication_name\fR [, \&.\&.\&.]
38 [ WITH ( \fIsubscription_parameter\fR [= \fIvalue\fR] [, \&.\&.\&. ] ) ]
42 \fBCREATE SUBSCRIPTION\fR
43 adds a new logical\-replication subscription\&. The user that creates a subscription becomes the owner of the subscription\&. The subscription name must be distinct from the name of any existing subscription in the current database\&.
45 A subscription represents a replication connection to the publisher\&. Hence, in addition to adding definitions in the local catalogs, this command normally creates a replication slot on the publisher\&.
47 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, unless the subscription is initially disabled\&.
49 To be able to create a subscription, you must have the privileges of the
50 pg_create_subscription
53 privileges on the current database\&.
55 Additional information about subscriptions and logical replication as a whole is available at
61 \fIsubscription_name\fR
63 The name of the new subscription\&.
66 CONNECTION \*(Aq\fIconninfo\fR\*(Aq
70 connection string defining how to connect to the publisher database\&. For details see
74 PUBLICATION \fIpublication_name\fR [, \&.\&.\&.]
76 Names of the publications on the publisher to subscribe to\&.
79 WITH ( \fIsubscription_parameter\fR [= \fIvalue\fR] [, \&.\&.\&. ] )
81 This clause specifies optional parameters for a subscription\&.
83 The following parameters control what happens during subscription creation:
88 \fBCREATE SUBSCRIPTION\fR
89 command should connect to the publisher at all\&. The default is
90 true\&. Setting this to
92 will force the values of
98 false\&. (You cannot combine setting
109 Since no connection is made when this option is
110 false, no tables are subscribed\&. To initiate replication, you must manually create the replication slot, enable the failover if required, enable the subscription, and refresh the subscription\&. See
115 create_slot (boolean)
117 Specifies whether the command should create the replication slot on the publisher\&. The default is
121 false, you are responsible for creating the publisher\*(Aqs slot in some other way\&. See
128 Specifies whether the subscription should be actively replicating or whether it should just be set up but not started yet\&. The default is
134 Name of the publisher\*(Aqs replication slot to use\&. The default is to use the name of the subscription for the slot name\&.
140 means there will be no replication slot associated with the subscription\&. Such subscriptions must also have both
145 false\&. Use this when you will be creating the replication slot later manually\&. See
156 property value of the named slot may differ from the counterpart
158 parameter specified in the subscription\&. Always ensure the slot property
160 matches the counterpart parameter of the subscription and vice versa\&. Otherwise, the slot on the publisher may behave differently from what these subscription options say: for example, the slot on the publisher could either be synced to the standbys even when the subscription\*(Aqs
162 option is disabled or could be disabled for sync even when the subscription\*(Aqs
167 The following parameters control the subscription\*(Aqs replication behavior after it has been created:
171 Specifies whether the subscription will request the publisher to send the data in binary format (as opposed to text)\&. The default is
172 false\&. Any initial table synchronization copy (see
173 copy_data) also uses the same format\&. Binary format can be faster than the text format, but it is less portable across machine architectures and
175 versions\&. Binary format is very data type specific; for example, it will not allow copying from a
179 column, even though that would work fine in text format\&. Even when this option is enabled, only data types having binary send and receive functions will be transferred in binary\&. Note that the initial synchronization requires all data types to have binary send and receive functions, otherwise the synchronization will fail (see
180 CREATE TYPE (\fBCREATE_TYPE\fR(7))
181 for more about send/receive functions)\&.
183 When doing cross\-version replication, it could be that the publisher has a binary send function for some data type, but the subscriber lacks a binary receive function for that type\&. In such a case, data transfer will fail, and the
185 option cannot be used\&.
187 If the publisher is a
189 version before 16, then any initial table synchronization will use text format even if
195 Specifies whether to copy pre\-existing data in the publications that are being subscribed to when the replication starts\&. The default is
198 If the publications contain
200 clauses, it will affect what data is copied\&. Refer to the
208 can interact with the
215 Specifies whether to enable streaming of in\-progress transactions for this subscription\&. The default value is
216 parallel, meaning incoming changes are directly applied via one of the parallel apply workers, if available\&. If no parallel apply worker is free to handle streaming transactions then the changes are written to temporary files and applied after the transaction is committed\&. Note that if an error happens in a parallel apply worker, the finish LSN of the remote transaction might not be reported in the server log\&.
222 .nr an-no-space-flag 1
229 There is a risk of deadlock when the schemas of the publisher and subscriber differ, although such cases are rare\&. The apply worker is equipped to retry these transactions automatically\&.
233 on, the incoming changes are written to temporary files and then applied only after the transaction is committed on the publisher and received by the subscriber\&.
236 off, all transactions are fully decoded on the publisher and only then sent to the subscriber as a whole\&.
239 synchronous_commit (enum)
241 The value of this parameter overrides the
243 setting within this subscription\*(Aqs apply worker processes\&. The default value is
248 for logical replication: If the subscriber loses transactions because of missing synchronization, the data will be sent again from the publisher\&.
250 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
252 for the subscriber to
254 when the subscription is used for synchronous replication might increase the latency for
256 on the publisher\&. In this scenario, it can be advantageous to set
265 Specifies whether two\-phase commit is enabled for this subscription\&. The default is
268 When two\-phase commit is enabled, prepared transactions are sent to the subscriber at the time of
269 \fBPREPARE TRANSACTION\fR, and are processed as two\-phase transactions on the subscriber too\&. Otherwise, prepared transactions are sent to the subscriber only when committed, and are then processed immediately by the subscriber\&.
271 The implementation of two\-phase commit requires that replication has successfully finished the initial table synchronization phase\&. So even when
273 is enabled for a subscription, the internal two\-phase state remains temporarily
275 until the initialization phase completes\&. See column
279 to know the actual two\-phase state\&.
282 disable_on_error (boolean)
284 Specifies whether the subscription should be automatically disabled if any errors are detected by subscription workers during data replication from the publisher\&. The default is
288 password_required (boolean)
291 true, connections to the publisher made as a result of this subscription must use password authentication and the password must be specified as a part of the connection string\&. This setting is ignored when the subscription is owned by a superuser\&. The default is
292 true\&. Only superusers can set this value to
296 run_as_owner (boolean)
298 If true, all replication actions are performed as the subscription owner\&. If false, replication workers will perform actions on each table as the owner of that table\&. The latter configuration is generally much more secure; for details, see
299 Section\ \&29.11\&. The default is
305 Specifies whether the subscription will request the publisher to only send changes that don\*(Aqt have an origin or send changes regardless of origin\&. Setting
309 means that the subscription will request the publisher to only send changes that don\*(Aqt have an origin\&. Setting
313 means that the publisher sends changes regardless of their origin\&. The default is
320 can interact with the
327 Specifies whether the replication slots associated with the subscription are enabled to be synced to the standbys so that logical replication can be resumed from the new primary after failover\&. The default is
332 When specifying a parameter of type
336 part can be omitted, which is equivalent to specifying
342 for details on how to configure access control between the subscription and the publication instance\&.
344 When creating a replication slot (the default behavior),
345 \fBCREATE SUBSCRIPTION\fR
346 cannot be executed inside a transaction block\&.
348 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
349 \fBCREATE SUBSCRIPTION\fR
350 call will hang\&. To make this work, create the replication slot separately (using the function
351 \fBpg_create_logical_replication_slot\fR
353 pgoutput) and create the subscription using the parameter
354 create_slot = false\&. See
356 for examples\&. This is an implementation restriction that might be lifted in a future release\&.
358 If any table in the publication has a
360 clause, rows for which the
366 will not be published\&. If the subscription has several publications in which the same table has been published with different
368 clauses, a row will be published if any of the expressions (referring to that publish operation) are satisfied\&. In the case of different
370 clauses, if one of the publications has no
372 clause (referring to that publish operation) or the publication is declared as
375 FOR TABLES IN SCHEMA, rows are always published regardless of the definition of the other expressions\&. If the subscriber is a
377 version before 15, then any row filtering is ignored during the initial data synchronization phase\&. For this case, the user might want to consider deleting any initially copied data that would be incompatible with subsequent filtering\&. Because initial data synchronization does not take into account the publication
379 parameter when copying existing table data, some rows may be copied that would not be replicated using DML\&. See
383 Subscriptions having several publications in which the same table has been published with different column lists are not supported\&.
385 We allow non\-existent publications to be specified so that users can add those later\&. This means
387 can have non\-existent publications\&.
389 When using a subscription parameter combination of
392 origin = NONE, the initial sync table data is copied directly from the publisher, meaning that knowledge of the true origin of that data is not possible\&. If the publisher also has subscriptions then the copied table data might have originated from further upstream\&. This scenario is detected and a WARNING is logged to the user, but the warning is only an indication of a potential problem; it is the user\*(Aqs responsibility to make the necessary checks to ensure the copied data origins are really as wanted or not\&.
394 To find which tables might potentially include non\-local origins (due to other subscriptions created on the publisher) try this SQL query:
400 # substitute <pub\-names> below with your publication name(s) to be queried
401 SELECT DISTINCT PT\&.schemaname, PT\&.tablename
402 FROM pg_publication_tables PT
403 JOIN pg_class C ON (C\&.relname = PT\&.tablename)
404 JOIN pg_namespace N ON (N\&.nspname = PT\&.schemaname),
405 pg_subscription_rel PS
406 WHERE C\&.relnamespace = N\&.oid AND
407 (PS\&.srrelid = C\&.oid OR
408 C\&.oid IN (SELECT relid FROM pg_partition_ancestors(PS\&.srrelid) UNION
409 SELECT relid FROM pg_partition_tree(PS\&.srrelid))) AND
410 PT\&.pubname IN (<pub\-names>);
417 Create a subscription to a remote server that replicates tables in the publications
421 and starts replicating immediately on commit:
427 CREATE SUBSCRIPTION mysub
428 CONNECTION \*(Aqhost=192\&.168\&.1\&.50 port=5432 user=foo dbname=foodb\*(Aq
429 PUBLICATION mypublication, insert_only;
435 Create a subscription to a remote server that replicates tables in the
437 publication and does not start replicating until enabled at a later time\&.
443 CREATE SUBSCRIPTION mysub
444 CONNECTION \*(Aqhost=192\&.168\&.1\&.50 port=5432 user=foo dbname=foodb\*(Aq
445 PUBLICATION insert_only
446 WITH (enabled = false);
453 \fBCREATE SUBSCRIPTION\fR
458 ALTER SUBSCRIPTION (\fBALTER_SUBSCRIPTION\fR(7)), DROP SUBSCRIPTION (\fBDROP_SUBSCRIPTION\fR(7)), CREATE PUBLICATION (\fBCREATE_PUBLICATION\fR(7)), ALTER PUBLICATION (\fBALTER_PUBLICATION\fR(7))