CREATE_FOREIGN_TABLE(7pg13)
π CREATE_FOREIGN_TABLE(7pg13) π
okay bestie, welcome to the PostgreSQL 13.23 Documentation page for CREATE FOREIGN TABLE β manual name CREATE_FOREIGN_TABLE(7pg13) β and it is literally here to define a new foreign table. main character energy fr fr β¨
πΈ NAME πΈ
CREATE_FOREIGN_TABLE β define a new foreign table. thatβs the whole vibe, no cap π
π¦ 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 π
so CREATE FOREIGN TABLE creates a new foreign table in the current database, and the table is owned by the user issuing the command. periodt. π
if a schema name is given β like CREATE FOREIGN TABLE myschema.mytable ... β then the table gets created in that specified schema. otherwise itβs created in the current schema. also the name of the foreign table has to be distinct from the name of any other foreign table, table, sequence, index, view, or materialized view in the same schema. like, unique name or itβs a no. π«Ά
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. lowkey iconic how it does that automatically. β¨
if the PARTITION OF clause is specified, then the table is created as a partition of parent_table with specified bounds. slay.
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 privilege, no table, bestie. π
π 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. so like, donβt assume itβs the same table just because the name matches πtable_nameβ the name (optionally schema-qualified) of the table to be created. simple as that.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. bestie, Chapter 8 is the data type lore drop π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. itβs giving βIβll pick if you donβtβ energy.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 TABLE(CREATE_TABLE(7)) for more details. family tree but make it tables π³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 TABLE(CREATE_TABLE(7)) for 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.) so like, read the room before partitioning π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. name your constraints, bestie, itβs self-care πNOT NULLβ the column is not allowed to contain null values. no nulls allowed, periodt.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. so like, itβs giving legacy compatibility, not fresh new code vibes π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. sotableoidis the one allowed system-column guest at the party π«Ά - a constraint marked with
NO INHERITwill not propagate to child tables. it stays in its lane, literally.
- currently,
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 no default means null walks in like βhey bestieβ π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. so like, generated but not a free-for-all β rules are rules, bestie β¨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)). server selection is giving βpick your favβ energy π¦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). so duplicate names? not slay. table option and column option sharing a name? thatβs allowed, iykyk π
okay bestie, thatβs the whole create syntax and every parameter β the notes and examples are next, and thereβs some important caution energy coming, so stay tuned π
π COMPATIBILITY π
Okay so vibe check on standards: the CREATE FOREIGN TABLE command largely conforms to the SQL standard. Slay. β¨
But bestie, there are a few Iconic PostgreSQL Choicesβ’ to know about:
- Much as with
CREATE TABLE, NULL constraints and zero-column foreign tables are permitted. Yes, zero columns. It's giving minimalist. π - The ability to specify column default values is also a PostgreSQL extension. Very "we added a little extra sparkle" of it. π
- Table inheritance, in the form defined by PostgreSQL, is nonstandard. Not standard-coded, but it's still main character energy. π
π SEE ALSO π
If you're doing a little link-hopping era, these are the besties to check next:
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. This little docs glow-up comes from PostgreSQL 13.23, dated 2025, and it is absolutely the CREATE FOREIGN TABLE(7) manual page. Go forth and foreign-table responsibly π«Άπβ¨
π¦ where this page comes from
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-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