summaryrefslogtreecommitdiff
path: root/doc/src/sgml/ref/create_publication.sgml
blob: 3cdde801fa1f6f35fa222ee0017b2fcaec8a117c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!--
doc/src/sgml/ref/create_publication.sgml
PostgreSQL documentation
-->

<refentry id="SQL-CREATEPUBLICATION">
 <indexterm zone="sql-createpublication">
  <primary>CREATE PUBLICATION</primary>
 </indexterm>

 <refmeta>
  <refentrytitle>CREATE PUBLICATION</refentrytitle>
  <manvolnum>7</manvolnum>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>

 <refnamediv>
  <refname>CREATE PUBLICATION</refname>
  <refpurpose>define a new publication</refpurpose>
 </refnamediv>

 <refsynopsisdiv>
<synopsis>
CREATE PUBLICATION <replaceable class="parameter">name</replaceable>
    [ FOR TABLE <replaceable class="parameter">table_name</replaceable> [, ...]
      | FOR ALL TABLES ]
    [ WITH ( <replaceable class="parameter">option</replaceable> [, ... ] ) ]

<phrase>where <replaceable class="parameter">option</replaceable> can be:</phrase>

      PUBLISH INSERT | NOPUBLISH INSERT
    | PUBLISH UPDATE | NOPUBLISH UPDATE
    | PUBLISH DELETE | NOPUBLISH DELETE
</synopsis>
 </refsynopsisdiv>

 <refsect1>
  <title>Description</title>

  <para>
   <command>CREATE PUBLICATION</command> 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.
  </para>

  <para>
   A publication is essentially a group of tables whose data changes are
   intended to be replicated through logical replication.  See
   <xref linkend="logical-replication-publication"> for details about how
   publications fit into the logical replication setup.
   </para>
 </refsect1>

 <refsect1>
  <title>Parameters</title>

  <variablelist>
   <varlistentry>
    <term><replaceable class="parameter">name</replaceable></term>
    <listitem>
     <para>
      The name of the new publication.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><literal>FOR TABLE</literal></term>
    <listitem>
     <para>
      Specifies a list of tables to add to the publication.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><literal>FOR ALL TABLES</literal></term>
    <listitem>
     <para>
      Marks the publication as one that replicates changes for all tables in
      the database, including tables created in the future.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><literal>PUBLISH INSERT</literal></term>
    <term><literal>NOPUBLISH INSERT</literal></term>
    <listitem>
     <para>
      These clauses determine whether the new publication will send
      the <command>INSERT</command> operations to the subscribers.
      <literal>PUBLISH INSERT</literal> is the default.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><literal>PUBLISH UPDATE</literal></term>
    <term><literal>NOPUBLISH UPDATE</literal></term>
    <listitem>
     <para>
      These clauses determine whether the new publication will send
      the <command>UPDATE</command> operations to the subscribers.
      <literal>PUBLISH UPDATE</literal> is the default.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><literal>PUBLISH DELETE</literal></term>
    <term><literal>NOPUBLISH DELETE</literal></term>
    <listitem>
     <para>
      These clauses determine whether the new publication will send
      the <command>DELETE</command> operations to the subscribers.
      <literal>PUBLISH DELETE</literal> is the default.
     </para>
    </listitem>
   </varlistentry>

  </variablelist>
 </refsect1>

 <refsect1>
  <title>Notes</title>

  <para>
   If neither <literal>FOR TABLE</literal> nor <literal>FOR ALL
   TABLES</literal> is specified, then the publication starts out with an
   empty set of tables.  That is useful if tables are to be added later.
  </para>

  <para>
   The creation of a publication does not start replication.  It only defines
   a grouping and filtering logic for future subscribers.
  </para>

  <para>
   To create a publication, the invoking user must have the
   <literal>CREATE</> privilege for the current database.
   (Of course, superusers bypass this check.)
  </para>

  <para>
   To add a table to a publication, the invoking user must have ownership
   rights on the table.  The <command>FOR ALL TABLES</command> clause requires
   the invoking user to be a superuser.
  </para>

  <para>
   The tables added to a publication that publishes <command>UPDATE</command>
   and/or <command>DELETE</command> operations must have
   <literal>REPLICA IDENTITY</> defined.  Otherwise those operations will be
   disallowed on those tables.
  </para>

  <para>
   For an <command>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 <command>INSERT</command> or
   <command>UPDATE</command>, or it may not be published at all.
  </para>

  <para>
   <command>TRUNCATE</command> and <acronym>DDL</acronym> operations
   are not published.
  </para>
 </refsect1>

 <refsect1>
  <title>Examples</title>

  <para>
   Create a publication that publishes all changes in two tables:
<programlisting>
CREATE PUBLICATION mypublication FOR TABLE users, departments;
</programlisting>
  </para>

  <para>
   Create a publication that publishes all changes in all tables:
<programlisting>
CREATE PUBLICATION alltables FOR ALL TABLES;
</programlisting>
  </para>

  <para>
   Create a publication that only publishes <command>INSERT</command>
   operations in one table:
<programlisting>
CREATE PUBLICATION insert_only FOR TABLE mydata
    WITH (NOPUBLISH UPDATE, NOPUBLISH DELETE);
</programlisting>
  </para>
 </refsect1>

 <refsect1>
  <title>Compatibility</title>

  <para>
   <command>CREATE PUBLICATION</command> is a <productname>PostgreSQL</>
   extension.
  </para>
 </refsect1>

 <refsect1>
  <title>See Also</title>

  <simplelist type="inline">
   <member><xref linkend="sql-alterpublication"></member>
   <member><xref linkend="sql-droppublication"></member>
  </simplelist>
 </refsect1>
</refentry>