2 .\" Title: CREATE PUBLICATION
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 "CREATE PUBLICATION" "7" "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 CREATE_PUBLICATION \- define a new publication
35 CREATE PUBLICATION \fIname\fR
37 | FOR \fIpublication_object\fR [, \&.\&.\&. ] ]
38 [ WITH ( \fIpublication_parameter\fR [= \fIvalue\fR] [, \&.\&.\&. ] ) ]
40 where \fIpublication_object\fR is one of:
42 TABLE [ ONLY ] \fItable_name\fR [ * ] [ ( \fIcolumn_name\fR [, \&.\&.\&. ] ) ] [ WHERE ( \fIexpression\fR ) ] [, \&.\&.\&. ]
43 TABLES IN SCHEMA { \fIschema_name\fR | CURRENT_SCHEMA } [, \&.\&.\&. ]
47 \fBCREATE PUBLICATION\fR
48 adds a new publication into the current database\&. The publication name must be distinct from the name of any existing publication in the current database\&.
50 A publication is essentially a group of tables whose data changes are intended to be replicated through logical replication\&. See
52 for details about how publications fit into the logical replication setup\&.
57 The name of the new publication\&.
62 Specifies a list of tables to add to the publication\&. If
64 is specified before the table name, only that table is added to the publication\&. If
66 is not specified, the table and all its descendant tables (if any) are added\&. Optionally,
68 can be specified after the table name to explicitly indicate that descendant tables are included\&. This does not apply to a partitioned table, however\&. The partitions of a partitioned table are always implicitly considered part of the publication, so they are never explicitly added to the publication\&.
72 clause is specified, it defines a
74 expression\&. Rows for which the
76 evaluates to false or null will not be published\&. Note that parentheses are required around the expression\&. It has no effect on
80 When a column list is specified, only the named columns are replicated\&. The column list can contain stored generated columns as well\&. If the column list is omitted, the publication will replicate all non\-generated columns (including any added in the future) by default\&. Stored generated columns can also be replicated if
81 publish_generated_columns
83 stored\&. Specifying a column list has no effect on
87 for details about column lists\&.
89 Only persistent base tables and partitioned tables can be part of a publication\&. Temporary tables, unlogged tables, foreign tables, materialized views, and regular views cannot be part of a publication\&.
91 Specifying a column list when the publication also publishes
95 When a partitioned table is added to a publication, all of its existing and future partitions are implicitly considered to be part of the publication\&. So, even operations that are performed directly on a partition are also published via publications that its ancestors are part of\&.
100 Marks the publication as one that replicates changes for all tables in the database, including tables created in the future\&.
105 Marks the publication as one that replicates changes for all tables in the specified list of schemas, including tables created in the future\&.
107 Specifying a schema when the publication also publishes a table with a column list is not supported\&.
109 Only persistent base tables and partitioned tables present in the schema will be included as part of the publication\&. Temporary tables, unlogged tables, foreign tables, materialized views, and regular views from the schema will not be part of the publication\&.
111 When a partitioned table is published via schema level publication, all of its existing and future partitions are implicitly considered to be part of the publication, regardless of whether they are from the publication schema or not\&. So, even operations that are performed directly on a partition are also published via publications that its ancestors are part of\&.
114 WITH ( \fIpublication_parameter\fR [= \fIvalue\fR] [, \&.\&.\&. ] )
116 This clause specifies optional parameters for a publication\&. The following parameters are supported:
120 This parameter determines which DML operations will be published by the new publication to the subscribers\&. The value is comma\-separated list of operations\&. The allowed operations are
124 truncate\&. The default is to publish all actions, and so the default value for this option is
125 \*(Aqinsert, update, delete, truncate\*(Aq\&.
127 This parameter only affects DML operations\&. In particular, the initial data synchronization (see
128 Section\ \&29.9.1) for logical replication does not take this parameter into account when copying existing table data\&.
131 publish_generated_columns (enum)
133 Specifies whether the generated columns present in the tables associated with the publication should be replicated\&. Possible values are
140 meaning the generated columns present in the tables associated with publication will not be replicated\&.
143 stored, the stored generated columns present in the tables associated with publication will be replicated\&.
149 .nr an-no-space-flag 1
156 If the subscriber is from a release prior to 18, then initial table synchronization won\*(Aqt copy generated columns even if parameter
157 publish_generated_columns
165 for more details about logical replication of generated columns\&.
168 publish_via_partition_root (boolean)
170 This parameter determines whether changes in a partitioned table (or on its partitions) contained in the publication will be published using the identity and schema of the partitioned table rather than that of the individual partitions that are actually changed; the latter is the default\&. Enabling this allows the changes to be replicated into a non\-partitioned table or a partitioned table consisting of a different set of partitions\&.
172 There can be a case where a subscription combines multiple publications\&. If a partitioned table is published by any subscribed publications which set
173 publish_via_partition_root = true, changes on this partitioned table (or on its partitions) will be published using the identity and schema of this partitioned table rather than that of the individual partitions\&.
175 This parameter also affects how row filters and column lists are chosen for partitions; see below for details\&.
179 operations performed directly on partitions are not replicated\&.
183 When specifying a parameter of type
187 part can be omitted, which is equivalent to specifying
196 are not specified, then the publication starts out with an empty set of tables\&. That is useful if tables or schemas are to be added later\&.
198 The creation of a publication does not start replication\&. It only defines a grouping and filtering logic for future subscribers\&.
200 To create a publication, the invoking user must have the
202 privilege for the current database\&. (Of course, superusers bypass this check\&.)
204 To add a table to a publication, the invoking user must have ownership rights on the table\&. The
207 \fBFOR TABLES IN SCHEMA\fR
208 clauses require the invoking user to be a superuser\&.
210 The tables added to a publication that publishes
216 defined\&. Otherwise those operations will be disallowed on those tables\&.
218 Any column list must include the
224 operations to be published\&. There are no column list restrictions if the publication publishes only
228 A row filter expression (i\&.e\&., the
230 clause) must contain only columns that are covered by the
231 REPLICA IDENTITY, in order for
235 operations to be published\&. For publication of
237 operations, any column may be used in the
239 expression\&. The row filter allows simple expressions that don\*(Aqt have user\-defined functions, user\-defined operators, user\-defined types, user\-defined collations, non\-immutable built\-in functions, or references to system columns\&.
241 The generated columns that are part of
243 must be published explicitly either by listing them in the column list or by enabling the
244 publish_generated_columns
249 operations to be published\&.
251 The row filter on a table becomes redundant if
253 is specified and the table belongs to the referred schema\&.
255 For published partitioned tables, the row filter for each partition is taken from the published partitioned table if the publication parameter
256 publish_via_partition_root
257 is true, or from the partition itself if it is false (the default)\&. See
259 for details about row filters\&. Similarly, for published partitioned tables, the column list for each partition is taken from the published partitioned table if the publication parameter
260 publish_via_partition_root
261 is true, or from the partition itself if it is false\&.
264 \fBINSERT \&.\&.\&. ON CONFLICT\fR
265 command, the publication will publish the operation that results from the command\&. Depending on the outcome, it may be published as either
268 \fBUPDATE\fR, or it may not be published at all\&.
272 command, the publication will publish an
276 for each row inserted, updated, or deleted\&.
278 \fBATTACH\fRing a table into a partition tree whose root is published using a publication with
279 publish_via_partition_root
282 does not result in the table\*(Aqs existing contents being replicated\&.
284 \fBCOPY \&.\&.\&. FROM\fR
285 commands are published as
290 operations are not published\&.
294 clause expression is executed with the role used for the replication connection\&.
297 Create a publication that publishes all changes in two tables:
303 CREATE PUBLICATION mypublication FOR TABLE users, departments;
309 Create a publication that publishes all changes from active departments:
315 CREATE PUBLICATION active_departments FOR TABLE departments WHERE (active IS TRUE);
321 Create a publication that publishes all changes in all tables:
327 CREATE PUBLICATION alltables FOR ALL TABLES;
333 Create a publication that only publishes
335 operations in one table:
341 CREATE PUBLICATION insert_only FOR TABLE mydata
342 WITH (publish = \*(Aqinsert\*(Aq);
348 Create a publication that publishes all changes for tables
351 and all changes for all the tables present in the schema
358 CREATE PUBLICATION production_publication FOR TABLE users, departments, TABLES IN SCHEMA production;
364 Create a publication that publishes all changes for all the tables present in the schemas
373 CREATE PUBLICATION sales_publication FOR TABLES IN SCHEMA marketing, sales;
379 Create a publication that publishes all changes for table
380 users, but replicates only columns
389 CREATE PUBLICATION users_filtered FOR TABLE users (user_id, firstname);
396 \fBCREATE PUBLICATION\fR
401 ALTER PUBLICATION (\fBALTER_PUBLICATION\fR(7)), DROP PUBLICATION (\fBDROP_PUBLICATION\fR(7)), CREATE SUBSCRIPTION (\fBCREATE_SUBSCRIPTION\fR(7)), ALTER SUBSCRIPTION (\fBALTER_SUBSCRIPTION\fR(7))