1CREATE FOREIGN TABLE(7)  PostgreSQL 11.6 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
35       and table_constraint is:
36
37       [ CONSTRAINT constraint_name ]
38       CHECK ( expression ) [ NO INHERIT ]
39

DESCRIPTION

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

PARAMETERS

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

NOTES

160       Constraints on foreign tables (such as CHECK or NOT NULL clauses) are
161       not enforced by the core PostgreSQL system, and most foreign data
162       wrappers do not attempt to enforce them either; that is, the constraint
163       is simply assumed to hold true. There would be little point in such
164       enforcement since it would only apply to rows inserted or updated via
165       the foreign table, and not to rows modified by other means, such as
166       directly on the remote server. Instead, a constraint attached to a
167       foreign table should represent a constraint that is being enforced by
168       the remote server.
169
170       Some special-purpose foreign data wrappers might be the only access
171       mechanism for the data they access, and in that case it might be
172       appropriate for the foreign data wrapper itself to perform constraint
173       enforcement. But you should not assume that a wrapper does that unless
174       its documentation says so.
175
176       Although PostgreSQL does not attempt to enforce constraints on foreign
177       tables, it does assume that they are correct for purposes of query
178       optimization. If there are rows visible in the foreign table that do
179       not satisfy a declared constraint, queries on the table might produce
180       incorrect answers. It is the user's responsibility to ensure that the
181       constraint definition matches reality.
182
183       While rows can be moved from local partitions to a foreign-table
184       partition (provided the foreign data wrapper supports tuple routing),
185       they cannot be moved from a foreign-table partition to another
186       partition.
187

EXAMPLES

189       Create foreign table films, which will be accessed through the server
190       film_server:
191
192           CREATE FOREIGN TABLE films (
193               code        char(5) NOT NULL,
194               title       varchar(40) NOT NULL,
195               did         integer NOT NULL,
196               date_prod   date,
197               kind        varchar(10),
198               len         interval hour to minute
199           )
200           SERVER film_server;
201
202       Create foreign table measurement_y2016m07, which will be accessed
203       through the server server_07, as a partition of the range partitioned
204       table measurement:
205
206           CREATE FOREIGN TABLE measurement_y2016m07
207               PARTITION OF measurement FOR VALUES FROM ('2016-07-01') TO ('2016-08-01')
208               SERVER server_07;
209

COMPATIBILITY

211       The CREATE FOREIGN TABLE command largely conforms to the SQL standard;
212       however, much as with CREATE TABLE, NULL constraints and zero-column
213       foreign tables are permitted. The ability to specify column default
214       values is also a PostgreSQL extension. Table inheritance, in the form
215       defined by PostgreSQL, is nonstandard.
216

SEE ALSO

218       ALTER FOREIGN TABLE (ALTER_FOREIGN_TABLE(7)), DROP FOREIGN TABLE
219       (DROP_FOREIGN_TABLE(7)), CREATE TABLE (CREATE_TABLE(7)), CREATE SERVER
220       (CREATE_SERVER(7)), IMPORT FOREIGN SCHEMA (IMPORT_FOREIGN_SCHEMA(7))
221
222
223
224PostgreSQL 11.6                      2019              CREATE FOREIGN TABLE(7)
Impressum