]> begriffs open source - ai-pg/blob - full-docs/man1/pg_createsubscriber.1
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man1 / pg_createsubscriber.1
1 '\" t
2 .\"     Title: pg_createsubscriber
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "PG_CREATESUBSCRIBER" "1" "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 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 pg_createsubscriber \- convert a physical replica into a new logical replica
32 .SH "SYNOPSIS"
33 .HP \w'\fBpg_createsubscriber\fR\ 'u
34 \fBpg_createsubscriber\fR [\fIoption\fR...] {\fB\-d\fR | \fB\-\-database\fR}\fIdbname\fR {\fB\-D\fR | \fB\-\-pgdata\fR}\fIdatadir\fR {\fB\-P\fR | \fB\-\-publisher\-server\fR}\fIconnstr\fR
35 .SH "DESCRIPTION"
36 .PP
37 pg_createsubscriber
38 creates a new logical replica from a physical standby server\&. All tables in the specified database are included in the
39 logical replication
40 setup\&. A pair of publication and subscription objects are created for each database\&. It must be run at the target server\&.
41 .PP
42 After a successful run, the state of the target server is analogous to a fresh logical replication setup\&. The main difference between the logical replication setup and
43 pg_createsubscriber
44 is how the data synchronization is done\&.
45 pg_createsubscriber
46 does not copy the initial table data\&. It does only the synchronization phase, which ensures each table is brought up to a synchronized state\&.
47 .PP
48 pg_createsubscriber
49 targets large database systems because in logical replication setup, most of the time is spent doing the initial data copy\&. Furthermore, a side effect of this long time spent synchronizing data is usually a large amount of changes to be applied (that were produced during the initial data copy), which increases even more the time when the logical replica will be available\&. For smaller databases, it is recommended to set up logical replication with initial data synchronization\&. For details, see the
50 \fBCREATE SUBSCRIPTION\fR
51 copy_data
52 option\&.
53 .SH "OPTIONS"
54 .PP
55 pg_createsubscriber
56 accepts the following command\-line arguments:
57 .PP
58 \fB\-a\fR
59 .br
60 \fB\-\-all\fR
61 .RS 4
62 Create one subscription per database on the target server\&. Exceptions are template databases and databases that don\*(Aqt allow connections\&. To discover the list of all databases, connect to the source server using the database name specified in the
63 \fB\-\-publisher\-server\fR
64 connection string, or if not specified, the
65 postgres
66 database will be used, or if that does not exist,
67 template1
68 will be used\&. Automatically generated names for subscriptions, publications, and replication slots are used when this option is specified\&. This option cannot be used along with
69 \fB\-\-database\fR,
70 \fB\-\-publication\fR,
71 \fB\-\-replication\-slot\fR, or
72 \fB\-\-subscription\fR\&.
73 .RE
74 .PP
75 \fB\-d \fR\fB\fIdbname\fR\fR
76 .br
77 \fB\-\-database=\fR\fB\fIdbname\fR\fR
78 .RS 4
79 The name of the database in which to create a subscription\&. Multiple databases can be selected by writing multiple
80 \fB\-d\fR
81 switches\&. This option cannot be used together with
82 \fB\-a\fR\&. If
83 \fB\-d\fR
84 option is not provided, the database name will be obtained from
85 \fB\-P\fR
86 option\&. If the database name is not specified in either the
87 \fB\-d\fR
88 option, or the
89 \fB\-P\fR
90 option, and
91 \fB\-a\fR
92 option is not specified, an error will be reported\&.
93 .RE
94 .PP
95 \fB\-D \fR\fB\fIdirectory\fR\fR
96 .br
97 \fB\-\-pgdata=\fR\fB\fIdirectory\fR\fR
98 .RS 4
99 The target directory that contains a cluster directory from a physical replica\&.
100 .RE
101 .PP
102 \fB\-n\fR
103 .br
104 \fB\-\-dry\-run\fR
105 .RS 4
106 Do everything except actually modifying the target directory\&.
107 .RE
108 .PP
109 \fB\-p \fR\fB\fIport\fR\fR
110 .br
111 \fB\-\-subscriber\-port=\fR\fB\fIport\fR\fR
112 .RS 4
113 The port number on which the target server is listening for connections\&. Defaults to running the target server on port 50432 to avoid unintended client connections\&.
114 .RE
115 .PP
116 \fB\-P \fR\fB\fIconnstr\fR\fR
117 .br
118 \fB\-\-publisher\-server=\fR\fB\fIconnstr\fR\fR
119 .RS 4
120 The connection string to the publisher\&. For details see
121 Section\ \&32.1.1\&.
122 .RE
123 .PP
124 \fB\-s \fR\fB\fIdir\fR\fR
125 .br
126 \fB\-\-socketdir=\fR\fB\fIdir\fR\fR
127 .RS 4
128 The directory to use for postmaster sockets on target server\&. The default is current directory\&.
129 .RE
130 .PP
131 \fB\-t \fR\fB\fIseconds\fR\fR
132 .br
133 \fB\-\-recovery\-timeout=\fR\fB\fIseconds\fR\fR
134 .RS 4
135 The maximum number of seconds to wait for recovery to end\&. Setting to 0 disables\&. The default is 0\&.
136 .RE
137 .PP
138 \fB\-T\fR
139 .br
140 \fB\-\-enable\-two\-phase\fR
141 .RS 4
142 Enables
143 two_phase
144 commit for the subscription\&. When multiple databases are specified, this option applies uniformly to all subscriptions created on those databases\&. The default is
145 false\&.
146 .RE
147 .PP
148 \fB\-U \fR\fB\fIusername\fR\fR
149 .br
150 \fB\-\-subscriber\-username=\fR\fB\fIusername\fR\fR
151 .RS 4
152 The user name to connect as on target server\&. Defaults to the current operating system user name\&.
153 .RE
154 .PP
155 \fB\-v\fR
156 .br
157 \fB\-\-verbose\fR
158 .RS 4
159 Enables verbose mode\&. This will cause
160 pg_createsubscriber
161 to output progress messages and detailed information about each step to standard error\&. Repeating the option causes additional debug\-level messages to appear on standard error\&.
162 .RE
163 .PP
164 \fB\-\-clean=\fR\fB\fIobjtype\fR\fR
165 .RS 4
166 Drop all objects of the specified type from specified databases on the target server\&.
167 .sp
168 .RS 4
169 .ie n \{\
170 \h'-04'\(bu\h'+03'\c
171 .\}
172 .el \{\
173 .sp -1
174 .IP \(bu 2.3
175 .\}
176 publications: The
177 FOR ALL TABLES
178 publications established for this subscriber are always dropped; specifying this object type causes all other publications replicated from the source server to be dropped as well\&.
179 .RE
180 .sp
181 The objects selected to be dropped are individually logged, including during a
182 \fB\-\-dry\-run\fR\&. There is no opportunity to affect or stop the dropping of the selected objects, so consider taking a backup of them using
183 pg_dump\&.
184 .RE
185 .PP
186 \fB\-\-config\-file=\fR\fB\fIfilename\fR\fR
187 .RS 4
188 Use the specified main server configuration file for the target data directory\&.
189 pg_createsubscriber
190 internally uses the
191 pg_ctl
192 command to start and stop the target server\&. It allows you to specify the actual
193 postgresql\&.conf
194 configuration file if it is stored outside the data directory\&.
195 .RE
196 .PP
197 \fB\-\-publication=\fR\fB\fIname\fR\fR
198 .RS 4
199 The publication name to set up the logical replication\&. Multiple publications can be specified by writing multiple
200 \fB\-\-publication\fR
201 switches\&. The number of publication names must match the number of specified databases, otherwise an error is reported\&. The order of the multiple publication name switches must match the order of database switches\&. If this option is not specified, a generated name is assigned to the publication name\&. This option cannot be used together with
202 \fB\-\-all\fR\&.
203 .RE
204 .PP
205 \fB\-\-replication\-slot=\fR\fB\fIname\fR\fR
206 .RS 4
207 The replication slot name to set up the logical replication\&. Multiple replication slots can be specified by writing multiple
208 \fB\-\-replication\-slot\fR
209 switches\&. The number of replication slot names must match the number of specified databases, otherwise an error is reported\&. The order of the multiple replication slot name switches must match the order of database switches\&. If this option is not specified, the subscription name is assigned to the replication slot name\&. This option cannot be used together with
210 \fB\-\-all\fR\&.
211 .RE
212 .PP
213 \fB\-\-subscription=\fR\fB\fIname\fR\fR
214 .RS 4
215 The subscription name to set up the logical replication\&. Multiple subscriptions can be specified by writing multiple
216 \fB\-\-subscription\fR
217 switches\&. The number of subscription names must match the number of specified databases, otherwise an error is reported\&. The order of the multiple subscription name switches must match the order of database switches\&. If this option is not specified, a generated name is assigned to the subscription name\&. This option cannot be used together with
218 \fB\-\-all\fR\&.
219 .RE
220 .PP
221 \fB\-V\fR
222 .br
223 \fB\-\-version\fR
224 .RS 4
225 Print the
226 pg_createsubscriber
227 version and exit\&.
228 .RE
229 .PP
230 \fB\-?\fR
231 .br
232 \fB\-\-help\fR
233 .RS 4
234 Show help about
235 pg_createsubscriber
236 command line arguments, and exit\&.
237 .RE
238 .SH "NOTES"
239 .SS "Prerequisites"
240 .PP
241 There are some prerequisites for
242 pg_createsubscriber
243 to convert the target server into a logical replica\&. If these are not met, an error will be reported\&. The source and target servers must have the same major version as the
244 pg_createsubscriber\&. The given target data directory must have the same system identifier as the source data directory\&. The given database user for the target data directory must have privileges for creating
245 subscriptions
246 and using
247 \fBpg_replication_origin_advance()\fR\&.
248 .PP
249 The target server must be used as a physical standby\&. The target server must have
250 max_active_replication_origins
251 and
252 max_logical_replication_workers
253 configured to a value greater than or equal to the number of specified databases\&. The target server must have
254 max_worker_processes
255 configured to a value greater than the number of specified databases\&. The target server must accept local connections\&. If you are planning to use the
256 \fB\-\-enable\-two\-phase\fR
257 switch then you will also need to set the
258 max_prepared_transactions
259 appropriately\&.
260 .PP
261 The source server must accept connections from the target server\&. The source server must not be in recovery\&. The source server must have
262 wal_level
263 as
264 logical\&. The source server must have
265 max_replication_slots
266 configured to a value greater than or equal to the number of specified databases plus existing replication slots\&. The source server must have
267 max_wal_senders
268 configured to a value greater than or equal to the number of specified databases and existing WAL sender processes\&.
269 .SS "Warnings"
270 .PP
271 If
272 pg_createsubscriber
273 fails after the target server was promoted, then the data directory is likely not in a state that can be recovered\&. In such case, creating a new standby server is recommended\&.
274 .PP
275 pg_createsubscriber
276 usually starts the target server with different connection settings during transformation\&. Hence, connections to the target server should fail\&.
277 .PP
278 Since DDL commands are not replicated by logical replication, avoid executing DDL commands that change the database schema while running
279 pg_createsubscriber\&. If the target server has already been converted to logical replica, the DDL commands might not be replicated, which might cause an error\&.
280 .PP
281 If
282 pg_createsubscriber
283 fails while processing, objects (publications, replication slots) created on the source server are removed\&. The removal might fail if the target server cannot connect to the source server\&. In such a case, a warning message will inform the objects left\&. If the target server is running, it will be stopped\&.
284 .PP
285 If the replication is using
286 primary_slot_name, it will be removed from the source server after the logical replication setup\&.
287 .PP
288 If the target server is a synchronous replica, transaction commits on the primary might wait for replication while running
289 pg_createsubscriber\&.
290 .PP
291 Unless the
292 \fB\-\-enable\-two\-phase\fR
293 switch is specified,
294 pg_createsubscriber
295 sets up logical replication with two\-phase commit disabled\&. This means that any prepared transactions will be replicated at the time of
296 \fBCOMMIT PREPARED\fR, without advance preparation\&. Once setup is complete, you can manually drop and re\-create the subscription(s) with the
297 two_phase
298 option enabled\&.
299 .PP
300 pg_createsubscriber
301 changes the system identifier using
302 pg_resetwal\&. It would avoid situations in which the target server might use WAL files from the source server\&. If the target server has a standby, replication will break and a fresh standby should be created\&.
303 .PP
304 Replication failures can occur if required WAL files are missing\&. To prevent this, the source server must set
305 max_slot_wal_keep_size
306 to
307 \-1
308 to ensure that required WAL files are not prematurely removed\&.
309 .SS "How It Works"
310 .PP
311 The basic idea is to have a replication start point from the source server and set up a logical replication to start from this point:
312 .sp
313 .RS 4
314 .ie n \{\
315 \h'-04' 1.\h'+01'\c
316 .\}
317 .el \{\
318 .sp -1
319 .IP "  1." 4.2
320 .\}
321 Start the target server with the specified command\-line options\&. If the target server is already running,
322 pg_createsubscriber
323 will terminate with an error\&.
324 .RE
325 .sp
326 .RS 4
327 .ie n \{\
328 \h'-04' 2.\h'+01'\c
329 .\}
330 .el \{\
331 .sp -1
332 .IP "  2." 4.2
333 .\}
334 Check if the target server can be converted\&. There are also a few checks on the source server\&. If any of the prerequisites are not met,
335 pg_createsubscriber
336 will terminate with an error\&.
337 .RE
338 .sp
339 .RS 4
340 .ie n \{\
341 \h'-04' 3.\h'+01'\c
342 .\}
343 .el \{\
344 .sp -1
345 .IP "  3." 4.2
346 .\}
347 Create a publication and replication slot for each specified database on the source server\&. Each publication is created using
348 FOR ALL TABLES\&. If the
349 \fB\-\-publication\fR
350 option is not specified, the publication has the following name pattern:
351 \(lqpg_createsubscriber_%u_%x\(rq
352 (parameter: database
353 \fIoid\fR, random
354 \fIint\fR)\&. If the
355 \fB\-\-replication\-slot\fR
356 option is not specified, the replication slot has the following name pattern:
357 \(lqpg_createsubscriber_%u_%x\(rq
358 (parameters: database
359 \fIoid\fR, random
360 \fIint\fR)\&. These replication slots will be used by the subscriptions in a future step\&. The last replication slot LSN is used as a stopping point in the
361 recovery_target_lsn
362 parameter and by the subscriptions as a replication start point\&. It guarantees that no transaction will be lost\&.
363 .RE
364 .sp
365 .RS 4
366 .ie n \{\
367 \h'-04' 4.\h'+01'\c
368 .\}
369 .el \{\
370 .sp -1
371 .IP "  4." 4.2
372 .\}
373 Write recovery parameters into the target data directory and restart the target server\&. It specifies an LSN (recovery_target_lsn) of the write\-ahead log location up to which recovery will proceed\&. It also specifies
374 promote
375 as the action that the server should take once the recovery target is reached\&. Additional
376 recovery parameters
377 are added to avoid unexpected behavior during the recovery process such as end of the recovery as soon as a consistent state is reached (WAL should be applied until the replication start location) and multiple recovery targets that can cause a failure\&. This step finishes once the server ends standby mode and is accepting read\-write transactions\&. If
378 \fB\-\-recovery\-timeout\fR
379 option is set,
380 pg_createsubscriber
381 terminates if recovery does not end until the given number of seconds\&.
382 .RE
383 .sp
384 .RS 4
385 .ie n \{\
386 \h'-04' 5.\h'+01'\c
387 .\}
388 .el \{\
389 .sp -1
390 .IP "  5." 4.2
391 .\}
392 Create a subscription for each specified database on the target server\&. If the
393 \fB\-\-subscription\fR
394 option is not specified, the subscription has the following name pattern:
395 \(lqpg_createsubscriber_%u_%x\(rq
396 (parameters: database
397 \fIoid\fR, random
398 \fIint\fR)\&. It does not copy existing data from the source server\&. It does not create a replication slot\&. Instead, it uses the replication slot that was created in a previous step\&. The subscription is created but it is not enabled yet\&. The reason is the replication progress must be set to the replication start point before starting the replication\&.
399 .RE
400 .sp
401 .RS 4
402 .ie n \{\
403 \h'-04' 6.\h'+01'\c
404 .\}
405 .el \{\
406 .sp -1
407 .IP "  6." 4.2
408 .\}
409 Drop publications on the target server that were replicated because they were created before the replication start location\&. It has no use on the subscriber\&.
410 .RE
411 .sp
412 .RS 4
413 .ie n \{\
414 \h'-04' 7.\h'+01'\c
415 .\}
416 .el \{\
417 .sp -1
418 .IP "  7." 4.2
419 .\}
420 Set the replication progress to the replication start point for each subscription\&. When the target server starts the recovery process, it catches up to the replication start point\&. This is the exact LSN to be used as a initial replication location for each subscription\&. The replication origin name is obtained since the subscription was created\&. The replication origin name and the replication start point are used in
421 \fBpg_replication_origin_advance()\fR
422 to set up the initial replication location\&.
423 .RE
424 .sp
425 .RS 4
426 .ie n \{\
427 \h'-04' 8.\h'+01'\c
428 .\}
429 .el \{\
430 .sp -1
431 .IP "  8." 4.2
432 .\}
433 Enable the subscription for each specified database on the target server\&. The subscription starts applying transactions from the replication start point\&.
434 .RE
435 .sp
436 .RS 4
437 .ie n \{\
438 \h'-04' 9.\h'+01'\c
439 .\}
440 .el \{\
441 .sp -1
442 .IP "  9." 4.2
443 .\}
444 If the standby server was using
445 primary_slot_name, it has no use from now on so drop it\&.
446 .RE
447 .sp
448 .RS 4
449 .ie n \{\
450 \h'-04' 10.\h'+01'\c
451 .\}
452 .el \{\
453 .sp -1
454 .IP "  10." 4.2
455 .\}
456 If the standby server contains
457 failover replication slots, they cannot be synchronized anymore, so drop them\&.
458 .RE
459 .sp
460 .RS 4
461 .ie n \{\
462 \h'-04' 11.\h'+01'\c
463 .\}
464 .el \{\
465 .sp -1
466 .IP "  11." 4.2
467 .\}
468 Update the system identifier on the target server\&. The
469 \fBpg_resetwal\fR(1)
470 is run to modify the system identifier\&. The target server is stopped as a
471 \fBpg_resetwal\fR
472 requirement\&.
473 .RE
474 .SH "EXAMPLES"
475 .PP
476 To create a logical replica for databases
477 hr
478 and
479 finance
480 from a physical replica at
481 foo:
482 .sp
483 .if n \{\
484 .RS 4
485 .\}
486 .nf
487 $ \fBpg_createsubscriber \-D /usr/local/pgsql/data \-P "host=foo" \-d hr \-d finance\fR
488 .fi
489 .if n \{\
490 .RE
491 .\}
492 .sp
493 .SH "SEE ALSO"
494 \fBpg_basebackup\fR(1)