CREATE_PUBLICATION(7pg13)

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

πŸ’… CREATE PUBLICATION (7pg13) β€” the whole page, bestie ✨

okay so this is CREATE PUBLICATION(7) straight out of the PostgreSQL 13.23 Documentation, and I'm gonna walk you through every single bit of it 🫢 no skimming, 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 🌸

CREATE PUBLICATION adds a brand-new publication into the current database, periodt. The publication's name has to be distinct from the name of any existing publication in the current database β€” like, no duplicates, we don't do that here πŸ™ˆ

A publication is basically a group of tables whose data changes are meant to be replicated through logical replication. (A whole squad of tables, all holding hands 🫢) If you wanna know how publications fit into the logical replication setup overall, peep Section 30.1 πŸ’œ

πŸ¦‹ PARAMETERS πŸ¦‹

  • name β€” the name of the new publication ✨
  • FOR TABLE β€” specifies a list of tables to add to the publication. If ONLY is given before the table name, only that table gets added β€” no plus-ones. If ONLY is not specified, the table and all its descendant tables (if any) get added. Optionally, * can be written after the table name to explicitly say "yes, descendants included, I meant that".
⚠️ Heads up: 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. Like, they're already in the group chat, you can't re-add them 😭

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 β€” they're not invited to this party πŸ’€

When a partitioned table is added to a publication, all of its existing and future partitions are implicitly considered part of the publication. So even operations performed directly on a partition are also published via publications that its ancestors are part of. the way that works is honestly iconic πŸ‘‘

  • FOR ALL TABLES β€” marks the publication as one that replicates changes for all tables in the database, including tables created in the future 🌈 (future-proofing, we love to see it)
  • 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, and so the default value for this option is 'insert, update, delete, truncate' ✨ (yes, all four, go off)

🧚 publish_via_partition_root (boolean)

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 being the default, ngl. Enabling this allows the changes to be replicated into a non-partitioned table, or into a partitioned table consisting of a different set of partitions πŸ‘€πŸ’…

🚨 If this is enabled, TRUNCATE operations performed directly on partitions are not replicated. like, at all. remember that.

πŸ’€ NOTES πŸ’€

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

The creation of a publication does not start replication. It only defines a grouping and filtering logic for future subscribers. (It's giving "planning era", not "execution era" ✨)

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, as always πŸ‘‘)

To add a table to a publication, the invoking user must have ownership rights on the table. 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 β€” straight up blocked 😭

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 "we'll see how it goes" πŸ™ˆ)

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 πŸ¦‹

COPY ... FROM commands are published as INSERT operations ⭐

DDL operations are not published. nope. not a single one. πŸ’…

🌟 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 πŸ’œ (homegrown, iconic, ours)

πŸ‘‘ SEE ALSO πŸ‘‘


okay that's literally the whole thing, bestie, you ate that 🫢 this was PostgreSQL 13.23, dated 2025 β€” now go create some publications, slay πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from