table of contents
CREATE PUBLICATION(7) | PostgreSQL 13.18 Documentation | CREATE PUBLICATION(7) |
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 new publication into the current database. The publication name must be distinct from the name of any existing publication in the current database.
A publication is essentially a group of tables whose data changes are intended to be replicated through logical replication. See Section 30.1 for details about how publications fit into the logical replication setup.
PARAMETERS¶
name
FOR TABLE
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.
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.
FOR ALL TABLES
WITH ( publication_parameter [= value] [, ... ] )
publish (string)
publish_via_partition_root (boolean)
If this is enabled, TRUNCATE operations performed directly on partitions are not replicated.
NOTES¶
If neither FOR TABLE nor FOR ALL TABLES is specified, then the publication starts out with an empty set of tables. That is useful if tables are to be added later.
The creation of a publication does not start replication. It only defines a grouping and filtering logic for future subscribers.
To create a publication, the invoking user must have the CREATE privilege for the current database. (Of course, superusers bypass this check.)
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.
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.
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.
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.
SEE ALSO¶
ALTER PUBLICATION (ALTER_PUBLICATION(7)), DROP PUBLICATION (DROP_PUBLICATION(7)), CREATE SUBSCRIPTION (CREATE_SUBSCRIPTION(7)), ALTER SUBSCRIPTION (ALTER_SUBSCRIPTION(7))
2024 | PostgreSQL 13.18 |