CREATE_FOREIGN_TABLE(7pg13)

πŸ“¦ version 13.23, in opensuse-leap 16.0

πŸ’– 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 β€” the COLLATE clause 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 optional INHERITS 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 CREATE 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 of CREATE 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 are UNIQUE indexes on the parent table. (see also ALTER 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 like col must be positive 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. 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 ] β€” the CHECK 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’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. so tableoid is the one allowed system-column guest at the party 🫢
    • a constraint marked with NO INHERIT will not propagate to child tables. it stays in its lane, literally.
  • DEFAULT default_expr β€” the DEFAULT 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. 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 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. 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, see CREATE 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:


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