CREATE_SUBSCRIPTION(7pg15)

๐Ÿ“ฆ version 15.19, in opensuse-leap 16.0

โœจ CREATE_SUBSCRIPTION ๐Ÿ’… (a.k.a. the "let's get this replication party started" command)

Okay bestie, so this is the PostgreSQL 15.19 Documentation page for CREATE_SUBSCRIPTION(7) and it's literally all about how to tell your database to follow another database's every move. Like, consensually. It's giving main character energy for logical replication, no cap. ๐ŸŽ€


๐ŸŒธ NAME ๐ŸŒธ

CREATE_SUBSCRIPTION โ€” define a new subscription

(Yes, that's the whole name section, it's short and iconic like that. ๐Ÿ’…)


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

CREATE SUBSCRIPTION subscription_name
    CONNECTION 'conninfo'
    PUBLICATION publication_name [, ...]
    [ WITH ( subscription_parameter [= value] [, ... ] ) ]

๐Ÿ’– DESCRIPTION ๐Ÿ’–

So CREATE SUBSCRIPTION adds a brand new logical-replication subscription, okay? Think of it like you're setting up a dedicated group chat between your database and another one. ๐Ÿ’ฌ

The subscription name must be distinct from the name of any existing subscription in the current database. No duplicate names, bestie, that's not the vibe.

