2 .\" Title: pg_createsubscriber
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 "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 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 pg_createsubscriber \- convert a physical replica into a new logical replica
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
38 creates a new logical replica from a physical standby server\&. All tables in the specified database are included in the
40 setup\&. A pair of publication and subscription objects are created for each database\&. It must be run at the target server\&.
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
44 is how the data synchronization is done\&.
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\&.
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
56 accepts the following command\-line arguments:
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
66 database will be used, or if that does not exist,
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
70 \fB\-\-publication\fR,
71 \fB\-\-replication\-slot\fR, or
72 \fB\-\-subscription\fR\&.
75 \fB\-d \fR\fB\fIdbname\fR\fR
77 \fB\-\-database=\fR\fB\fIdbname\fR\fR
79 The name of the database in which to create a subscription\&. Multiple databases can be selected by writing multiple
81 switches\&. This option cannot be used together with
84 option is not provided, the database name will be obtained from
86 option\&. If the database name is not specified in either the
92 option is not specified, an error will be reported\&.
95 \fB\-D \fR\fB\fIdirectory\fR\fR
97 \fB\-\-pgdata=\fR\fB\fIdirectory\fR\fR
99 The target directory that contains a cluster directory from a physical replica\&.
106 Do everything except actually modifying the target directory\&.
109 \fB\-p \fR\fB\fIport\fR\fR
111 \fB\-\-subscriber\-port=\fR\fB\fIport\fR\fR
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\&.
116 \fB\-P \fR\fB\fIconnstr\fR\fR
118 \fB\-\-publisher\-server=\fR\fB\fIconnstr\fR\fR
120 The connection string to the publisher\&. For details see
124 \fB\-s \fR\fB\fIdir\fR\fR
126 \fB\-\-socketdir=\fR\fB\fIdir\fR\fR
128 The directory to use for postmaster sockets on target server\&. The default is current directory\&.
131 \fB\-t \fR\fB\fIseconds\fR\fR
133 \fB\-\-recovery\-timeout=\fR\fB\fIseconds\fR\fR
135 The maximum number of seconds to wait for recovery to end\&. Setting to 0 disables\&. The default is 0\&.
140 \fB\-\-enable\-two\-phase\fR
144 commit for the subscription\&. When multiple databases are specified, this option applies uniformly to all subscriptions created on those databases\&. The default is
148 \fB\-U \fR\fB\fIusername\fR\fR
150 \fB\-\-subscriber\-username=\fR\fB\fIusername\fR\fR
152 The user name to connect as on target server\&. Defaults to the current operating system user name\&.
159 Enables verbose mode\&. This will cause
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\&.
164 \fB\-\-clean=\fR\fB\fIobjtype\fR\fR
166 Drop all objects of the specified type from specified databases on the target server\&.
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\&.
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
186 \fB\-\-config\-file=\fR\fB\fIfilename\fR\fR
188 Use the specified main server configuration file for the target data directory\&.
192 command to start and stop the target server\&. It allows you to specify the actual
194 configuration file if it is stored outside the data directory\&.
197 \fB\-\-publication=\fR\fB\fIname\fR\fR
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
205 \fB\-\-replication\-slot=\fR\fB\fIname\fR\fR
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
213 \fB\-\-subscription=\fR\fB\fIname\fR\fR
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
236 command line arguments, and exit\&.
241 There are some prerequisites for
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
247 \fBpg_replication_origin_advance()\fR\&.
249 The target server must be used as a physical standby\&. The target server must have
250 max_active_replication_origins
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
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
261 The source server must accept connections from the target server\&. The source server must not be in recovery\&. The source server must have
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
268 configured to a value greater than or equal to the number of specified databases and existing WAL sender processes\&.
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\&.
276 usually starts the target server with different connection settings during transformation\&. Hence, connections to the target server should fail\&.
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\&.
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\&.
285 If the replication is using
286 primary_slot_name, it will be removed from the source server after the logical replication setup\&.
288 If the target server is a synchronous replica, transaction commits on the primary might wait for replication while running
289 pg_createsubscriber\&.
292 \fB\-\-enable\-two\-phase\fR
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
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\&.
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
308 to ensure that required WAL files are not prematurely removed\&.
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:
321 Start the target server with the specified command\-line options\&. If the target server is already running,
323 will terminate with an error\&.
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,
336 will terminate with an error\&.
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
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
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
362 parameter and by the subscriptions as a replication start point\&. It guarantees that no transaction will be lost\&.
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
375 as the action that the server should take once the recovery target is reached\&. Additional
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
381 terminates if recovery does not end until the given number of seconds\&.
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
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\&.
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\&.
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\&.
433 Enable the subscription for each specified database on the target server\&. The subscription starts applying transactions from the replication start point\&.
444 If the standby server was using
445 primary_slot_name, it has no use from now on so drop it\&.
456 If the standby server contains
457 failover replication slots, they cannot be synchronized anymore, so drop them\&.
468 Update the system identifier on the target server\&. The
470 is run to modify the system identifier\&. The target server is stopped as a
476 To create a logical replica for databases
480 from a physical replica at
487 $ \fBpg_createsubscriber \-D /usr/local/pgsql/data \-P "host=foo" \-d hr \-d finance\fR
494 \fBpg_basebackup\fR(1)