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 ONLY comes before the table name, then only that table gets added. Periodt.
  • If ONLY is 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 are insert, update, delete, and truncate. 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 are none and stored. ๐ŸŽ€

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 parameter publish_generated_columns is stored in 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.

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:


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. ๐Ÿ‘‘โœจ