CREATE_FOREIGN_TABLE(7pg16)
π CREATE_FOREIGN_TABLE π
okay bestie, this is the PostgreSQL 16.15 documentation page for CREATE_FOREIGN_TABLE, and itβs literally the blueprint for defining a new foreign table. slay. no cap, every detail is staying right here. π«Ά
β¨ NAME β¨
CREATE_FOREIGN_TABLE β define a new foreign table π
πΈ SYNOPSIS πΈ
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' [, ... ] ) ]
where 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 )
π¦ DESCRIPTION π¦
okay so CREATE FOREIGN TABLE creates a new foreign table in the current database. The table will be owned by the user issuing the command. main character energy only.
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. 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 collisions bestie.
CREATE FOREIGN TABLE 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. yep, itβs giving package deal. π
If PARTITION OF clause is specified then the table is created as a partition of parent_table with specified bounds.
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. π
π 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. Note that there is no guarantee that the existing relation is anything like the one that would have been created. π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.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.PARTITION OF parent_table { FOR VALUES partition_bound_spec | DEFAULT }β 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 ofCREATE TABLEfor more details. 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.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. ngl, itβs a legacy vibe.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,CHECKexpressions cannot contain subqueries nor refer to variables other than columns of the current row. The system columntableoidmay be referenced, but not any other system column. A constraint marked withNO INHERITwill not propagate to child tables.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.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. The keywordSTOREDis 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. β¨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).
β¨ COMPATIBILITY β¨
Okay so compatibility check, bestie: CREATE FOREIGN TABLE largely conforms to the SQL standard β iconic behavior, honestly. But there are a few little βwe do it our wayβ moments, and theyβre not secret: much like with CREATE TABLE, NULL constraints and zero-column foreign tables are permitted. Yes, you read that right β zero columns can be a whole vibe here. π«Ά
Also, being able to specify column default values? Thatβs a PostgreSQL extension, so itβs a little extra sparkle beyond the standard, not baseline SQL energy. And table inheritance, in the form defined by PostgreSQL, is nonstandard β so donβt assume every database is gonna understand that family-tree situation. π
π SEE ALSO π
Your next-click rabbit holes, all lined up:
ALTER FOREIGN TABLE(ALTER_FOREIGN_TABLE(7))DROP FOREIGN TABLE(DROP_FOREIGN_TABLE(7))CREATE TABLE(CREATE_TABLE(7))CREATE SERVER(CREATE_SERVER(7))IMPORT FOREIGN SCHEMA(IMPORT_FOREIGN_SCHEMA(7))
Thatβs the whole page, bestie: source is PostgreSQL 16.15, date is 2026, and the manual page is literally CREATE FOREIGN TABLE(7). slay, go build your foreign tables πβ¨π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-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