1CREATE FOREIGN TABLE(7) PostgreSQL 10.7 Documentation CREATE FOREIGN TABLE(7)
2
3
4
6 CREATE_FOREIGN_TABLE - define a new foreign table
7
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
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
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 CONSTRAINT constraint_name
93 An optional name for a column or table constraint. If the
94 constraint is violated, the constraint name is present in error
95 messages, so constraint names like col must be positive can be used
96 to communicate helpful constraint information to client
97 applications. (Double-quotes are needed to specify constraint names
98 that contain spaces.) If a constraint name is not specified, the
99 system generates a name.
100
101 NOT NULL
102 The column is not allowed to contain null values.
103
104 NULL
105 The column is allowed to contain null values. This is the default.
106
107 This clause is only provided for compatibility with non-standard
108 SQL databases. Its use is discouraged in new applications.
109
110 CHECK ( expression ) [ NO INHERIT ]
111 The CHECK clause specifies an expression producing a Boolean result
112 which each row in the foreign table is expected to satisfy; that
113 is, the expression should produce TRUE or UNKNOWN, never FALSE, for
114 all rows in the foreign table. A check constraint specified as a
115 column constraint should reference that column's value only, while
116 an expression appearing in a table constraint can reference
117 multiple columns.
118
119 Currently, CHECK expressions cannot contain subqueries nor refer to
120 variables other than columns of the current row. The system column
121 tableoid may be referenced, but not any other system column.
122
123 A constraint marked with NO INHERIT will not propagate to child
124 tables.
125
126 DEFAULT default_expr
127 The DEFAULT clause assigns a default data value for the column
128 whose column definition it appears within. The value is any
129 variable-free expression (subqueries and cross-references to other
130 columns in the current table are not allowed). The data type of the
131 default expression must match the data type of the column.
132
133 The default expression will be used in any insert operation that
134 does not specify a value for the column. If there is no default for
135 a column, then the default is null.
136
137 server_name
138 The name of an existing foreign server to use for the foreign
139 table. For details on defining a server, see CREATE SERVER
140 (CREATE_SERVER(7)).
141
142 OPTIONS ( option 'value' [, ...] )
143 Options to be associated with the new foreign table or one of its
144 columns. The allowed option names and values are specific to each
145 foreign data wrapper and are validated using the foreign-data
146 wrapper's validator function. Duplicate option names are not
147 allowed (although it's OK for a table option and a column option to
148 have the same name).
149
151 Constraints on foreign tables (such as CHECK or NOT NULL clauses) are
152 not enforced by the core PostgreSQL system, and most foreign data
153 wrappers do not attempt to enforce them either; that is, the constraint
154 is simply assumed to hold true. There would be little point in such
155 enforcement since it would only apply to rows inserted or updated via
156 the foreign table, and not to rows modified by other means, such as
157 directly on the remote server. Instead, a constraint attached to a
158 foreign table should represent a constraint that is being enforced by
159 the remote server.
160
161 Some special-purpose foreign data wrappers might be the only access
162 mechanism for the data they access, and in that case it might be
163 appropriate for the foreign data wrapper itself to perform constraint
164 enforcement. But you should not assume that a wrapper does that unless
165 its documentation says so.
166
167 Although PostgreSQL does not attempt to enforce constraints on foreign
168 tables, it does assume that they are correct for purposes of query
169 optimization. If there are rows visible in the foreign table that do
170 not satisfy a declared constraint, queries on the table might produce
171 incorrect answers. It is the user's responsibility to ensure that the
172 constraint definition matches reality.
173
175 Create foreign table films, which will be accessed through the server
176 film_server:
177
178 CREATE FOREIGN TABLE films (
179 code char(5) NOT NULL,
180 title varchar(40) NOT NULL,
181 did integer NOT NULL,
182 date_prod date,
183 kind varchar(10),
184 len interval hour to minute
185 )
186 SERVER film_server;
187
188 Create foreign table measurement_y2016m07, which will be accessed
189 through the server server_07, as a partition of the range partitioned
190 table measurement:
191
192 CREATE FOREIGN TABLE measurement_y2016m07
193 PARTITION OF measurement FOR VALUES FROM ('2016-07-01') TO ('2016-08-01')
194 SERVER server_07;
195
197 The CREATE FOREIGN TABLE command largely conforms to the SQL standard;
198 however, much as with CREATE TABLE, NULL constraints and zero-column
199 foreign tables are permitted. The ability to specify column default
200 values is also a PostgreSQL extension. Table inheritance, in the form
201 defined by PostgreSQL, is nonstandard.
202
204 ALTER FOREIGN TABLE (ALTER_FOREIGN_TABLE(7)), DROP FOREIGN TABLE
205 (DROP_FOREIGN_TABLE(7)), CREATE TABLE (CREATE_TABLE(7)), CREATE SERVER
206 (CREATE_SERVER(7)), IMPORT FOREIGN SCHEMA (IMPORT_FOREIGN_SCHEMA(7))
207
208
209
210PostgreSQL 10.7 2019 CREATE FOREIGN TABLE(7)