1 <!-- doc/src/sgml/ref/create_foreign_table.sgml -->
3 <refentry id="SQL-CREATEFOREIGNTABLE">
5 <refentrytitle>CREATE FOREIGN TABLE</refentrytitle>
6 <manvolnum>7</manvolnum>
7 <refmiscinfo>SQL - Language Statements</refmiscinfo>
11 <refname>CREATE FOREIGN TABLE</refname>
12 <refpurpose>define a new foreign table</refpurpose>
15 <indexterm zone="sql-createforeigntable">
16 <primary>CREATE FOREIGN TABLE</primary>
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 ] }
25 SERVER <replaceable class="parameter">server_name</replaceable>
26 [ OPTIONS ( <replaceable class="PARAMETER">option</replaceable> '<replaceable class="PARAMETER">value</replaceable>' [, ... ] ) ]
31 <refsect1 id="SQL-CREATEFOREIGNTABLE-description">
32 <title>Description</title>
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
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.
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.
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.
64 <title>Parameters</title>
69 <term><literal>IF NOT EXISTS</></term>
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
81 <term><replaceable class="PARAMETER">table_name</replaceable></term>
84 The name (optionally schema-qualified) of the table to be created.
90 <term><replaceable class="PARAMETER">column_name</replaceable></term>
93 The name of a column to be created in the new table.
99 <term><replaceable class="PARAMETER">data_type</replaceable></term>
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
111 <term><literal>NOT NULL</></term>
114 The column is not allowed to contain null values.
120 <term><literal>NULL</></term>
123 The column is allowed to contain null values. This is the default.
127 This clause is only provided for compatibility with
128 non-standard SQL databases. Its use is discouraged in new
135 <term><replaceable class="PARAMETER">server_name</replaceable></term>
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">.
146 <term><literal>OPTIONS ( <replaceable class="PARAMETER">option</replaceable> '<replaceable class="PARAMETER">value</replaceable>' [, ...] )</literal></term>
149 Options to be associated with the new foreign table or one of its
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).
164 <refsect1 id="SQL-CREATEFOREIGNTABLE-examples">
165 <title>Examples</title>
168 Create foreign table <structname>films</>, which will be accessed through
169 the server <structname>film_server</>:
172 CREATE FOREIGN TABLE films (
173 code char(5) NOT NULL,
174 title varchar(40) NOT NULL,
175 did integer NOT NULL,
178 len interval hour to minute
181 </programlisting></para>
185 <refsect1 id="SQL-CREATEFOREIGNTABLE-compatibility">
186 <title id="SQL-CREATEFOREIGNTABLE-compatibility-title">Compatibility</title>
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.
198 <title>See Also</title>
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>