CREATE PUBLICATION

CREATE PUBLICATION

7

SQL - Language Statements

CREATE PUBLICATION

define a new publication

CREATE PUBLICATION

name

\[ FOR ALL TABLES \| FOR

publication\_object

\[, \... \] \] \[ WITH (

publication\_parameter

\[=

value

\] \[, \... \] ) \]

where

publication\_object

is one of:

TABLE \[ ONLY \]

table\_name

\[ \* \] \[ (

column\_name

\[, \... \] ) \] \[ WHERE (

expression

) \] \[, \... \] ALL TABLES IN SCHEMA {

schema\_name

\| CURRENT\_SCHEMA } \[, \... \]

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
[???](#logical-replication-publication) for details about how
publications fit into the logical replication setup.

Parameters
==========

name

:   The name of the new publication.

`FOR TABLE`

:   Specifies a list of tables to add to the publication. If `ONLY` is
    specified before the table name, only that table is added to the
    publication. If `ONLY` is not specified, the table and all its
    descendant tables (if any) are added. Optionally, `*` can be
    specified after the table name to explicitly indicate that
    descendant tables are included. 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.

    If the optional `WHERE` clause is specified, rows for which the
    expression evaluates to false or null will not be published. Note
    that parentheses are required around the expression. It has no
    effect on `TRUNCATE` commands.

    When a column list is specified, only the named columns are
    replicated. If no column list is specified, all columns of the table
    are replicated through this publication, including any columns added
    later. If a column list is specified, it must include the replica
    identity columns.

    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.

    Specifying a table that is part of a schema specified by
    `FOR ALL TABLES IN SCHEMA` is not supported.

`FOR ALL TABLES`

:   Marks the publication as one that replicates changes for all tables
    in the database, including tables created in the future.

`FOR ALL 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 along with a table which belongs to the
    specified schema using `FOR TABLE` 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.

    When a partitioned table is published via 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 that are
    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. 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
        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'`.

    `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 is the default. Enabling this allows the
        changes to be replicated into a non-partitioned table or a
        partitioned table consisting of a different set of partitions.

        If this is enabled, `TRUNCATE` operations performed directly on
        partitions are not replicated.

Notes
=====

If `FOR TABLE`, `FOR ALL TABLES` or `FOR ALL TABLES IN SCHEMA` are not
specified, then the publication starts out with an empty set of tables.
That is useful if tables or schemas 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` and `FOR ALL TABLES IN SCHEMA`
clauses require 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.

A `WHERE` (i.e. row filter) expression 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
`WHERE` clause 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. If your publication contains a partitioned table, the
publication parameter `publish_via_partition_root` determines if it uses
the partition\'s row filter (if the parameter is false, the default) or
the root partitioned table\'s row filter.

For an `INSERT ... ON CONFLICT` command, the publication will publish
the operation that actually results from the command. So depending of
the outcome, it may be published as either `INSERT` or `UPDATE`, or it
may not be published at all.

`ATTACH`ing 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.

The `WHERE` clause expression is executed with the role used for the
replication connection.

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 from active departments:

    CREATE PUBLICATION active_departments FOR TABLE departments WHERE (active IS TRUE);

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');

Create a publication that publishes all changes for tables users,
departments and all changes for all the tables present in the schema
production:

    CREATE PUBLICATION production_publication FOR TABLE users, departments, ALL TABLES IN SCHEMA production;

Create a publication that publishes all changes for all the tables
present in the schemas marketing and sales:

    CREATE PUBLICATION sales_publication FOR ALL TABLES IN SCHEMA marketing, sales;

Create a publication that publishes all changes for table users, but
replicates only columns user\_id and firstname:

    CREATE PUBLICATION users_filtered FOR TABLE users (user_id, firstname);

Compatibility
=============

`CREATE PUBLICATION` is a PostgreSQL extension.

See Also
========
