CREATE_FOREIGN_TABLE(7pg17)
πΈ NAME πΈ
okay so this is the CREATE_FOREIGN_TABLE(7pg17) page from the PostgreSQL 17.11 Documentation, and it's literally all about how to define a new foreign table. that's it. that's the vibe. π
β¨ SYNOPSIS β¨
bestie, here's the whole shape of it, exactly as written:
CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name ( [
{ column_name data_type [ OPTIONS ( option 'value' [, ... ] ) ] [ COLLATE collation ] [ column_constraint [ ... ] ]
| table_constraint }
[, ... ]
] )
[ INHERITS ( parent_table [, ... ] ) ]
SERVER server_name
[ OPTIONS ( option 'value' [, ... ] ) ]
CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name
PARTITION OF parent_table [ (
{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]
| table_constraint }
[, ... ]
) ]
{ FOR VALUES partition_bound_spec | DEFAULT }
SERVER server_name
[ OPTIONS ( option 'value' [, ... ] ) ]
and column_constraint is:
[ CONSTRAINT constraint_name ]
{ NOT NULL |
NULL |
CHECK ( expression ) [ NO INHERIT ] |
DEFAULT default_expr |
GENERATED ALWAYS AS ( generation_expr ) STORED }
and table_constraint is:
[ CONSTRAINT constraint_name ]
CHECK ( expression ) [ NO INHERIT ]
and partition_bound_spec is:
IN ( partition_bound_expr [, ...] ) |
FROM ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] )
TO ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] ) |
WITH ( MODULUS numeric_literal, REMAINDER numeric_literal )
no cap, that's the whole grammar. π«Ά
π¦ DESCRIPTION π¦
CREATE FOREIGN TABLE creates a new foreign table in the current database. and the table will be owned by the user issuing the command. main character energy, honestly π
if a schema name is given (for example, CREATE FOREIGN TABLE myschema.mytable ...) then the table is created in the specified schema. otherwise it is created in the current schema. and 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. like, no name-stealing bestie, that's not cute. π
CREATE FOREIGN TABLE also automatically creates a data type that represents the composite type corresponding to one row of the foreign table. so therefore, foreign tables cannot have the same name as any existing data type in the same schema. it's giving "you can't sit with us" and honestly? valid.
if the PARTITION OF clause is specified, then the table is created as a partition of parent_table with the specified bounds. slay.
and to be able to create a foreign table, you must have USAGE privilege on the foreign server, as well as USAGE privilege on all column types used in the table. no privileges, no table. periodt. β¨
π PARAMETERS π
IF NOT EXISTSβ do not throw an error if a relation with the same name already exists. a notice is issued in this case. but bestie, note that there is no guarantee that the existing relation is anything like the one that would have been created. it's giving "we don't know her, but she's here." πtable_nameβ the name (optionally schema-qualified) of the table to be created.column_nameβ the name of a column to be created in the new table.data_typeβ the data type of the column. this can include array specifiers. for more information on the data types supported by PostgreSQL, refer to Chapter 8.COLLATE collationβ theCOLLATEclause assigns a collation to the column (which must be of a collatable data type). if not specified, the column data type's default collation is used. default queen behavior πINHERITS ( parent_table [, ... ] )β the optionalINHERITSclause 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 ofCREATE TABLEfor more details. it's giving family tree, but make it SQL. πPARTITION OF parent_table { FOR VALUES partition_bound_spec | DEFAULT }β this form can be used to create the foreign table as a partition of the given parent table with specified partition bound values. see the similar form ofCREATE TABLEfor more details. but β οΈ note that it is currently not allowed to create the foreign table as a partition of the parent table if there areUNIQUEindexes on the parent table. (see alsoALTER TABLE ATTACH PARTITION.)CONSTRAINT constraint_nameβ 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 likecol must be positivecan 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. cute of it honestly πNOT NULLβ the column is not allowed to contain null values.NULLβ the column is allowed to contain null values. this is the default.
this clause is only provided for compatibility with non-standard SQL databases. its use is discouraged in new applications. like, it's giving legacy energy. we don't stan. π ββοΈ
CHECK ( expression ) [ NO INHERIT ]β theCHECKclause specifies an expression producing a Boolean result which each row in the foreign table is expected to satisfy; that is, the expression should produceTRUEorUNKNOWN, neverFALSE, for all rows in the foreign table. a check constraint specified as a column constraint should reference that column's value only, while an expression appearing in a table constraint can reference multiple columns.
currently, CHECK expressions cannot contain subqueries nor refer to variables other than columns of the current row. the system column tableoid may be referenced, but not any other system column.
a constraint marked with NO INHERIT will not propagate to child tables. no hand-me-downs, bestie. β
DEFAULT default_exprβ theDEFAULTclause 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.
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. so basically, if you don't show up with a value, you get null. relatable π
GENERATED ALWAYS AS ( generation_expr ) STOREDβ this clause creates the column as a generated column. the column cannot be written to, and when read the result of the specified expression will be returned. she's calculated, she's iconic, she doesn't take edits. π
the keyword STORED is required to signify that 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.)
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. no cap, those are the rules. π₯
server_nameβ the name of an existing foreign server to use for the foreign table. for details on defining a server, seeCREATE SERVER(CREATE_SERVER(7)).OPTIONS ( option 'value' [, ...] )β 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's validator function. duplicate option names are not allowed (although it's OK for a table option and a column option to have the same name). sharing is caring, but only across table and column scopes, apparently. π
okay bestie, that's the whole parameter list and we're not even done β the NOTES section is next and it's lowkey the most important part, so stay with me π
π COMPATIBILITY π
Okay so compatibility check, bestie: CREATE FOREIGN TABLE largely conforms to the SQL standard. Slay. β¨
But much like with CREATE TABLE, there are a couple of little quirks: NULL constraints and zero-column foreign tables are permitted. Also, the ability to specify column default values is a PostgreSQL extension, and table inheritance, in the form defined by PostgreSQL, is nonstandard. No cap, that's just how it is. π
π SEE ALSO π
ALTER FOREIGN TABLE(ALTER_FOREIGN_TABLE(7)), for when you need to change things up πDROP FOREIGN TABLE(DROP_FOREIGN_TABLE(7)), for the dramatic goodbye πCREATE TABLE(CREATE_TABLE(7)), the classic icon πCREATE SERVER(CREATE_SERVER(7)), server creation main character energy π«ΆIMPORT FOREIGN SCHEMA(IMPORT_FOREIGN_SCHEMA(7)), for bringing the whole schema over β¨
That's the whole page, bestie: PostgreSQL 17.11, dated 2026, CREATE FOREIGN TABLE(7). Go forth and foreign-table your heart out πππ¦
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms