CREATE_PUBLICATION(7pg14)

πŸ“¦ version 14.24, in opensuse-leap 16.0

πŸ’– CREATE_PUBLICATION πŸ’–

okay so bestie, this is the PostgreSQL 14.24 documentation page for CREATE_PUBLICATION, and we're gonna get you fluent in it, no cap ✨


🌸 NAME 🌸

CREATE_PUBLICATION β€” define a new publication πŸŽ€

✨ SYNOPSIS ✨

CREATE PUBLICATION name
    [ FOR TABLE [ ONLY ] table_name [ * ] [, ...]
      | FOR ALL TABLES ]
    [ WITH ( publication_parameter [= value] [, ... ] ) ]

πŸ¦‹ DESCRIPTION πŸ¦‹

So CREATE PUBLICATION adds a brand-new publication into the current database. And the name? It has to be distinct from the name of any existing publication in the current database β€” no duplicate names, periodt. πŸ’…

A publication is basically a group of tables whose data changes are meant to be replicated through logical replication. Like, that's the whole vibe of it. If you wanna see how publications fit into the logical replication setup long-form, Section 31.1 has the details, iykyk. πŸ“š

πŸ’œ PARAMETERS πŸ’œ

  • name β€” the name of the new publication, obviously 🫢
  • FOR TABLE β€” specifies a list of tables to add to the publication. If ONLY is written before the table name, then only that table gets added. If ONLY is not specified, the table and all its descendant tables (if any) get added too. Optionally you can slap a * after the table name to explicitly indicate that descendant tables are included. ⭐
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 it. πŸ’€

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. Not them. πŸ™ˆ

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 publications that its ancestors are part of. It's giving family tree energy. 🌈

  • FOR ALL TABLES β€” marks the publication as one that replicates changes for all tables in the database, including tables created in the future πŸ‘‘
  • WITH ( publication_parameter [= value] [, ... ] ) β€” this clause specifies optional parameters for a publication. The following parameters are supported:
    • publish (string) β€” this parameter determines 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'. πŸ“
    • 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. 🧚
If this is enabled, TRUNCATE operations performed directly on partitions are not replicated. Important, don't forget πŸ’…

πŸ”₯ NOTES πŸ”₯

If neither FOR TABLE nor FOR ALL TABLES is specified, then the publication starts out with an empty set of tables. That's actually useful if tables are gonna be added later. πŸŽ€

Creating a publication does not start replication. It only defines a grouping and filtering logic for future subscribers. Like, it's the blueprint, not the build. 😭

To create a publication, the invoking user must have the CREATE privilege for the current database. (Of course, superusers bypass this check. main character energy πŸ‘‘)

To add a table to a publication, the invoking user must have ownership rights on the table. And the FOR ALL TABLES clause requires the invoking user to be a superuser. ⭐

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. No identity, no updates, fr fr. πŸ’€

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 chaotic neutral. πŸ™ˆ

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. Just so you know. πŸ’œ

COPY ... FROM commands are published as INSERT operations. πŸ¦‹

DDL operations are not published. Periodt. ✨

🌟 EXAMPLES 🌟

Create a publication that publishes all changes in two tables:

CREATE PUBLICATION mypublication FOR TABLE users, departments;

Create a publication that publishes all changes in all tables:

CREATE PUBLICATION alltables FOR ALL TABLES;

Create a publication that only publishes INSERT operations in one table:

CREATE PUBLICATION insert_only FOR TABLE mydata
    WITH (publish = 'insert');

πŸ’… COMPATIBILITY πŸ’…

CREATE PUBLICATION is a PostgreSQL extension. That's it, that's the whole section. 🫢

🌈 SEE ALSO 🌈


okay bestie you made it to the end, iconic behavior honestly πŸŽ€ this was PostgreSQL 14.24 documentation, dated 2026, and the page you just read is CREATE_PUBLICATION(7) β€” go forth and publish, slay πŸ’–βœ¨

πŸ“¦ where this page comes from