A subscription represents a replication connection to the publisher (the database you're subscribing to, iykyk). So like, in addition to adding definitions in the local catalogs, this command normally creates a replication slot on the publisher. Slots are a whole thing, we love them.

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 โ€” unless the subscription is initially disabled. So like, it just goes. Immediately. ๐Ÿƒโ€โ™€๏ธ๐Ÿ’จ

Want more lore about subscriptions and logical replication in general? The original is like "see Section 31.2 and Chapter 31" and honestly, fair, go read those. ๐Ÿ“š


๐Ÿ’… PARAMETERS ๐Ÿ’…

Here's where it gets juicy. Every single one of these matters, so pay attention. ๐Ÿ‘‘

  • subscription_name โ€” the name of the new subscription. Pick something cute, but remember it has to be unique in this database. No copy-paste names, that's cringe.
  • CONNECTION 'conninfo' โ€” the libpq connection string that defines how to connect to the publisher database. This is literally the address of your new bestie. For all the deets, see Section 34.1.1. ๐Ÿ”—
  • PUBLICATION publication_name [, ...] โ€” the names of the publications on the publisher that you want to subscribe to. You can list a bunch, just comma-separate them. ๐Ÿ’ซ
  • WITH ( subscription_parameter [= value] [, ... ] ) โ€” this clause is where you drop all the optional parameters for your subscription. It's like the settings menu but make it fashion.

The following parameters control what happens during subscription creation:

  • connect (boolean) โ€” specifies whether the CREATE SUBSCRIPTION command should connect to the publisher at all. Default is true. Setting this to false will force the values of create_slot, enabled, and copy_data to false. (And no, you cannot combine setting connect to false with setting create_slot, enabled, or copy_data to true โ€” that's just not allowed. ๐Ÿšซ)

Since no connection is made when this option is false, no tables are subscribed, and so after you enable the subscription nothing will be replicated. You will need to then run ALTER SUBSCRIPTION ... REFRESH PUBLICATION for tables to be subscribed. So like, you gotta do that extra step. ๐Ÿ™ˆ

  • create_slot (boolean) โ€” specifies whether the command should create the replication slot on the publisher. Default is true. If set to false, you are responsible for creating the publisher's slot in some other way. You're on your own, bestie! ๐Ÿซก
  • enabled (boolean) โ€” specifies whether the subscription should be actively replicating or whether it should just be set up but not started yet. Default is true. So like, do you want it to GO or do you want it to just sit there looking pretty? ๐Ÿ’โ€โ™€๏ธ
  • slot_name (string) โ€” name of the publisher's replication slot to use. The default is to use the name of the subscription for the slot name. Nice and simple, we love that.

Setting slot_name to NONE means there will be no replication slot associated with the subscription. Use this when you will be creating the replication slot later manually. Such subscriptions must also have both enabled and create_slot set to false. It's a whole package deal. ๐ŸŽ

The following parameters control the subscription's replication behavior after it has been created:

  • binary (boolean) โ€” specifies whether the subscription will request the publisher to send the data in binary format (as opposed to text). Default is false. Even when this option is enabled, only data types having binary send and receive functions will be transferred in binary. So like, it's not everything, just the cool data types. ๐Ÿงฌ

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

  • copy_data (boolean) โ€” specifies whether to copy pre-existing data in the publications that are being subscribed to when the replication starts. Default is true. So like, do you want the whole history or just the new stuff? ๐Ÿ“œ

If the publications contain WHERE clauses, it will affect what data is copied. Refer to the Notes for details. (Spoiler: it's down there. โฌ‡๏ธ)

  • streaming (boolean) โ€” specifies whether to enable streaming of in-progress transactions for this subscription. By default, all transactions are fully decoded on the publisher and only then sent to the subscriber as a whole. So like, streaming is the fast mode but the default is the safe mode. You decide. ๐ŸŒŠ
  • 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. So like, no stress, it'll just retry. ๐Ÿ’โ€โ™€๏ธ

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. So like, trade-offs, bestie. โš–๏ธ

  • two_phase (boolean) โ€” specifies whether two-phase commit is enabled for this subscription. Default is false. ๐Ÿšฆ

When two-phase commit is enabled, prepared transactions are sent to the subscriber at the time of PREPARE TRANSACTION, and are processed as two-phase transactions on the subscriber too. Otherwise, prepared transactions are sent to the subscriber only when committed, and are then processed immediately by the subscriber. So like, it's a whole different flow. ๐Ÿ”„

The implementation of two-phase commit requires that replication has successfully finished the initial table synchronization phase. So even when two_phase is enabled for a subscription, the internal two-phase state remains temporarily โ€œpendingโ€ until the initialization phase completes. See column subtwophasestate of pg_subscription to know the actual two-phase state. It's giving "we're not ready yet, hold on." โณ

  • disable_on_error (boolean) โ€” specifies whether the subscription should be automatically disabled if any errors are detected by subscription workers during data replication from the publisher. Default is false. So like, do you want it to stop the party if someone spills a drink, or just keep going? ๐Ÿน

๐ŸŽ€ NOTES ๐ŸŽ€

See Section 31.9 for details on how to configure access control between the subscription and the publication instance. Security matters, bestie. ๐Ÿ”

When creating a replication slot (the default behavior), CREATE SUBSCRIPTION cannot be executed inside a transaction block. So like, don't try to put it inside a BEGIN/COMMIT, it's not gonna work. ๐Ÿ™…โ€โ™€๏ธ

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. Like, forever. Well, not forever, but you'll be waiting. ๐Ÿ’€ 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. So like, it is what it is for now. ๐Ÿคทโ€โ™€๏ธ

If any table in the publication has a WHERE clause, rows for which the expression evaluates to false or null will not be published. If the subscription has several publications in which the same table has been published with different WHERE clauses, a row will be published if any of the expressions (referring to that publish operation) are satisfied. In the case of different WHERE clauses, if one of the publications has no WHERE clause (referring to that publish operation) or the publication is declared as FOR ALL TABLES or FOR TABLES IN SCHEMA, rows are always published regardless of the definition of the other expressions. So like, it's a whole logic puzzle. ๐Ÿงฉ

If the subscriber is a PostgreSQL version before 15, then any row filtering is ignored during the initial data synchronization phase. For this case, the user might want to consider deleting any initially copied data that would be incompatible with subsequent filtering. Because initial data synchronization does not take into account the publication publish parameter when copying existing table data, some rows may be copied that would not be replicated using DML. See Section 31.2.2 for examples. So like, you might get some extra stuff you didn't want. Clean it up. ๐Ÿงน

Subscriptions having several publications in which the same table has been published with different column lists are not supported. That's a no-go, bestie. ๐Ÿšซ

We allow non-existent publications to be specified so that users can add those later. This means pg_subscription can have non-existent publications. So like, you can plan ahead. Future-proofing, we love to see it. ๐Ÿ”ฎ


๐ŸŒˆ 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);

See? Simple. Cute. Effective. ๐ŸŽ€


๐Ÿ’œ COMPATIBILITY ๐Ÿ’œ

CREATE SUBSCRIPTION is a PostgreSQL extension. So like, it's our thing, not a standard SQL thing. Periodt. ๐Ÿ‘‘


๐Ÿซถ SEE ALSO ๐Ÿซถ


Okay bestie, that's literally the whole page! You're now fully caught up on CREATE_SUBSCRIPTION and honestly? You're gonna slay at logical replication. ๐Ÿ’…โœจ

So like, this was PostgreSQL 15.19 documentation, dated 2026, and I hope you had as much fun reading it as I did explaining it to you. Byeeee! ๐Ÿฆ‹๐ŸŒธ๐Ÿ’–

๐Ÿ“ฆ where this page comes from