CREATE_SUBSCRIPTION(7pg17)
πΈ NAME πΈ
CREATE_SUBSCRIPTION β define a new subscription. This is the CREATE SUBSCRIPTION(7) page from the PostgreSQL 17.11 Documentation, and bestie, we're making a subscription today. π
β¨ SYNOPSIS β¨
CREATE SUBSCRIPTION subscription_name
CONNECTION 'conninfo'
PUBLICATION publication_name [, ...]
[ WITH ( subscription_parameter [= value] [, ... ] ) ]
π¦ DESCRIPTION π¦
Okay so CREATE SUBSCRIPTION adds a shiny new logical-replication subscription. The user who creates it becomes the owner β main character energy, but make it ownership. π The subscription name must be distinct from the name of any existing subscription in the current database. No duplicates, periodt.
A subscription literally represents a replication connection to the publisher. So on top of adding definitions in the local catalogs, this command normally creates a replication slot on the publisher. Slay. β¨
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. Yep, that's the exception, bestie. π
To be able to create a subscription, you must have the privileges of the pg_create_subscription role, plus CREATE privileges on the current database. Both. No skipping. π«Ά
Want more tea about subscriptions and logical replication as a whole? It's all in Section 29.2 and Chapter 29. π
β¨ PARAMETERS β¨
Okay so this is where CREATE SUBSCRIPTION gets its whole personality. These parameters are literally the blueprint, and ngl there are a lot of them β but weβre covering every single one, because missing a flag is not the vibe. π
subscription_nameβ the name of the new subscription. Thatβs it, thatβs the label. πCONNECTION 'conninfo'β thelibpqconnection string that defines how to connect to the publisher database. For details see Section 32.1.1. πPUBLICATION publication_name [, ...]β names of the publications on the publisher that you want to subscribe to. β¨WITH ( subscription_parameter [= value] [, ... ] )β this clause specifies optional parameters for a subscription. Optional, but make it make sense. π«Ά
The following parameters control what happens during subscription creation: π οΈ
connect(boolean) β says whetherCREATE SUBSCRIPTIONshould connect to the publisher at all. Default istrue. Setting this tofalsewill force the values ofcreate_slot,enabledandcopy_datatofalse. (You cannot combine settingconnecttofalsewith settingcreate_slot,enabled, orcopy_datatotrue.) Since no connection is made when this option isfalse, no tables are subscribed. To initiate replication, you must manually create the replication slot, enable the failover if required, enable the subscription, and refresh the subscription. See Section 29.2.3 for examples. π¦create_slot(boolean) β says whether the command should create the replication slot on the publisher. Default istrue. If set tofalse, you are responsible for creating the publisherβs slot in some other way. See Section 29.2.3 for examples. πenabled(boolean) β says whether the subscription should be actively replicating or whether it should just be set up but not started yet. Default istrue. π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. Settingslot_nametoNONEmeans there will be no replication slot associated with the subscription. Such subscriptions must also have bothenabledandcreate_slotset tofalse. Use this when you will be creating the replication slot later manually. See Section 29.2.3 for examples. πΈ
β οΈ Failover tea, do not skip: when settingslot_nameto a valid name andcreate_slottofalse, the failover property value of the named slot may differ from the counterpartfailoverparameter specified in the subscription. Always ensure the slot propertyfailovermatches the counterpart parameter of the subscription and vice versa. Otherwise, the slot on the publisher may behave differently from what these subscription options say: for example, the slot on the publisher could either be synced to the standbys even when the subscriptionβsfailoveroption is disabled or could be disabled for sync even when the subscriptionβsfailoveroption is enabled. π
The following parameters control the subscriptionβs replication behavior after it has been created: π
binary(boolean) β says whether the subscription will request the publisher to send the data in binary format (as opposed to text). Default isfalse. Any initial table synchronization copy (seecopy_data) also uses the same format. Binary format can be faster than the text format, but it is less portable across machine architectures and PostgreSQL versions. Binary format is very data type specific; for example, it will not allow copying from asmallintcolumn to anintegercolumn, even though that would work fine in text format. Even when this option is enabled, only data types having binary send and receive functions will be transferred in binary. Note that the initial synchronization requires all data types to have binary send and receive functions, otherwise the synchronization will fail (seeCREATE TYPE (CREATE_TYPE(7))for more about send/receive functions). π
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. Also: if the publisher is a PostgreSQL version before 16, then any initial table synchronization will use text format even if binary = true. π«
copy_data(boolean) β says whether to copy pre-existing data in the publications that are being subscribed to when the replication starts. Default istrue. If the publications containWHEREclauses, it will affect what data is copied. Refer to the Notes for details. See Notes for details of howcopy_data = truecan interact with theoriginparameter. πstreaming(enum) β says whether to enable streaming of in-progress transactions for this subscription. The default value isoff, meaning all transactions are fully decoded on the publisher and only then sent to the subscriber as a whole. If set toon, the incoming changes are written to temporary files and then applied only after the transaction is committed on the publisher and received by the subscriber. If set toparallel, incoming changes are directly applied via one of the parallel apply workers, if available. If no parallel apply worker is free to handle streaming transactions then the changes are written to temporary files and applied after the transaction is committed. Note that if an error happens in a parallel apply worker, the finishLSNof the remote transaction might not be reported in the server log. π₯synchronous_commit(enum) β the value of this parameter overrides thesynchronous_commitsetting within this subscriptionβs apply worker processes. The default value isoff. It is safe to useofffor 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 settingsynchronous_commitfor the subscriber tooffwhen the subscription is used for synchronous replication might increase the latency forCOMMITon the publisher. In this scenario, it can be advantageous to setsynchronous_committolocalor higher. πtwo_phase(boolean) β says whether two-phase commit is enabled for this subscription. Default isfalse. When two-phase commit is enabled, prepared transactions are sent to the subscriber at the time ofPREPARE 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. The implementation of two-phase commit requires that replication has successfully finished the initial table synchronization phase. So even whentwo_phaseis enabled for a subscription, the internal two-phase state remains temporarily βpendingβ until the initialization phase completes. See columnsubtwophasestateofpg_subscriptionto know the actual two-phase state. π§disable_on_error(boolean) β says whether the subscription should be automatically disabled if any errors are detected by subscription workers during data replication from the publisher. Default isfalse. πpassword_required(boolean) β if set totrue, connections to the publisher made as a result of this subscription must use password authentication and the password must be specified as a part of the connection string. This setting is ignored when the subscription is owned by a superuser. Default istrue. Only superusers can set this value tofalse. πrun_as_owner(boolean) β iftrue, all replication actions are performed as the subscription owner. Iffalse, replication workers will perform actions on each table as the owner of that table. The latter configuration is generally much more secure; for details, see Section 29.10. Default isfalse. πorigin(string) β says whether the subscription will request the publisher to only send changes that donβt have an origin or send changes regardless of origin. Settingorigintononemeans that the subscription will request the publisher to only send changes that donβt have an origin. Settingorigintoanymeans that the publisher sends changes regardless of their origin. Default isany. See Notes for details of howcopy_data = truecan interact with theoriginparameter. πfailover(boolean) β says whether the replication slots associated with the subscription are enabled to be synced to the standbys so that logical replication can be resumed from the new primary after failover. Default isfalse. π¦
And the final little boolean note, bestie: when specifying a parameter of type boolean, the = value part can be omitted, which is equivalent to specifying TRUE. Periodt. π
- π Wanna know how to lock down access control between the subscription and the publication instance? That's Section 29.10, bestie β go read it.
- π« When you're creating a replication slot (which is, like, the default behavior),
CREATE SUBSCRIPTIONcannot be executed inside a transaction block. Periodt. - π Creating a subscription that connects to the same database cluster (think: replicating between databases in the same cluster, or replicating within the same database) will only succeed if the replication slot is not created as part of the same command. Otherwise the
CREATE SUBSCRIPTIONcall will justβ¦ hang. Not slay. To make it work, create the replication slot separately (using the functionpg_create_logical_replication_slotwith the plugin namepgoutput) and then create the subscription using the parametercreate_slot = false. Examples live in Section 29.2.3. This is an implementation restriction that might be lifted in a future release. - π― WHERE clause tea, listen up: if any table in the publication has a
WHEREclause, 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 differentWHEREclauses, a row will be published if any of the expressions (referring to that publish operation) are satisfied. And in the case of differentWHEREclauses, if one of the publications has noWHEREclause (referring to that publish operation) or the publication is declared asFOR ALL TABLESorFOR TABLES IN SCHEMA, rows are always published regardless of the definition of the other expressions. - π 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
publishparameter when copying existing table data, some rows may be copied that would not be replicated using DML. See Section 29.2.2 for examples. - π Subscriptions having several publications in which the same table has been published with different column lists are not supported. Full stop.
- π» We allow non-existent publications to be specified so that users can add those later. This means
pg_subscriptioncan have non-existent publications. Spooky but true. - π«Ά When using a subscription parameter combination of
copy_data = trueandorigin = NONE, the initial sync table data is copied directly from the publisher, meaning that knowledge of the true origin of that data is not possible. And if the publisher also has subscriptions then the copied table data might have originated from further upstream. This scenario is detected and aWARNINGis logged to the user β but the warning is only an indication of a potential problem. It is the user's responsibility to make the necessary checks to ensure the copied data origins are really as wanted or not. No cap. - π Wanna find which tables might potentially include non-local origins (due to other subscriptions created on the publisher)? Try this SQL query:
# substitute <pub-names> below with your publication name(s) to be queried
SELECT DISTINCT PT.schemaname, PT.tablename
FROM pg_publication_tables PT
JOIN pg_class C ON (C.relname = PT.tablename)
JOIN pg_namespace N ON (N.nspname = PT.schemaname),
pg_subscription_rel PS
WHERE C.relnamespace = N.oid AND
(PS.srrelid = C.oid OR
C.oid IN (SELECT relid FROM pg_partition_ancestors(PS.srrelid) UNION
SELECT relid FROM pg_partition_tree(PS.srrelid))) AND
PT.pubname IN (<pub-names>);
β¨ EXAMPLES β¨
Okay so real talk, here's how it actually looks in the wild π
Example numero uno π β 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;
Example dos π¦ β 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. That's the whole vibe. π
π SEE ALSO
ALTER SUBSCRIPTION(ALTER_SUBSCRIPTION(7))DROP SUBSCRIPTION(DROP_SUBSCRIPTION(7))CREATE PUBLICATION(CREATE_PUBLICATION(7))ALTER PUBLICATION(ALTER_PUBLICATION(7))
β¨ That's the whole page, bestie β you made it to the end! π This icon was brought to you by PostgreSQL 17.11, dated 2026, filed under CREATE SUBSCRIPTION(7). Go forth and replicate responsibly π«Άπ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms