1CREATE FOREIGN TABLE(7) PostgreSQL 14.3 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 GENERATED ALWAYS AS ( generation_expr ) STORED }
35
36 and table_constraint is:
37
38 [ CONSTRAINT constraint_name ]
39 CHECK ( expression ) [ NO INHERIT ]
40
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
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 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 for more details. Note that it is
97 currently not allowed to create the foreign table as a partition of
98 the parent table if there are UNIQUE indexes on the parent table.
99 (See also ALTER TABLE ATTACH 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 GENERATED ALWAYS AS ( generation_expr ) STORED
147 This clause creates the column as a generated column. The column
148 cannot be written to, and when read the result of the specified
149 expression will be returned.
150
151 The keyword STORED is required to signify that the column will be
152 computed on write. (The computed value will be presented to the
153 foreign-data wrapper for storage and must be returned on reading.)
154
155 The generation expression can refer to other columns in the table,
156 but not other generated columns. Any functions and operators used
157 must be immutable. References to other tables are not allowed.
158
159 server_name
160 The name of an existing foreign server to use for the foreign
161 table. For details on defining a server, see CREATE SERVER
162 (CREATE_SERVER(7)).
163
164 OPTIONS ( option 'value' [, ...] )
165 Options to be associated with the new foreign table or one of its
166 columns. The allowed option names and values are specific to each
167 foreign data wrapper and are validated using the foreign-data
168 wrapper's validator function. Duplicate option names are not
169 allowed (although it's OK for a table option and a column option to
170 have the same name).
171
173 Constraints on foreign tables (such as CHECK or NOT NULL clauses) are
174 not enforced by the core PostgreSQL system, and most foreign data
175 wrappers do not attempt to enforce them either; that is, the constraint
176 is simply assumed to hold true. There would be little point in such
177 enforcement since it would only apply to rows inserted or updated via
178 the foreign table, and not to rows modified by other means, such as
179 directly on the remote server. Instead, a constraint attached to a
180 foreign table should represent a constraint that is being enforced by
181 the remote server.
182
183 Some special-purpose foreign data wrappers might be the only access
184 mechanism for the data they access, and in that case it might be
185 appropriate for the foreign data wrapper itself to perform constraint
186 enforcement. But you should not assume that a wrapper does that unless
187 its documentation says so.
188
189 Although PostgreSQL does not attempt to enforce constraints on foreign
190 tables, it does assume that they are correct for purposes of query
191 optimization. If there are rows visible in the foreign table that do
192 not satisfy a declared constraint, queries on the table might produce
193 errors or incorrect answers. It is the user's responsibility to ensure
194 that the constraint definition matches reality.
195
196 Caution
197 When a foreign table is used as a partition of a partitioned table,
198 there is an implicit constraint that its contents must satisfy the
199 partitioning rule. Again, it is the user's responsibility to ensure
200 that that is true, which is best done by installing a matching
201 constraint on the remote server.
202
203 Within a partitioned table containing foreign-table partitions, an
204 UPDATE that changes the partition key value can cause a row to be moved
205 from a local partition to a foreign-table partition, provided the
206 foreign data wrapper supports tuple routing. However it is not
207 currently possible to move a row from a foreign-table partition to
208 another partition. An UPDATE that would require doing that will fail
209 due to the partitioning constraint, assuming that that is properly
210 enforced by the remote server.
211
212 Similar considerations apply to generated columns. Stored generated
213 columns are computed on insert or update on the local PostgreSQL server
214 and handed to the foreign-data wrapper for writing out to the foreign
215 data store, but it is not enforced that a query of the foreign table
216 returns values for stored generated columns that are consistent with
217 the generation expression. Again, this might result in incorrect query
218 results.
219
221 Create foreign table films, which will be accessed through the server
222 film_server:
223
224 CREATE FOREIGN TABLE films (
225 code char(5) NOT NULL,
226 title varchar(40) NOT NULL,
227 did integer NOT NULL,
228 date_prod date,
229 kind varchar(10),
230 len interval hour to minute
231 )
232 SERVER film_server;
233
234 Create foreign table measurement_y2016m07, which will be accessed
235 through the server server_07, as a partition of the range partitioned
236 table measurement:
237
238 CREATE FOREIGN TABLE measurement_y2016m07
239 PARTITION OF measurement FOR VALUES FROM ('2016-07-01') TO ('2016-08-01')
240 SERVER server_07;
241
243 The CREATE FOREIGN TABLE command largely conforms to the SQL standard;
244 however, much as with CREATE TABLE, NULL constraints and zero-column
245 foreign tables are permitted. The ability to specify column default
246 values is also a PostgreSQL extension. Table inheritance, in the form
247 defined by PostgreSQL, is nonstandard.
248
250 ALTER FOREIGN TABLE (ALTER_FOREIGN_TABLE(7)), DROP FOREIGN TABLE
251 (DROP_FOREIGN_TABLE(7)), CREATE TABLE (CREATE_TABLE(7)), CREATE SERVER
252 (CREATE_SERVER(7)), IMPORT FOREIGN SCHEMA (IMPORT_FOREIGN_SCHEMA(7))
253
254
255
256PostgreSQL 14.3 2022 CREATE FOREIGN TABLE(7)