1CREATE FOREIGN TABLE(7)  PostgreSQL 13.4 Documentation CREATE FOREIGN TABLE(7)
2
3
4

NAME

6       CREATE_FOREIGN_TABLE - define a new foreign table
7

SYNOPSIS

9       CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name ( [
10         { column_name data_type [ OPTIONS ( option 'value' [, ... ] ) ] [ COLLATE collation ] [ column_constraint [ ... ] ]
11           | table_constraint }
12           [, ... ]
13       ] )
14       [ INHERITS ( parent_table [, ... ] ) ]
15         SERVER server_name
16       [ OPTIONS ( option 'value' [, ... ] ) ]
17
18       CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name
19         PARTITION OF parent_table [ (
20         { column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]
21           | table_constraint }
22           [, ... ]
23       ) ] partition_bound_spec
24         SERVER server_name
25       [ OPTIONS ( option 'value' [, ... ] ) ]
26
27       where column_constraint is:
28
29       [ CONSTRAINT constraint_name ]
30       { NOT NULL |
31         NULL |
32         CHECK ( expression ) [ NO INHERIT ] |
33         DEFAULT default_expr |
34         GENERATED ALWAYS AS ( generation_expr ) STORED }
35
36       and table_constraint is:
37
38       [ CONSTRAINT constraint_name ]
39       CHECK ( expression ) [ NO INHERIT ]
40

DESCRIPTION

42       CREATE FOREIGN TABLE creates a new foreign table in the current
43       database. The table will be owned by the user issuing the command.
44
45       If a schema name is given (for example, CREATE FOREIGN TABLE
46       myschema.mytable ...) then the table is created in the specified
47       schema. Otherwise it is created in the current schema. The name of the
48       foreign table must be distinct from the name of any other foreign
49       table, table, sequence, index, view, or materialized view in the same
50       schema.
51
52       CREATE FOREIGN TABLE also automatically creates a data type that
53       represents the composite type corresponding to one row of the foreign
54       table. Therefore, foreign tables cannot have the same name as any
55       existing data type in the same schema.
56
57       If PARTITION OF clause is specified then the table is created as a
58       partition of parent_table with specified bounds.
59
60       To be able to create a foreign table, you must have USAGE privilege on
61       the foreign server, as well as USAGE privilege on all column types used
62       in the table.
63

PARAMETERS

65       IF NOT EXISTS
66           Do not throw an error if a relation with the same name already
67           exists. A notice is issued in this case. Note that there is no
68           guarantee that the existing relation is anything like the one that
69           would have been created.
70
71       table_name
72           The name (optionally schema-qualified) of the table to be created.
73
74       column_name
75           The name of a column to be created in the new table.
76
77       data_type
78           The data type of the column. This can include array specifiers. For
79           more information on the data types supported by PostgreSQL, refer
80           to Chapter 8.
81
82       COLLATE collation
83           The COLLATE clause assigns a collation to the column (which must be
84           of a collatable data type). If not specified, the column data
85           type's default collation is used.
86
87       INHERITS ( parent_table [, ... ] )
88           The optional INHERITS clause specifies a list of tables from which
89           the new foreign table automatically inherits all columns. Parent
90           tables can be plain tables or foreign tables. See the similar form
91           of CREATE TABLE (CREATE_TABLE(7)) for more details.
92
93       PARTITION OF parent_table FOR VALUES partition_bound_spec
94           This form can be used to create the foreign table as partition of
95           the given parent table with specified partition bound values. See
96           the similar form of CREATE TABLE (CREATE_TABLE(7)) for more
97           details. Note that it is currently not allowed to create the
98           foreign table as a partition of the parent table if there are
99           UNIQUE indexes on the parent table. (See also ALTER TABLE ATTACH
100           PARTITION.)
101
102       CONSTRAINT constraint_name
103           An optional name for a column or table constraint. If the
104           constraint is violated, the constraint name is present in error
105           messages, so constraint names like col must be positive can be used
106           to communicate helpful constraint information to client
107           applications. (Double-quotes are needed to specify constraint names
108           that contain spaces.) If a constraint name is not specified, the
109           system generates a name.
110
111       NOT NULL
112           The column is not allowed to contain null values.
113
114       NULL
115           The column is allowed to contain null values. This is the default.
116
117           This clause is only provided for compatibility with non-standard
118           SQL databases. Its use is discouraged in new applications.
119
120       CHECK ( expression ) [ NO INHERIT ]
121           The CHECK clause specifies an expression producing a Boolean result
122           which each row in the foreign table is expected to satisfy; that
123           is, the expression should produce TRUE or UNKNOWN, never FALSE, for
124           all rows in the foreign table. A check constraint specified as a
125           column constraint should reference that column's value only, while
126           an expression appearing in a table constraint can reference
127           multiple columns.
128
129           Currently, CHECK expressions cannot contain subqueries nor refer to
130           variables other than columns of the current row. The system column
131           tableoid may be referenced, but not any other system column.
132
133           A constraint marked with NO INHERIT will not propagate to child
134           tables.
135
136       DEFAULT default_expr
137           The DEFAULT clause assigns a default data value for the column
138           whose column definition it appears within. The value is any
139           variable-free expression (subqueries and cross-references to other
140           columns in the current table are not allowed). The data type of the
141           default expression must match the data type of the column.
142
143           The default expression will be used in any insert operation that
144           does not specify a value for the column. If there is no default for
145           a column, then the default is null.
146
147       GENERATED ALWAYS AS ( generation_expr ) STORED
148           This clause creates the column as a generated column. The column
149           cannot be written to, and when read the result of the specified
150           expression will be returned.
151
152           The keyword STORED is required to signify that the column will be
153           computed on write. (The computed value will be presented to the
154           foreign-data wrapper for storage and must be returned on reading.)
155
156           The generation expression can refer to other columns in the table,
157           but not other generated columns. Any functions and operators used
158           must be immutable. References to other tables are not allowed.
159
160       server_name
161           The name of an existing foreign server to use for the foreign
162           table. For details on defining a server, see CREATE SERVER
163           (CREATE_SERVER(7)).
164
165       OPTIONS ( option 'value' [, ...] )
166           Options to be associated with the new foreign table or one of its
167           columns. The allowed option names and values are specific to each
168           foreign data wrapper and are validated using the foreign-data
169           wrapper's validator function. Duplicate option names are not
170           allowed (although it's OK for a table option and a column option to
171           have the same name).
172

NOTES

174       Constraints on foreign tables (such as CHECK or NOT NULL clauses) are
175       not enforced by the core PostgreSQL system, and most foreign data
176       wrappers do not attempt to enforce them either; that is, the constraint
177       is simply assumed to hold true. There would be little point in such
178       enforcement since it would only apply to rows inserted or updated via
179       the foreign table, and not to rows modified by other means, such as
180       directly on the remote server. Instead, a constraint attached to a
181       foreign table should represent a constraint that is being enforced by
182       the remote server.
183
184       Some special-purpose foreign data wrappers might be the only access
185       mechanism for the data they access, and in that case it might be
186       appropriate for the foreign data wrapper itself to perform constraint
187       enforcement. But you should not assume that a wrapper does that unless
188       its documentation says so.
189
190       Although PostgreSQL does not attempt to enforce constraints on foreign
191       tables, it does assume that they are correct for purposes of query
192       optimization. If there are rows visible in the foreign table that do
193       not satisfy a declared constraint, queries on the table might produce
194       incorrect answers. It is the user's responsibility to ensure that the
195       constraint definition matches reality.
196
197       Similar considerations apply to generated columns. Stored generated
198       columns are computed on insert or update on the local PostgreSQL server
199       and handed to the foreign-data wrapper for writing out to the foreign
200       data store, but it is not enforced that a query of the foreign table
201       returns values for stored generated columns that are consistent with
202       the generation expression. Again, this might result in incorrect query
203       results.
204
205       While rows can be moved from local partitions to a foreign-table
206       partition (provided the foreign data wrapper supports tuple routing),
207       they cannot be moved from a foreign-table partition to another
208       partition.
209

EXAMPLES

211       Create foreign table films, which will be accessed through the server
212       film_server:
213
214           CREATE FOREIGN TABLE films (
215               code        char(5) NOT NULL,
216               title       varchar(40) NOT NULL,
217               did         integer NOT NULL,
218               date_prod   date,
219               kind        varchar(10),
220               len         interval hour to minute
221           )
222           SERVER film_server;
223
224       Create foreign table measurement_y2016m07, which will be accessed
225       through the server server_07, as a partition of the range partitioned
226       table measurement:
227
228           CREATE FOREIGN TABLE measurement_y2016m07
229               PARTITION OF measurement FOR VALUES FROM ('2016-07-01') TO ('2016-08-01')
230               SERVER server_07;
231

COMPATIBILITY

233       The CREATE FOREIGN TABLE command largely conforms to the SQL standard;
234       however, much as with CREATE TABLE, NULL constraints and zero-column
235       foreign tables are permitted. The ability to specify column default
236       values is also a PostgreSQL extension. Table inheritance, in the form
237       defined by PostgreSQL, is nonstandard.
238

SEE ALSO

240       ALTER FOREIGN TABLE (ALTER_FOREIGN_TABLE(7)), DROP FOREIGN TABLE
241       (DROP_FOREIGN_TABLE(7)), CREATE TABLE (CREATE_TABLE(7)), CREATE SERVER
242       (CREATE_SERVER(7)), IMPORT FOREIGN SCHEMA (IMPORT_FOREIGN_SCHEMA(7))
243
244
245
246PostgreSQL 13.4                      2021              CREATE FOREIGN TABLE(7)
Impressum