CREATE_SUBSCRIPTION(7pg14)

πŸ“¦ version 14.24, in opensuse-leap 16.0

CREATE SUBSCRIPTION(7) from the PostgreSQL 14.24 Documentation has entered the chat, bestie πŸ’– and it’s literally here to define a new subscription in the cutest way possible. Buckle up, because this one is highkey iconic and also extremely specific. πŸŽ€


🌸 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 shiny new subscription for the current database. The subscription name must be distinct from the name of any existing subscription in the database β€” no name collisions, bestie πŸ’….

The subscription represents a replication connection to the publisher. So this command doesn’t just add definitions in the local catalogs β€” it also creates a replication slot on the publisher. She’s doing both, periodt ✨.

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. Yep, at commit β€” that’s the vibe.

Want more lore? Additional information about subscriptions and logical replication as a whole is available at Section 31.2 and Chapter 31. πŸ“š

πŸŽ€ PARAMETERS πŸŽ€

  • subscription_name β€” the name of the new subscription. Cute, simple, must be unique πŸ’–.
  • CONNECTION 'conninfo' β€” the connection string to the publisher. For details see Section 34.1.1.
  • PUBLICATION publication_name β€” names of the publications on the publisher to subscribe to.
  • WITH ( subscription_parameter [= value] [, ... ] ) β€” this clause specifies optional parameters for a subscription. The following parameters are supported:
    • copy_data (boolean) β€” specifies whether the existing data in the publications that are being subscribed to should be copied once the replication starts. The default is true.
    • create_slot (boolean) β€” specifies whether the command should create the replication slot on the publisher. The default is true.
    • enabled (boolean) β€” specifies whether the subscription should be actively replicating, or whether it should be just setup but not started yet. The default is true.
    • slot_name (string) β€” name of the replication slot to use. The default behavior is to use the name of the subscription for the slot name.

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) β€” the value of this parameter overrides the synchronous_commit setting within this subscription’s apply worker processes. The default value is off.

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. No cap.

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. πŸ”₯

  • binary (boolean) β€” specifies whether the subscription will request the publisher to send the data in binary format (as opposed to text). The default is false. Even when this option is enabled, only data types that have binary send and receive functions will be transferred in binary.

When doing cross-version replication, it could happen that the publisher has a binary send function for some data type, but the subscriber lacks a binary receive function for the type. In such a case, data transfer will fail, and the binary option cannot be used. πŸ’€

  • connect (boolean) β€” specifies whether the CREATE SUBSCRIPTION should connect to the publisher at all. Setting this to false will change default values of enabled, create_slot and copy_data to false.

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. πŸ™ˆ

  • streaming (boolean) β€” specifies whether streaming of in-progress transactions should be enabled for this subscription. By default, all transactions are fully decoded on the publisher, and only then sent to the subscriber as a whole. 🌈

⚠️ NOTES ⚠️

  • Access control lore: See Section 31.7 for details on how to configure access control between the subscription and the publication instance.
  • Transaction-block caveat: when creating a replication slot (the default behavior), CREATE SUBSCRIPTION cannot be executed inside a transaction block. Periodt.
  • Same-cluster warning, listen up: 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. She’s built different, but only in PostgreSQL-land πŸ’….

🌟 SEE ALSO 🌟

mwah πŸ’‹ β€” source/version: PostgreSQL 14.24, date: 2026, and yes, she ate. 🫢

πŸ“¦ where this page comes from