CREATE_PUBLICATION(7)
๐ CREATE PUBLICATION ๐
CREATE PUBLICATION(7) โ straight out of the PostgreSQL 18.6 Documentation, and bestie, we are about to make a whole publication. No cap. ๐
โจ NAME โจ
CREATE_PUBLICATION โ define a new publication ๐ฆ
๐ธ SYNOPSIS ๐ธ
CREATE PUBLICATION name
[ FOR ALL TABLES
| FOR publication_object [, ... ] ]
[ WITH ( publication_parameter [= value] [, ... ] ) ]
where publication_object is one of:
TABLE table_and_columns [, ... ]
TABLES IN SCHEMA { schema_name | CURRENT_SCHEMA } [, ... ]
and table_and_columns is:
[ ONLY ] table_name [ * ] [ ( column_name [, ... ] ) ] [ WHERE ( expression ) ]
Like, that's the whole vibe right there. Slay. ๐
๐ซถ DESCRIPTION ๐ซถ
CREATE PUBLICATION adds a brand new publication into the current database. The publication name has to be distinct from the name of every other existing publication in the current database โ no name twins allowed, it's giving exclusive. ๐
A publication is basically a group of tables whose data changes are meant to be replicated through logical replication. If you wanna know how publications fit into the whole logical replication setup, go read Section 29.1 โ it's all laid out there. โจ
๐ PARAMETERS ๐
nameโ the name of the new publication. That's it, that's the whole job. ๐
๐ง FOR TABLE
Specifies a list of tables to add to the publication.
- If
ONLYcomes before the table name, then only that table gets added. Periodt. - If
ONLYis not specified, then the table and all its descendant tables (if there are any) get added. โจ - Optionally you can slap a
*after the table name to explicitly say "yes, descendants included, I meant that."
Heads up though โ this does not apply to a partitioned table. The partitions of a partitioned table are always implicitly considered part of the publication, so they are never explicitly added to the publication. It's automatic, bestie. ๐ฆ
If the optional WHERE clause is there, it defines a row filter expression. Rows where the expression evaluates to false or null will not be published. And yes, parentheses are required around the expression โ don't skip them, the parser will be so disappointed in you. ๐ญ Note that it has no effect on TRUNCATE commands.
When a column list is specified, only the named columns are replicated. The column list can contain stored generated columns as well. If you leave the column list out, the publication will replicate all non-generated columns by default โ including any added in the future, lowkey future-proofing. Stored generated columns can also be replicated if publish_generated_columns is set to stored. Specifying a column list has no effect on TRUNCATE commands. Section 29.5 has all the deets about column lists. ๐ธ
Only persistent base tables and partitioned tables can be part of a publication. Temporary tables, unlogged tables, foreign tables, materialized views, and regular views? Absolutely not, they cannot be part of a publication. Vibe check failed. ๐
Specifying a column list when the publication also publishes FOR TABLES IN SCHEMA is not supported. ๐ซ
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 performed directly on a partition are also published via the publications that its ancestors are part of. Main character energy propagating down the whole tree. ๐
๐ FOR ALL TABLES
Marks the publication as one that replicates changes for all tables in the database โ including tables created in the future. It's giving "everything, everywhere, all at once". โจ
๐ FOR TABLES IN SCHEMA
Marks the publication as one that replicates changes for all tables in the specified list of schemas โ including tables created in the future. ๐ฆ
Specifying a schema when the publication also publishes a table with a column list is not supported. ๐ซ
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. Sorry bestie, they're just not invited to the function. ๐
When a partitioned table is published via a 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 performed directly on a partition are also published via publications that its ancestors are part of. ๐
๐ WITH ( publication_parameter [= value] [, ... ] )
This clause specifies optional parameters for a publication. Here's what's supported:
publish(string) โ this parameter decides which DML operations will be published by the new publication to the subscribers. The value is a comma-separated list of operations. The allowed operations areinsert,update,delete, andtruncate. The default is to publish all actions, so the default value for this option is'insert, update, delete, truncate'. ๐
This parameter only affects DML operations. In particular, the initial data synchronization (see Section 29.9.1) for logical replication does not take this parameter into account when copying existing table data. Noted. ๐ฅ
publish_generated_columns(enum) โ specifies whether the generated columns present in the tables associated with the publication should be replicated. Possible values arenoneandstored. ๐
The default is none, meaning the generated columns present in the tables associated with the publication will not be replicated.
If set to stored, the stored generated columns present in the tables associated with the publication will be replicated. โจ
Note ๐ซถ If the subscriber is from a release prior to 18, then initial table synchronization won't copy generated columns even if the parameterpublish_generated_columnsisstoredin the publisher. So likeโฆ check your subscriber's age first. ๐ญ
See Section 29.6 for more details about logical replication of generated columns.
publish_via_partition_root(boolean) โ this parameter controls how changes to a partitioned table (or any of its partitions) are published. ๐ธ- When set to
true, changes are published using the identity and schema of the root partitioned table. - When set to
false(the default), changes are published using the identity and schema of the individual partitions where the changes actually occurred.
- When set to
Enabling this option allows the changes to be replicated into a non-partitioned table, or into a partitioned table whose partition structure differs from that of the publisher. Flexible, iconic, we love to see it. ๐
There can be a case where a subscription combines multiple publications. If a partitioned table is published by any subscribed publications which set 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.
This parameter also affects how row filters and column lists are chosen for partitions; see below for details. ๐
If this is enabled, TRUNCATE operations performed directly on partitions are not replicated. ๐ซ
When specifying a parameter of type boolean, the = value part can be omitted, which is equivalent to specifying TRUE. Easy. โจ
๐ NOTES ๐
If FOR TABLE, FOR ALL TABLES or FOR TABLES IN SCHEMA are not specified, then the publication starts out with an empty set of tables. That's actually super useful if tables or schemas are gonna be added later. No pressure, start empty, grow at your own pace. ๐ฑ
The creation of a publication does not start replication. It only defines a grouping and filtering logic for future subscribers. It's the plan, not the party. ๐
To create a publication, the invoking user must have the CREATE privilege for the current database. (Of course, superusers bypass this check โ must be nice. ๐)
To add a table to a publication, the invoking user must have ownership rights on the table. The FOR ALL TABLES and FOR TABLES IN SCHEMA clauses require the invoking user to be a superuser. No shortcuts, bestie. ๐
The tables added to a publication that publishes UPDATE and/or DELETE operations must have REPLICA IDENTITY defined. Otherwise those operations will be disallowed on those tables. Fr fr, don't skip this. ๐จ
Any column list must include the REPLICA IDENTITY columns in order for UPDATE or DELETE operations to be published. There are no column list restrictions if the publication publishes only INSERT operations. โจ
A row filter expression (i.e., the WHERE clause) must contain only columns that are covered by the REPLICA IDENTITY, in order for UPDATE and DELETE operations to be published. For publication of INSERT operations, any column may be used in the WHERE expression. The row filter allows simple expressions that don't have user-defined functions, user-defined operators, user-defined types, user-defined collations, non-immutable built-in functions, or references to system columns. Keep it simple, keep it clean. ๐ง
The generated columns that are part of REPLICA IDENTITY must be published explicitly โ either by listing them in the column list or by enabling the publish_generated_columns option โ in order for UPDATE and DELETE operations to be published. ๐
The row filter on a table becomes redundant if FOR TABLES IN SCHEMA is specified and the table belongs to the referred schema. Yeah, it's just extra at that point. ๐ญ
For published partitioned tables, the row filter for each partition is taken from the published partitioned table if the publication parameter publish_via_partition_root is true, or from the partition itself if it is false (the default). See Section 29.4 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 publish_via_partition_root is true, or from the partition itself if it is false. ๐
For an INSERT ... ON CONFLICT command, the publication will publish the operation that results from the command. Depending on the outcome, it may be published as either INSERT or UPDATE, or it may not be published at all. It's giving unpredictable. ๐
For a MERGE command, the publication will publish an INSERT, UPDATE, or DELETE for each row inserted, updated, or deleted. ๐ฏ
ATTACHing a table into a partition tree whose root is published using a publication with publish_via_partition_root set to true does not result in the table's existing contents being replicated. Important!! ๐จ
COPY ... FROM commands are published as INSERT operations. ๐ฆ
DDL operations are not published. Periodt. ๐
The WHERE clause expression is executed with the role used for the replication connection. ๐
โฆto be continued, bestie, there's more coming ๐
โจ EXAMPLES โจ
Okay bestie, this is the part where it all clicks, I promise. ๐
Want a publication that ships every change in two tables? Say less:
CREATE PUBLICATION mypublication FOR TABLE users, departments;
Now let's get picky โ only the rows from departments where active IS TRUE, nothing else:
CREATE PUBLICATION active_departments FOR TABLE departments WHERE (active IS TRUE);
Feeling unhinged and generous? Publish literally everything, all the tables, no filter, main character energy:
CREATE PUBLICATION alltables FOR ALL TABLES;
Only care about new rows showing up? Just INSERT, nothing else:
CREATE PUBLICATION insert_only FOR TABLE mydata
WITH (publish = 'insert');
Mixing it up โ tables users and departments, plus every table living in the schema production:
CREATE PUBLICATION production_publication FOR TABLE users, departments, TABLES IN SCHEMA production;
Whole schemas at once, two of them, slay:
CREATE PUBLICATION sales_publication FOR TABLES IN SCHEMA marketing, sales;
And my personal fave โ all of users, but only the user_id and firstname columns get replicated. Column filtering, iconic:
CREATE PUBLICATION users_filtered FOR TABLE users (user_id, firstname);
๐ซถ Pro tip from me to you: those WHERE clauses and column lists are doing real work, so keep them exactly as written. No vibes-based edits, fr fr.
๐ซ COMPATIBILITY ๐ซ
CREATE PUBLICATION is a PostgreSQL extension. It's not part of the SQL standard, it's a Postgres thing. So if you're out here expecting other databases to know what you meanโฆ they don't. It's giving exclusive. ๐
๐ SEE ALSO ๐
Go read these next, they're all connected, iykyk:
ALTER PUBLICATION(ALTER_PUBLICATION(7)) โ for when you need to change your mindDROP PUBLICATION(DROP_PUBLICATION(7)) โ for when you need to let it go ๐ญCREATE SUBSCRIPTION(CREATE_SUBSCRIPTION(7)) โ the other half of the replication love storyALTER SUBSCRIPTION(ALTER_SUBSCRIPTION(7)) โ same, but editable
And that's a wrap on CREATE_PUBLICATION(7), bestie!! ๐ This page comes from PostgreSQL 18.6 and it's dated 2026 โ yes, we're in the future, and yes, you understood the whole thing. So proud of you. Go publish something. Periodt. ๐โจ
๐ฆ where this page comes from
- alpine 3.23: postgresql18-doc 18.6-r0 ยท PostgreSQL
- alpine 3.24: postgresql18-doc 18.6-r0 ยท PostgreSQL
- arch rolling: postgresql 18.6-4 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- nixos 26.05: postgresql 18.6
- ubuntu resolute: postgresql-client-18 18.6-0ubuntu0.26.04.1 ยท PostgreSQL
- ubuntu stonking: postgresql-client-18 18.6-3build2 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms