]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/create_foreign_table.sgml
Add support for piping COPY to/from an external program.
[postgresql] / doc / src / sgml / ref / create_foreign_table.sgml
1 <!-- doc/src/sgml/ref/create_foreign_table.sgml -->
2
3 <refentry id="SQL-CREATEFOREIGNTABLE">
4  <refmeta>
5   <refentrytitle>CREATE FOREIGN TABLE</refentrytitle>
6   <manvolnum>7</manvolnum>
7   <refmiscinfo>SQL - Language Statements</refmiscinfo>
8  </refmeta>
9
10  <refnamediv>
11   <refname>CREATE FOREIGN TABLE</refname>
12   <refpurpose>define a new foreign table</refpurpose>
13  </refnamediv>
14
15  <indexterm zone="sql-createforeigntable">
16   <primary>CREATE FOREIGN TABLE</primary>
17  </indexterm>
18
19  <refsynopsisdiv>
20 <synopsis>
21 CREATE FOREIGN TABLE [ IF NOT EXISTS ] <replaceable class="PARAMETER">table_name</replaceable> ( [
22   { <replaceable class="PARAMETER">column_name</replaceable> <replaceable class="PARAMETER">data_type</replaceable> [ OPTIONS ( <replaceable class="PARAMETER">option</replaceable> '<replaceable class="PARAMETER">value</replaceable>' [, ... ] ) ] [ NULL | NOT NULL ] }
23     [, ... ]
24 ] )
25   SERVER <replaceable class="parameter">server_name</replaceable>
26 [ OPTIONS ( <replaceable class="PARAMETER">option</replaceable> '<replaceable class="PARAMETER">value</replaceable>' [, ... ] ) ]
27
28 </synopsis>
29  </refsynopsisdiv>
30
31  <refsect1 id="SQL-CREATEFOREIGNTABLE-description">
32   <title>Description</title>
33
34   <para>
35    <command>CREATE FOREIGN TABLE</command> creates a new foreign table
36    in the current database. The table will be owned by the user issuing the
37    command.
38   </para>
39
40   <para>
41    If a schema name is given (for example, <literal>CREATE FOREIGN TABLE
42    myschema.mytable ...</>) then the table is created in the specified
43    schema.  Otherwise it is created in the current schema.
44    The name of the foreign table must be
45    distinct from the name of any other foreign table, table, sequence, index,
46    or view in the same schema.
47   </para>
48
49   <para>
50    <command>CREATE FOREIGN TABLE</command> also automatically creates a data
51    type that represents the composite type corresponding to one row of
52    the foreign table.  Therefore, foreign tables cannot have the same
53    name as any existing data type in the same schema.
54   </para>
55
56   <para>
57    To be able to create a foreign table, you must have <literal>USAGE</literal>
58    privilege on the foreign server, as well as <literal>USAGE</literal>
59    privilege on all column types used in the table.
60   </para>
61  </refsect1>
62
63  <refsect1>
64   <title>Parameters</title>
65
66   <variablelist>
67
68    <varlistentry>
69     <term><literal>IF NOT EXISTS</></term>
70     <listitem>
71      <para>
72       Do not throw an error if a relation with the same name already exists.
73       A notice is issued in this case.  Note that there is no guarantee that
74       the existing relation is anything like the one that would have been
75       created.
76      </para>
77     </listitem>
78    </varlistentry>
79
80    <varlistentry>
81     <term><replaceable class="PARAMETER">table_name</replaceable></term>
82     <listitem>
83      <para>
84       The name (optionally schema-qualified) of the table to be created.
85      </para>
86     </listitem>
87    </varlistentry>
88
89    <varlistentry>
90     <term><replaceable class="PARAMETER">column_name</replaceable></term>
91     <listitem>
92      <para>
93       The name of a column to be created in the new table.
94      </para>
95     </listitem>
96    </varlistentry>
97
98    <varlistentry>
99     <term><replaceable class="PARAMETER">data_type</replaceable></term>
100     <listitem>
101      <para>
102       The data type of the column. This can include array
103       specifiers. For more information on the data types supported by
104       <productname>PostgreSQL</productname>, refer to <xref
105       linkend="datatype">.
106      </para>
107     </listitem>
108    </varlistentry>
109
110    <varlistentry>
111     <term><literal>NOT NULL</></term>
112     <listitem>
113      <para>
114       The column is not allowed to contain null values.
115      </para>
116     </listitem>
117    </varlistentry>
118
119    <varlistentry>
120     <term><literal>NULL</></term>
121     <listitem>
122      <para>
123       The column is allowed to contain null values. This is the default.
124      </para>
125
126      <para>
127       This clause is only provided for compatibility with
128       non-standard SQL databases.  Its use is discouraged in new
129       applications.
130      </para>
131     </listitem>
132    </varlistentry>
133
134    <varlistentry>
135     <term><replaceable class="PARAMETER">server_name</replaceable></term>
136     <listitem>
137      <para>
138       The name of an existing foreign server to use for the foreign table.
139       For details on defining a server, see <xref
140       linkend="SQL-CREATESERVER">.
141      </para>
142     </listitem>
143    </varlistentry>
144
145    <varlistentry>
146     <term><literal>OPTIONS ( <replaceable class="PARAMETER">option</replaceable> '<replaceable class="PARAMETER">value</replaceable>' [, ...] )</literal></term>
147     <listitem>
148      <para>
149       Options to be associated with the new foreign table or one of its
150       columns.
151       The allowed option names and values are specific to each foreign
152       data wrapper and are validated using the foreign-data wrapper's
153       validator function.  Duplicate option names are not allowed (although
154       it's OK for a table option and a column option to have the same name).
155      </para>
156     </listitem>
157    </varlistentry>
158
159   </variablelist>
160
161  </refsect1>
162
163
164  <refsect1 id="SQL-CREATEFOREIGNTABLE-examples">
165   <title>Examples</title>
166
167   <para>
168    Create foreign table <structname>films</>, which will be accessed through
169    the server <structname>film_server</>:
170
171 <programlisting>
172 CREATE FOREIGN TABLE films (
173     code        char(5) NOT NULL,
174     title       varchar(40) NOT NULL,
175     did         integer NOT NULL,
176     date_prod   date,
177     kind        varchar(10),
178     len         interval hour to minute
179 )
180 SERVER film_server;
181 </programlisting></para>
182
183  </refsect1>
184
185  <refsect1 id="SQL-CREATEFOREIGNTABLE-compatibility">
186   <title id="SQL-CREATEFOREIGNTABLE-compatibility-title">Compatibility</title>
187
188   <para>
189    The <command>CREATE FOREIGN TABLE</command> command largely conforms to the
190    <acronym>SQL</acronym> standard; however, much as with
191    <link linkend="sql-createtable"><command>CREATE TABLE</></link>,
192    <literal>NULL</> constraints and zero-column foreign tables are permitted.
193   </para>
194
195  </refsect1>
196
197  <refsect1>
198   <title>See Also</title>
199
200   <simplelist type="inline">
201    <member><xref linkend="sql-alterforeigntable"></member>
202    <member><xref linkend="sql-dropforeigntable"></member>
203    <member><xref linkend="sql-createtable"></member>
204    <member><xref linkend="sql-createserver"></member>
205   </simplelist>
206  </refsect1>
207 </refentry>