CREATE FOREIGN TABLE

CREATE FOREIGN TABLE — define a new foreign table

Synopsis

  1. CREATE FOREIGN TABLE [ IF NOT EXISTS ]
  2. table_name
  3. ( [
  4. {
  5. column_name
  6. data_type
  7. [ OPTIONS (
  8. option
  9. '
  10. value
  11. ' [, ... ] ) ] [ COLLATE
  12. collation
  13. ] [
  14. column_constraint
  15. [ ... ] ]
  16. |
  17. table_constraint
  18. }
  19. [, ... ]
  20. ] )
  21. [ INHERITS (
  22. parent_table
  23. [, ... ] ) ]
  24. SERVER
  25. server_name
  26. [ OPTIONS (
  27. option
  28. '
  29. value
  30. ' [, ... ] ) ]
  31. CREATE FOREIGN TABLE [ IF NOT EXISTS ]
  32. table_name
  33. PARTITION OF
  34. parent_table
  35. [ (
  36. {
  37. column_name
  38. [ WITH OPTIONS ] [
  39. column_constraint
  40. [ ... ] ]
  41. |
  42. table_constraint
  43. }
  44. [, ... ]
  45. ) ]
  46. partition_bound_spec
  47. SERVER
  48. server_name
  49. [ OPTIONS (
  50. option
  51. '
  52. value
  53. ' [, ... ] ) ]
  54. where
  55. column_constraint
  56. is:
  57. [ CONSTRAINT
  58. constraint_name
  59. ]
  60. { NOT NULL |
  61. NULL |
  62. CHECK (
  63. expression
  64. ) [ NO INHERIT ] |
  65. DEFAULT
  66. default_expr
  67. }
  68. and
  69. table_constraint
  70. is:
  71. [ CONSTRAINT
  72. constraint_name
  73. ]
  74. CHECK (
  75. expression
  76. ) [ NO INHERIT ]

Description

CREATE FOREIGN TABLEcreates a new foreign table in the current database. The table will be owned by the user issuing the command.

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 foreign table, table, sequence, index, view, or materialized view in the same schema.

CREATE FOREIGN TABLEalso 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.

IfPARTITION OFclause is specified then the table is created as a partition ofparent_tablewith specified bounds.

To be able to create a foreign table, you must haveUSAGEprivilege on the foreign server, as well asUSAGEprivilege 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 byPostgreSQL, refer toChapter 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.

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.

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 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,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.

server_name

The name of an existing foreign server to use for the foreign table. For details on defining a server, seeCREATE SERVER.

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).

Notes

Constraints on foreign tables (such asCHECKorNOT NULLclauses) are not enforced by the corePostgreSQLsystem, 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.

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.

AlthoughPostgreSQLdoes 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 incorrect answers. It is the user’s responsibility to ensure that the constraint definition matches reality.

Examples

Create foreign tablefilms, which will be accessed through the serverfilm_server:

  1. CREATE FOREIGN TABLE films (
  2. code char(5) NOT NULL,
  3. title varchar(40) NOT NULL,
  4. did integer NOT NULL,
  5. date_prod date,
  6. kind varchar(10),
  7. len interval hour to minute
  8. )
  9. SERVER film_server;

Create foreign tablemeasurement_y2016m07, which will be accessed through the serverserver_07, as a partition of the range partitioned tablemeasurement:

  1. CREATE FOREIGN TABLE measurement_y2016m07
  2. PARTITION OF measurement FOR VALUES FROM ('2016-07-01') TO ('2016-08-01')
  3. SERVER server_07;

Compatibility

TheCREATE FOREIGN TABLEcommand largely conforms to theSQLstandard; however, much as withCREATE TABLE,NULLconstraints and zero-column foreign tables are permitted. The ability to specify column default values is also aPostgreSQLextension. Table inheritance, in the form defined byPostgreSQL, is nonstandard.

See Also

ALTER FOREIGN TABLE

,

DROP FOREIGN TABLE

,

CREATE TABLE

,

CREATE SERVER

,

IMPORT FOREIGN SCHEMA