]> begriffs open source - ai-pg/blob - full-docs/man7/CREATE_PUBLICATION.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / CREATE_PUBLICATION.7
1 '\" t
2 .\"     Title: CREATE PUBLICATION
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
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 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 CREATE_PUBLICATION \- define a new publication
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 CREATE PUBLICATION \fIname\fR
36     [ FOR ALL TABLES
37       | FOR \fIpublication_object\fR [, \&.\&.\&. ] ]
38     [ WITH ( \fIpublication_parameter\fR [= \fIvalue\fR] [, \&.\&.\&. ] ) ]
39
40 where \fIpublication_object\fR is one of:
41
42     TABLE [ ONLY ] \fItable_name\fR [ * ] [ ( \fIcolumn_name\fR [, \&.\&.\&. ] ) ] [ WHERE ( \fIexpression\fR ) ] [, \&.\&.\&. ]
43     TABLES IN SCHEMA { \fIschema_name\fR | CURRENT_SCHEMA } [, \&.\&.\&. ]
44 .fi
45 .SH "DESCRIPTION"
46 .PP
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\&.
49 .PP
50 A publication is essentially a group of tables whose data changes are intended to be replicated through logical replication\&. See
51 Section\ \&29.1
52 for details about how publications fit into the logical replication setup\&.
53 .SH "PARAMETERS"
54 .PP
55 \fIname\fR
56 .RS 4
57 The name of the new publication\&.
58 .RE
59 .PP
60 FOR TABLE
61 .RS 4
62 Specifies a list of tables to add to the publication\&. If
63 ONLY
64 is specified before the table name, only that table is added to the publication\&. If
65 ONLY
66 is not specified, the table and all its descendant tables (if any) are added\&. Optionally,
67 *
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\&.
69 .sp
70 If the optional
71 WHERE
72 clause is specified, it defines a
73 row filter
74 expression\&. Rows for which the
75 \fIexpression\fR
76 evaluates to false or null will not be published\&. Note that parentheses are required around the expression\&. It has no effect on
77 TRUNCATE
78 commands\&.
79 .sp
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
82 is set to
83 stored\&. Specifying a column list has no effect on
84 TRUNCATE
85 commands\&. See
86 Section\ \&29.5
87 for details about column lists\&.
88 .sp
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\&.
90 .sp
91 Specifying a column list when the publication also publishes
92 FOR TABLES IN SCHEMA
93 is not supported\&.
94 .sp
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\&.
96 .RE
97 .PP
98 FOR ALL TABLES
99 .RS 4
100 Marks the publication as one that replicates changes for all tables in the database, including tables created in the future\&.
101 .RE
102 .PP
103 FOR TABLES IN SCHEMA
104 .RS 4
105 Marks the publication as one that replicates changes for all tables in the specified list of schemas, including tables created in the future\&.
106 .sp
107 Specifying a schema when the publication also publishes a table with a column list is not supported\&.
108 .sp
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\&.
110 .sp
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\&.
112 .RE
113 .PP
114 WITH ( \fIpublication_parameter\fR [= \fIvalue\fR] [, \&.\&.\&. ] )
115 .RS 4
116 This clause specifies optional parameters for a publication\&. The following parameters are supported:
117 .PP
118 publish (string)
119 .RS 4
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
121 insert,
122 update,
123 delete, and
124 truncate\&. The default is to publish all actions, and so the default value for this option is
125 \*(Aqinsert, update, delete, truncate\*(Aq\&.
126 .sp
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\&.
129 .RE
130 .PP
131 publish_generated_columns (enum)
132 .RS 4
133 Specifies whether the generated columns present in the tables associated with the publication should be replicated\&. Possible values are
134 none
135 and
136 stored\&.
137 .sp
138 The default is
139 none
140 meaning the generated columns present in the tables associated with publication will not be replicated\&.
141 .sp
142 If set to
143 stored, the stored generated columns present in the tables associated with publication will be replicated\&.
144 .if n \{\
145 .sp
146 .\}
147 .RS 4
148 .it 1 an-trap
149 .nr an-no-space-flag 1
150 .nr an-break-flag 1
151 .br
152 .ps +1
153 \fBNote\fR
154 .ps -1
155 .br
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
158 is
159 stored
160 in the publisher\&.
161 .sp .5v
162 .RE
163 See
164 Section\ \&29.6
165 for more details about logical replication of generated columns\&.
166 .RE
167 .PP
168 publish_via_partition_root (boolean)
169 .RS 4
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\&.
171 .sp
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\&.
174 .sp
175 This parameter also affects how row filters and column lists are chosen for partitions; see below for details\&.
176 .sp
177 If this is enabled,
178 TRUNCATE
179 operations performed directly on partitions are not replicated\&.
180 .RE
181 .RE
182 .PP
183 When specifying a parameter of type
184 boolean, the
185 =
186 \fIvalue\fR
187 part can be omitted, which is equivalent to specifying
188 TRUE\&.
189 .SH "NOTES"
190 .PP
191 If
192 FOR TABLE,
193 FOR ALL TABLES
194 or
195 FOR TABLES IN SCHEMA
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\&.
197 .PP
198 The creation of a publication does not start replication\&. It only defines a grouping and filtering logic for future subscribers\&.
199 .PP
200 To create a publication, the invoking user must have the
201 CREATE
202 privilege for the current database\&. (Of course, superusers bypass this check\&.)
203 .PP
204 To add a table to a publication, the invoking user must have ownership rights on the table\&. The
205 \fBFOR ALL TABLES\fR
206 and
207 \fBFOR TABLES IN SCHEMA\fR
208 clauses require the invoking user to be a superuser\&.
209 .PP
210 The tables added to a publication that publishes
211 \fBUPDATE\fR
212 and/or
213 \fBDELETE\fR
214 operations must have
215 REPLICA IDENTITY
216 defined\&. Otherwise those operations will be disallowed on those tables\&.
217 .PP
218 Any column list must include the
219 REPLICA IDENTITY
220 columns in order for
221 \fBUPDATE\fR
222 or
223 \fBDELETE\fR
224 operations to be published\&. There are no column list restrictions if the publication publishes only
225 \fBINSERT\fR
226 operations\&.
227 .PP
228 A row filter expression (i\&.e\&., the
229 WHERE
230 clause) must contain only columns that are covered by the
231 REPLICA IDENTITY, in order for
232 \fBUPDATE\fR
233 and
234 \fBDELETE\fR
235 operations to be published\&. For publication of
236 \fBINSERT\fR
237 operations, any column may be used in the
238 WHERE
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\&.
240 .PP
241 The generated columns that are part of
242 REPLICA IDENTITY
243 must be published explicitly either by listing them in the column list or by enabling the
244 publish_generated_columns
245 option, in order for
246 \fBUPDATE\fR
247 and
248 \fBDELETE\fR
249 operations to be published\&.
250 .PP
251 The row filter on a table becomes redundant if
252 FOR TABLES IN SCHEMA
253 is specified and the table belongs to the referred schema\&.
254 .PP
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
258 Section\ \&29.4
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\&.
262 .PP
263 For an
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
266 \fBINSERT\fR
267 or
268 \fBUPDATE\fR, or it may not be published at all\&.
269 .PP
270 For a
271 \fBMERGE\fR
272 command, the publication will publish an
273 \fBINSERT\fR,
274 \fBUPDATE\fR, or
275 \fBDELETE\fR
276 for each row inserted, updated, or deleted\&.
277 .PP
278 \fBATTACH\fRing a table into a partition tree whose root is published using a publication with
279 publish_via_partition_root
280 set to
281 true
282 does not result in the table\*(Aqs existing contents being replicated\&.
283 .PP
284 \fBCOPY \&.\&.\&. FROM\fR
285 commands are published as
286 \fBINSERT\fR
287 operations\&.
288 .PP
289 DDL
290 operations are not published\&.
291 .PP
292 The
293 WHERE
294 clause expression is executed with the role used for the replication connection\&.
295 .SH "EXAMPLES"
296 .PP
297 Create a publication that publishes all changes in two tables:
298 .sp
299 .if n \{\
300 .RS 4
301 .\}
302 .nf
303 CREATE PUBLICATION mypublication FOR TABLE users, departments;
304 .fi
305 .if n \{\
306 .RE
307 .\}
308 .PP
309 Create a publication that publishes all changes from active departments:
310 .sp
311 .if n \{\
312 .RS 4
313 .\}
314 .nf
315 CREATE PUBLICATION active_departments FOR TABLE departments WHERE (active IS TRUE);
316 .fi
317 .if n \{\
318 .RE
319 .\}
320 .PP
321 Create a publication that publishes all changes in all tables:
322 .sp
323 .if n \{\
324 .RS 4
325 .\}
326 .nf
327 CREATE PUBLICATION alltables FOR ALL TABLES;
328 .fi
329 .if n \{\
330 .RE
331 .\}
332 .PP
333 Create a publication that only publishes
334 \fBINSERT\fR
335 operations in one table:
336 .sp
337 .if n \{\
338 .RS 4
339 .\}
340 .nf
341 CREATE PUBLICATION insert_only FOR TABLE mydata
342     WITH (publish = \*(Aqinsert\*(Aq);
343 .fi
344 .if n \{\
345 .RE
346 .\}
347 .PP
348 Create a publication that publishes all changes for tables
349 users,
350 departments
351 and all changes for all the tables present in the schema
352 production:
353 .sp
354 .if n \{\
355 .RS 4
356 .\}
357 .nf
358 CREATE PUBLICATION production_publication FOR TABLE users, departments, TABLES IN SCHEMA production;
359 .fi
360 .if n \{\
361 .RE
362 .\}
363 .PP
364 Create a publication that publishes all changes for all the tables present in the schemas
365 marketing
366 and
367 sales:
368 .sp
369 .if n \{\
370 .RS 4
371 .\}
372 .nf
373 CREATE PUBLICATION sales_publication FOR TABLES IN SCHEMA marketing, sales;
374 .fi
375 .if n \{\
376 .RE
377 .\}
378 .PP
379 Create a publication that publishes all changes for table
380 users, but replicates only columns
381 user_id
382 and
383 firstname:
384 .sp
385 .if n \{\
386 .RS 4
387 .\}
388 .nf
389 CREATE PUBLICATION users_filtered FOR TABLE users (user_id, firstname);
390 .fi
391 .if n \{\
392 .RE
393 .\}
394 .SH "COMPATIBILITY"
395 .PP
396 \fBCREATE PUBLICATION\fR
397 is a
398 PostgreSQL
399 extension\&.
400 .SH "SEE ALSO"
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))