table of contents
CREATE SUBSCRIPTION(7) | PostgreSQL 12.22 Documentation | CREATE SUBSCRIPTION(7) |
NAME¶
CREATE_SUBSCRIPTION - define a new subscription
SYNOPSIS¶
CREATE SUBSCRIPTION subscription_name
CONNECTION 'conninfo'
PUBLICATION publication_name [, ...]
[ WITH ( subscription_parameter [= value] [, ... ] ) ]
DESCRIPTION¶
CREATE SUBSCRIPTION adds a new subscription for the current database. The subscription name must be distinct from the name of any existing subscription in the database.
The subscription represents a replication connection to the publisher. As such this command does not only add definitions in the local catalogs but also creates a replication slot on the publisher.
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.
Additional information about subscriptions and logical replication as a whole is available at Section 30.2 and Chapter 30.
PARAMETERS¶
subscription_name
CONNECTION 'conninfo'
PUBLICATION publication_name
WITH ( subscription_parameter [= value] [, ... ] )
copy_data (boolean)
create_slot (boolean)
enabled (boolean)
slot_name (string)
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.
synchronous_commit (enum)
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.
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.
connect (boolean)
It is not allowed to combine connect set to false and enabled, create_slot, or copy_data set to true.
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.
NOTES¶
See Section 30.7 for details on how to configure access control between the subscription and the publication instance.
When creating a replication slot (the default behavior), CREATE SUBSCRIPTION cannot be executed inside a transaction block.
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.
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.
SEE ALSO¶
ALTER SUBSCRIPTION (ALTER_SUBSCRIPTION(7)), DROP SUBSCRIPTION (DROP_SUBSCRIPTION(7)), CREATE PUBLICATION (CREATE_PUBLICATION(7)), ALTER PUBLICATION (ALTER_PUBLICATION(7))
2024 | PostgreSQL 12.22 |