]> begriffs open source - ai-pg/blob - full-docs/man7/CREATE_FOREIGN_TABLE.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / CREATE_FOREIGN_TABLE.7
1 '\" t
2 .\"     Title: CREATE FOREIGN TABLE
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "CREATE FOREIGN TABLE" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 CREATE_FOREIGN_TABLE \- define a new foreign table
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 CREATE FOREIGN TABLE [ IF NOT EXISTS ] \fItable_name\fR ( [
36   { \fIcolumn_name\fR \fIdata_type\fR [ OPTIONS ( \fIoption\fR \*(Aq\fIvalue\fR\*(Aq [, \&.\&.\&. ] ) ] [ COLLATE \fIcollation\fR ] [ \fIcolumn_constraint\fR [ \&.\&.\&. ] ]
37     | \fItable_constraint\fR
38     | LIKE \fIsource_table\fR [ \fIlike_option\fR \&.\&.\&. ] }
39     [, \&.\&.\&. ]
40 ] )
41 [ INHERITS ( \fIparent_table\fR [, \&.\&.\&. ] ) ]
42   SERVER \fIserver_name\fR
43 [ OPTIONS ( \fIoption\fR \*(Aq\fIvalue\fR\*(Aq [, \&.\&.\&. ] ) ]
44
45 CREATE FOREIGN TABLE [ IF NOT EXISTS ] \fItable_name\fR
46   PARTITION OF \fIparent_table\fR [ (
47   { \fIcolumn_name\fR [ WITH OPTIONS ] [ \fIcolumn_constraint\fR [ \&.\&.\&. ] ]
48     | \fItable_constraint\fR }
49     [, \&.\&.\&. ]
50 ) ]
51 { FOR VALUES \fIpartition_bound_spec\fR | DEFAULT }
52   SERVER \fIserver_name\fR
53 [ OPTIONS ( \fIoption\fR \*(Aq\fIvalue\fR\*(Aq [, \&.\&.\&. ] ) ]
54
55 where \fIcolumn_constraint\fR is:
56
57 [ CONSTRAINT \fIconstraint_name\fR ]
58 { NOT NULL [ NO INHERIT ] |
59   NULL |
60   CHECK ( \fIexpression\fR ) [ NO INHERIT ] |
61   DEFAULT \fIdefault_expr\fR |
62   GENERATED ALWAYS AS ( \fIgeneration_expr\fR ) [ STORED | VIRTUAL ] }
63 [ ENFORCED | NOT ENFORCED ]
64
65 and \fItable_constraint\fR is:
66
67 [ CONSTRAINT \fIconstraint_name\fR ]
68 {  NOT NULL \fIcolumn_name\fR [ NO INHERIT ] |
69    CHECK ( \fIexpression\fR ) [ NO INHERIT ] }
70 [ ENFORCED | NOT ENFORCED ]
71
72 and \fIlike_option\fR is:
73
74 { INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | GENERATED | STATISTICS | ALL }
75
76 and \fIpartition_bound_spec\fR is:
77
78 IN ( \fIpartition_bound_expr\fR [, \&.\&.\&.] ) |
79 FROM ( { \fIpartition_bound_expr\fR | MINVALUE | MAXVALUE } [, \&.\&.\&.] )
80   TO ( { \fIpartition_bound_expr\fR | MINVALUE | MAXVALUE } [, \&.\&.\&.] ) |
81 WITH ( MODULUS \fInumeric_literal\fR, REMAINDER \fInumeric_literal\fR )
82 .fi
83 .SH "DESCRIPTION"
84 .PP
85 \fBCREATE FOREIGN TABLE\fR
86 creates a new foreign table in the current database\&. The table will be owned by the user issuing the command\&.
87 .PP
88 If a schema name is given (for example,
89 CREATE FOREIGN TABLE myschema\&.mytable \&.\&.\&.) then the table is created in the specified schema\&. Otherwise it is created in the current schema\&. The name of the foreign table must be distinct from the name of any other relation (table, sequence, index, view, materialized view, or foreign table) in the same schema\&.
90 .PP
91 \fBCREATE FOREIGN TABLE\fR
92 also automatically creates a data type that represents the composite type corresponding to one row of the foreign table\&. Therefore, foreign tables cannot have the same name as any existing data type in the same schema\&.
93 .PP
94 If
95 PARTITION OF
96 clause is specified then the table is created as a partition of
97 parent_table
98 with specified bounds\&.
99 .PP
100 To be able to create a foreign table, you must have
101 USAGE
102 privilege on the foreign server, as well as
103 USAGE
104 privilege on all column types used in the table\&.
105 .SH "PARAMETERS"
106 .PP
107 IF NOT EXISTS
108 .RS 4
109 Do not throw an error if a relation with the same name already exists\&. A notice is issued in this case\&. Note that there is no guarantee that the existing relation is anything like the one that would have been created\&.
110 .RE
111 .PP
112 \fItable_name\fR
113 .RS 4
114 The name (optionally schema\-qualified) of the table to be created\&.
115 .RE
116 .PP
117 \fIcolumn_name\fR
118 .RS 4
119 The name of a column to be created in the new table\&.
120 .RE
121 .PP
122 \fIdata_type\fR
123 .RS 4
124 The data type of the column\&. This can include array specifiers\&. For more information on the data types supported by
125 PostgreSQL, refer to
126 Chapter\ \&8\&.
127 .RE
128 .PP
129 COLLATE \fIcollation\fR
130 .RS 4
131 The
132 COLLATE
133 clause assigns a collation to the column (which must be of a collatable data type)\&. If not specified, the column data type\*(Aqs default collation is used\&.
134 .RE
135 .PP
136 INHERITS ( \fIparent_table\fR [, \&.\&.\&. ] )
137 .RS 4
138 The optional
139 INHERITS
140 clause specifies a list of tables from which the new foreign table automatically inherits all columns\&. Parent tables can be plain tables or foreign tables\&. See the similar form of
141 \fBCREATE TABLE\fR
142 for more details\&.
143 .RE
144 .PP
145 PARTITION OF \fIparent_table\fR { FOR VALUES \fIpartition_bound_spec\fR | DEFAULT }
146 .RS 4
147 This form can be used to create the foreign table as partition of the given parent table with specified partition bound values\&. See the similar form of
148 \fBCREATE TABLE\fR
149 for more details\&. Note that it is currently not allowed to create the foreign table as a partition of the parent table if there are
150 UNIQUE
151 indexes on the parent table\&. (See also
152 \fBALTER TABLE ATTACH PARTITION\fR\&.)
153 .RE
154 .PP
155 LIKE \fIsource_table\fR [ \fIlike_option\fR \&.\&.\&. ]
156 .RS 4
157 The
158 LIKE
159 clause specifies a table from which the new table automatically copies all column names, their data types, and their not\-null constraints\&.
160 .sp
161 Unlike
162 INHERITS, the new table and original table are completely decoupled after creation is complete\&. Changes to the original table will not be applied to the new table, and it is not possible to include data of the new table in scans of the original table\&.
163 .sp
164 Also unlike
165 INHERITS, columns and constraints copied by
166 LIKE
167 are not merged with similarly named columns and constraints\&. If the same name is specified explicitly or in another
168 LIKE
169 clause, an error is signaled\&.
170 .sp
171 The optional
172 \fIlike_option\fR
173 clauses specify which additional properties of the original table to copy\&. Specifying
174 INCLUDING
175 copies the property, specifying
176 EXCLUDING
177 omits the property\&.
178 EXCLUDING
179 is the default\&. If multiple specifications are made for the same kind of object, the last one is used\&. The available options are:
180 .PP
181 INCLUDING COMMENTS
182 .RS 4
183 Comments for the copied columns and constraints will be copied\&. The default behavior is to exclude comments, resulting in the copied columns and constraints in the new table having no comments\&.
184 .RE
185 .PP
186 INCLUDING CONSTRAINTS
187 .RS 4
188 CHECK
189 constraints will be copied\&. No distinction is made between column constraints and table constraints\&. Not\-null constraints are always copied to the new table\&.
190 .RE
191 .PP
192 INCLUDING DEFAULTS
193 .RS 4
194 Default expressions for the copied column definitions will be copied\&. Otherwise, default expressions are not copied, resulting in the copied columns in the new table having null defaults\&. Note that copying defaults that call database\-modification functions, such as
195 \fBnextval\fR, may create a functional linkage between the original and new tables\&.
196 .RE
197 .PP
198 INCLUDING GENERATED
199 .RS 4
200 Any generation expressions of copied column definitions will be copied\&. By default, new columns will be regular base columns\&.
201 .RE
202 .PP
203 INCLUDING STATISTICS
204 .RS 4
205 Extended statistics are copied to the new table\&.
206 .RE
207 .PP
208 INCLUDING ALL
209 .RS 4
210 INCLUDING ALL
211 is an abbreviated form selecting all the available individual options\&. (It could be useful to write individual
212 EXCLUDING
213 clauses after
214 INCLUDING ALL
215 to select all but some specific options\&.)
216 .RE
217 .sp
218 .RE
219 .PP
220 CONSTRAINT \fIconstraint_name\fR
221 .RS 4
222 An optional name for a column or table constraint\&. If the constraint is violated, the constraint name is present in error messages, so constraint names like
223 col must be positive
224 can be used to communicate helpful constraint information to client applications\&. (Double\-quotes are needed to specify constraint names that contain spaces\&.) If a constraint name is not specified, the system generates a name\&.
225 .RE
226 .PP
227 NOT NULL [ NO INHERIT ]
228 .RS 4
229 The column is not allowed to contain null values\&.
230 .sp
231 A constraint marked with
232 NO INHERIT
233 will not propagate to child tables\&.
234 .RE
235 .PP
236 NULL
237 .RS 4
238 The column is allowed to contain null values\&. This is the default\&.
239 .sp
240 This clause is only provided for compatibility with non\-standard SQL databases\&. Its use is discouraged in new applications\&.
241 .RE
242 .PP
243 CHECK ( \fIexpression\fR ) [ NO INHERIT ]
244 .RS 4
245 The
246 CHECK
247 clause specifies an expression producing a Boolean result which each row in the foreign table is expected to satisfy; that is, the expression should produce TRUE or UNKNOWN, never FALSE, for all rows in the foreign table\&. A check constraint specified as a column constraint should reference that column\*(Aqs value only, while an expression appearing in a table constraint can reference multiple columns\&.
248 .sp
249 Currently,
250 CHECK
251 expressions cannot contain subqueries nor refer to variables other than columns of the current row\&. The system column
252 tableoid
253 may be referenced, but not any other system column\&.
254 .sp
255 A constraint marked with
256 NO INHERIT
257 will not propagate to child tables\&.
258 .RE
259 .PP
260 DEFAULT \fIdefault_expr\fR
261 .RS 4
262 The
263 DEFAULT
264 clause assigns a default data value for the column whose column definition it appears within\&. The value is any variable\-free expression (subqueries and cross\-references to other columns in the current table are not allowed)\&. The data type of the default expression must match the data type of the column\&.
265 .sp
266 The default expression will be used in any insert operation that does not specify a value for the column\&. If there is no default for a column, then the default is null\&.
267 .RE
268 .PP
269 GENERATED ALWAYS AS ( \fIgeneration_expr\fR ) [ STORED | VIRTUAL ]
270 .RS 4
271 This clause creates the column as a
272 generated column\&. The column cannot be written to, and when read the result of the specified expression will be returned\&.
273 .sp
274 When
275 VIRTUAL
276 is specified, the column will be computed when it is read\&. (The foreign\-data wrapper will see it as a null value in new rows and may choose to store it as a null value or ignore it altogether\&.) When
277 STORED
278 is specified, the column will be computed on write\&. (The computed value will be presented to the foreign\-data wrapper for storage and must be returned on reading\&.)
279 VIRTUAL
280 is the default\&.
281 .sp
282 The generation expression can refer to other columns in the table, but not other generated columns\&. Any functions and operators used must be immutable\&. References to other tables are not allowed\&.
283 .RE
284 .PP
285 \fIserver_name\fR
286 .RS 4
287 The name of an existing foreign server to use for the foreign table\&. For details on defining a server, see
288 CREATE SERVER (\fBCREATE_SERVER\fR(7))\&.
289 .RE
290 .PP
291 OPTIONS ( \fIoption\fR \*(Aq\fIvalue\fR\*(Aq [, \&.\&.\&.] )
292 .RS 4
293 Options to be associated with the new foreign table or one of its columns\&. The allowed option names and values are specific to each foreign data wrapper and are validated using the foreign\-data wrapper\*(Aqs validator function\&. Duplicate option names are not allowed (although it\*(Aqs OK for a table option and a column option to have the same name)\&.
294 .RE
295 .SH "NOTES"
296 .PP
297 Constraints on foreign tables (such as
298 CHECK
299 or
300 NOT NULL
301 clauses) are not enforced by the core
302 PostgreSQL
303 system, and most foreign data wrappers do not attempt to enforce them either; that is, the constraint is simply assumed to hold true\&. There would be little point in such enforcement since it would only apply to rows inserted or updated via the foreign table, and not to rows modified by other means, such as directly on the remote server\&. Instead, a constraint attached to a foreign table should represent a constraint that is being enforced by the remote server\&.
304 .PP
305 Some special\-purpose foreign data wrappers might be the only access mechanism for the data they access, and in that case it might be appropriate for the foreign data wrapper itself to perform constraint enforcement\&. But you should not assume that a wrapper does that unless its documentation says so\&.
306 .PP
307 Although
308 PostgreSQL
309 does not attempt to enforce constraints on foreign tables, it does assume that they are correct for purposes of query optimization\&. If there are rows visible in the foreign table that do not satisfy a declared constraint, queries on the table might produce errors or incorrect answers\&. It is the user\*(Aqs responsibility to ensure that the constraint definition matches reality\&.
310 .if n \{\
311 .sp
312 .\}
313 .RS 4
314 .it 1 an-trap
315 .nr an-no-space-flag 1
316 .nr an-break-flag 1
317 .br
318 .ps +1
319 \fBCaution\fR
320 .ps -1
321 .br
322 .PP
323 When a foreign table is used as a partition of a partitioned table, there is an implicit constraint that its contents must satisfy the partitioning rule\&. Again, it is the user\*(Aqs responsibility to ensure that that is true, which is best done by installing a matching constraint on the remote server\&.
324 .sp .5v
325 .RE
326 .PP
327 Within a partitioned table containing foreign\-table partitions, an
328 \fBUPDATE\fR
329 that changes the partition key value can cause a row to be moved from a local partition to a foreign\-table partition, provided the foreign data wrapper supports tuple routing\&. However, it is not currently possible to move a row from a foreign\-table partition to another partition\&. An
330 \fBUPDATE\fR
331 that would require doing that will fail due to the partitioning constraint, assuming that that is properly enforced by the remote server\&.
332 .PP
333 Similar considerations apply to generated columns\&. Stored generated columns are computed on insert or update on the local
334 PostgreSQL
335 server and handed to the foreign\-data wrapper for writing out to the foreign data store, but it is not enforced that a query of the foreign table returns values for stored generated columns that are consistent with the generation expression\&. Again, this might result in incorrect query results\&.
336 .SH "EXAMPLES"
337 .PP
338 Create foreign table
339 films, which will be accessed through the server
340 film_server:
341 .sp
342 .if n \{\
343 .RS 4
344 .\}
345 .nf
346 CREATE FOREIGN TABLE films (
347     code        char(5) NOT NULL,
348     title       varchar(40) NOT NULL,
349     did         integer NOT NULL,
350     date_prod   date,
351     kind        varchar(10),
352     len         interval hour to minute
353 )
354 SERVER film_server;
355 .fi
356 .if n \{\
357 .RE
358 .\}
359 .PP
360 Create foreign table
361 measurement_y2016m07, which will be accessed through the server
362 server_07, as a partition of the range partitioned table
363 measurement:
364 .sp
365 .if n \{\
366 .RS 4
367 .\}
368 .nf
369 CREATE FOREIGN TABLE measurement_y2016m07
370     PARTITION OF measurement FOR VALUES FROM (\*(Aq2016\-07\-01\*(Aq) TO (\*(Aq2016\-08\-01\*(Aq)
371     SERVER server_07;
372 .fi
373 .if n \{\
374 .RE
375 .\}
376 .SH "COMPATIBILITY"
377 .PP
378 The
379 \fBCREATE FOREIGN TABLE\fR
380 command largely conforms to the
381 SQL
382 standard; however, much as with
383 \fBCREATE TABLE\fR,
384 NULL
385 constraints and zero\-column foreign tables are permitted\&. The ability to specify column default values is also a
386 PostgreSQL
387 extension\&. Table inheritance, in the form defined by
388 PostgreSQL, is nonstandard\&. The
389 LIKE
390 clause, as supported in this command, is nonstandard\&.
391 .SH "SEE ALSO"
392 ALTER FOREIGN TABLE (\fBALTER_FOREIGN_TABLE\fR(7)), DROP FOREIGN TABLE (\fBDROP_FOREIGN_TABLE\fR(7)), CREATE TABLE (\fBCREATE_TABLE\fR(7)), CREATE SERVER (\fBCREATE_SERVER\fR(7)), IMPORT FOREIGN SCHEMA (\fBIMPORT_FOREIGN_SCHEMA\fR(7))