1CREATE SUBSCRIPTION(7) PostgreSQL 15.4 Documentation CREATE SUBSCRIPTION(7)
2
3
4
6 CREATE_SUBSCRIPTION - define a new subscription
7
9 CREATE SUBSCRIPTION subscription_name
10 CONNECTION 'conninfo'
11 PUBLICATION publication_name [, ...]
12 [ WITH ( subscription_parameter [= value] [, ... ] ) ]
13
15 CREATE SUBSCRIPTION adds a new logical-replication subscription. The
16 subscription name must be distinct from the name of any existing
17 subscription in the current database.
18
19 A subscription represents a replication connection to the publisher.
20 Hence, in addition to adding definitions in the local catalogs, this
21 command normally creates a replication slot on the publisher.
22
23 A logical replication worker will be started to replicate data for the
24 new subscription at the commit of the transaction where this command is
25 run, unless the subscription is initially disabled.
26
27 Additional information about subscriptions and logical replication as a
28 whole is available at Section 31.2 and Chapter 31.
29
31 subscription_name
32 The name of the new subscription.
33
34 CONNECTION 'conninfo'
35 The libpq connection string defining how to connect to the
36 publisher database. For details see Section 34.1.1.
37
38 PUBLICATION publication_name [, ...]
39 Names of the publications on the publisher to subscribe to.
40
41 WITH ( subscription_parameter [= value] [, ... ] )
42 This clause specifies optional parameters for a subscription.
43
44 The following parameters control what happens during subscription
45 creation:
46
47 connect (boolean)
48 Specifies whether the CREATE SUBSCRIPTION command should
49 connect to the publisher at all. The default is true. Setting
50 this to false will force the values of create_slot, enabled and
51 copy_data to false. (You cannot combine setting connect to
52 false with setting create_slot, enabled, or copy_data to true.)
53
54 Since no connection is made when this option is false, no
55 tables are subscribed, and so after you enable the subscription
56 nothing will be replicated. You will need to then run ALTER
57 SUBSCRIPTION ... REFRESH PUBLICATION for tables to be
58 subscribed.
59
60 create_slot (boolean)
61 Specifies whether the command should create the replication
62 slot on the publisher. The default is true. If set to false,
63 you are responsible for creating the publisher's slot in some
64 other way.
65
66 enabled (boolean)
67 Specifies whether the subscription should be actively
68 replicating or whether it should just be set up but not started
69 yet. The default is true.
70
71 slot_name (string)
72 Name of the publisher's replication slot to use. The default is
73 to use the name of the subscription for the slot name.
74
75 Setting slot_name to NONE means there will be no replication
76 slot associated with the subscription. Use this when you will
77 be creating the replication slot later manually. Such
78 subscriptions must also have both enabled and create_slot set
79 to false.
80
81 The following parameters control the subscription's replication
82 behavior after it has been created:
83
84 binary (boolean)
85 Specifies whether the subscription will request the publisher
86 to send the data in binary format (as opposed to text). The
87 default is false. Even when this option is enabled, only data
88 types having binary send and receive functions will be
89 transferred in binary.
90
91 When doing cross-version replication, it could be that the
92 publisher has a binary send function for some data type, but
93 the subscriber lacks a binary receive function for that type.
94 In such a case, data transfer will fail, and the binary option
95 cannot be used.
96
97 copy_data (boolean)
98 Specifies whether to copy pre-existing data in the publications
99 that are being subscribed to when the replication starts. The
100 default is true.
101
102 If the publications contain WHERE clauses, it will affect what
103 data is copied. Refer to the Notes for details.
104
105 streaming (boolean)
106 Specifies whether to enable streaming of in-progress
107 transactions for this subscription. By default, all
108 transactions are fully decoded on the publisher and only then
109 sent to the subscriber as a whole.
110
111 synchronous_commit (enum)
112 The value of this parameter overrides the synchronous_commit
113 setting within this subscription's apply worker processes. The
114 default value is off.
115
116 It is safe to use off for logical replication: If the
117 subscriber loses transactions because of missing
118 synchronization, the data will be sent again from the
119 publisher.
120
121 A different setting might be appropriate when doing synchronous
122 logical replication. The logical replication workers report the
123 positions of writes and flushes to the publisher, and when
124 using synchronous replication, the publisher will wait for the
125 actual flush. This means that setting synchronous_commit for
126 the subscriber to off when the subscription is used for
127 synchronous replication might increase the latency for COMMIT
128 on the publisher. In this scenario, it can be advantageous to
129 set synchronous_commit to local or higher.
130
131 two_phase (boolean)
132 Specifies whether two-phase commit is enabled for this
133 subscription. The default is false.
134
135 When two-phase commit is enabled, prepared transactions are
136 sent to the subscriber at the time of PREPARE TRANSACTION, and
137 are processed as two-phase transactions on the subscriber too.
138 Otherwise, prepared transactions are sent to the subscriber
139 only when committed, and are then processed immediately by the
140 subscriber.
141
142 The implementation of two-phase commit requires that
143 replication has successfully finished the initial table
144 synchronization phase. So even when two_phase is enabled for a
145 subscription, the internal two-phase state remains temporarily
146 “pending” until the initialization phase completes. See column
147 subtwophasestate of pg_subscription to know the actual
148 two-phase state.
149
150 disable_on_error (boolean)
151 Specifies whether the subscription should be automatically
152 disabled if any errors are detected by subscription workers
153 during data replication from the publisher. The default is
154 false.
155
157 See Section 31.9 for details on how to configure access control between
158 the subscription and the publication instance.
159
160 When creating a replication slot (the default behavior), CREATE
161 SUBSCRIPTION cannot be executed inside a transaction block.
162
163 Creating a subscription that connects to the same database cluster (for
164 example, to replicate between databases in the same cluster or to
165 replicate within the same database) will only succeed if the
166 replication slot is not created as part of the same command. Otherwise,
167 the CREATE SUBSCRIPTION call will hang. To make this work, create the
168 replication slot separately (using the function
169 pg_create_logical_replication_slot with the plugin name pgoutput) and
170 create the subscription using the parameter create_slot = false. This
171 is an implementation restriction that might be lifted in a future
172 release.
173
174 If any table in the publication has a WHERE clause, rows for which the
175 expression evaluates to false or null will not be published. If the
176 subscription has several publications in which the same table has been
177 published with different WHERE clauses, a row will be published if any
178 of the expressions (referring to that publish operation) are satisfied.
179 In the case of different WHERE clauses, if one of the publications has
180 no WHERE clause (referring to that publish operation) or the
181 publication is declared as FOR ALL TABLES or FOR TABLES IN SCHEMA, rows
182 are always published regardless of the definition of the other
183 expressions. If the subscriber is a PostgreSQL version before 15, then
184 any row filtering is ignored during the initial data synchronization
185 phase. For this case, the user might want to consider deleting any
186 initially copied data that would be incompatible with subsequent
187 filtering. Because initial data synchronization does not take into
188 account the publication publish parameter when copying existing table
189 data, some rows may be copied that would not be replicated using DML.
190 See Section 31.2.2 for examples.
191
192 Subscriptions having several publications in which the same table has
193 been published with different column lists are not supported.
194
195 We allow non-existent publications to be specified so that users can
196 add those later. This means pg_subscription can have non-existent
197 publications.
198
200 Create a subscription to a remote server that replicates tables in the
201 publications mypublication and insert_only and starts replicating
202 immediately on commit:
203
204 CREATE SUBSCRIPTION mysub
205 CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'
206 PUBLICATION mypublication, insert_only;
207
208 Create a subscription to a remote server that replicates tables in the
209 insert_only publication and does not start replicating until enabled at
210 a later time.
211
212 CREATE SUBSCRIPTION mysub
213 CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'
214 PUBLICATION insert_only
215 WITH (enabled = false);
216
218 CREATE SUBSCRIPTION is a PostgreSQL extension.
219
221 ALTER SUBSCRIPTION (ALTER_SUBSCRIPTION(7)), DROP SUBSCRIPTION
222 (DROP_SUBSCRIPTION(7)), CREATE PUBLICATION (CREATE_PUBLICATION(7)),
223 ALTER PUBLICATION (ALTER_PUBLICATION(7))
224
225
226
227PostgreSQL 15.4 2023 CREATE SUBSCRIPTION(7)