{
    "content": [
        {
            "type": "text",
            "text": "# CREATE_FOREIGN_TABLE (man)\n\n## NAME\n\nCREATEFOREIGNTABLE - define a new foreign table\n\n## SYNOPSIS\n\nCREATE FOREIGN TABLE [ IF NOT EXISTS ] tablename ( [\n{ columnname datatype [ OPTIONS ( option 'value' [, ... ] ) ] [ COLLATE collation ] [ columnconstraint [ ... ] ]\n| tableconstraint }\n[, ... ]\n] )\n[ INHERITS ( parenttable [, ... ] ) ]\nSERVER servername\n[ OPTIONS ( option 'value' [, ... ] ) ]\nCREATE FOREIGN TABLE [ IF NOT EXISTS ] tablename\nPARTITION OF parenttable [ (\n{ columnname [ WITH OPTIONS ] [ columnconstraint [ ... ] ]\n| tableconstraint }\n[, ... ]\n) ]\n{ FOR VALUES partitionboundspec | DEFAULT }\nSERVER servername\n[ OPTIONS ( option 'value' [, ... ] ) ]\nwhere columnconstraint is:\n[ CONSTRAINT constraintname ]\n{ NOT NULL |\nNULL |\nCHECK ( expression ) [ NO INHERIT ] |\nDEFAULT defaultexpr |\nGENERATED ALWAYS AS ( generationexpr ) STORED }\nand tableconstraint is:\n[ CONSTRAINT constraintname ]\nCHECK ( expression ) [ NO INHERIT ]\nand partitionboundspec is:\nIN ( partitionboundexpr [, ...] ) |\nFROM ( { partitionboundexpr | MINVALUE | MAXVALUE } [, ...] )\nTO ( { partitionboundexpr | MINVALUE | MAXVALUE } [, ...] ) |\nWITH ( MODULUS numericliteral, REMAINDER numericliteral )\n\n## DESCRIPTION\n\nCREATE FOREIGN TABLE creates a new foreign table in the current database. The table will be\nowned by the user issuing the command.\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS**\n- **DESCRIPTION**\n- **PARAMETERS**\n- **NOTES**\n- **EXAMPLES**\n- **COMPATIBILITY**\n- **SEE ALSO**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "CREATE_FOREIGN_TABLE",
        "section": "",
        "mode": "man",
        "summary": "CREATEFOREIGNTABLE - define a new foreign table",
        "synopsis": "CREATE FOREIGN TABLE [ IF NOT EXISTS ] tablename ( [\n{ columnname datatype [ OPTIONS ( option 'value' [, ... ] ) ] [ COLLATE collation ] [ columnconstraint [ ... ] ]\n| tableconstraint }\n[, ... ]\n] )\n[ INHERITS ( parenttable [, ... ] ) ]\nSERVER servername\n[ OPTIONS ( option 'value' [, ... ] ) ]\nCREATE FOREIGN TABLE [ IF NOT EXISTS ] tablename\nPARTITION OF parenttable [ (\n{ columnname [ WITH OPTIONS ] [ columnconstraint [ ... ] ]\n| tableconstraint }\n[, ... ]\n) ]\n{ FOR VALUES partitionboundspec | DEFAULT }\nSERVER servername\n[ OPTIONS ( option 'value' [, ... ] ) ]\nwhere columnconstraint is:\n[ CONSTRAINT constraintname ]\n{ NOT NULL |\nNULL |\nCHECK ( expression ) [ NO INHERIT ] |\nDEFAULT defaultexpr |\nGENERATED ALWAYS AS ( generationexpr ) STORED }\nand tableconstraint is:\n[ CONSTRAINT constraintname ]\nCHECK ( expression ) [ NO INHERIT ]\nand partitionboundspec is:\nIN ( partitionboundexpr [, ...] ) |\nFROM ( { partitionboundexpr | MINVALUE | MAXVALUE } [, ...] )\nTO ( { partitionboundexpr | MINVALUE | MAXVALUE } [, ...] ) |\nWITH ( MODULUS numericliteral, REMAINDER numericliteral )",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [
            "Create foreign table films, which will be accessed through the server filmserver:",
            "CREATE FOREIGN TABLE films (",
            "code        char(5) NOT NULL,",
            "title       varchar(40) NOT NULL,",
            "did         integer NOT NULL,",
            "dateprod   date,",
            "kind        varchar(10),",
            "len         interval hour to minute",
            "SERVER filmserver;",
            "Create foreign table measurementy2016m07, which will be accessed through the server",
            "server07, as a partition of the range partitioned table measurement:",
            "CREATE FOREIGN TABLE measurementy2016m07",
            "PARTITION OF measurement FOR VALUES FROM ('2016-07-01') TO ('2016-08-01')",
            "SERVER server07;"
        ],
        "see_also": [
            {
                "name": "ALTERFOREIGNTABLE",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/ALTERFOREIGNTABLE/7/json"
            },
            {
                "name": "DROPFOREIGNTABLE",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/DROPFOREIGNTABLE/7/json"
            },
            {
                "name": "CREATETABLE",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/CREATETABLE/7/json"
            },
            {
                "name": "CREATESERVER",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/CREATESERVER/7/json"
            },
            {
                "name": "IMPORTFOREIGNSCHEMA",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/IMPORTFOREIGNSCHEMA/7/json"
            },
            {
                "name": "TABLE",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/TABLE/7/json"
            }
        ],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 40,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 18,
                "subsections": []
            },
            {
                "name": "PARAMETERS",
                "lines": 91,
                "subsections": []
            },
            {
                "name": "NOTES",
                "lines": 38,
                "subsections": []
            },
            {
                "name": "EXAMPLES",
                "lines": 19,
                "subsections": []
            },
            {
                "name": "COMPATIBILITY",
                "lines": 5,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 7,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "CREATEFOREIGNTABLE - define a new foreign table\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "CREATE FOREIGN TABLE [ IF NOT EXISTS ] tablename ( [\n{ columnname datatype [ OPTIONS ( option 'value' [, ... ] ) ] [ COLLATE collation ] [ columnconstraint [ ... ] ]\n| tableconstraint }\n[, ... ]\n] )\n[ INHERITS ( parenttable [, ... ] ) ]\nSERVER servername\n[ OPTIONS ( option 'value' [, ... ] ) ]\n\nCREATE FOREIGN TABLE [ IF NOT EXISTS ] tablename\nPARTITION OF parenttable [ (\n{ columnname [ WITH OPTIONS ] [ columnconstraint [ ... ] ]\n| tableconstraint }\n[, ... ]\n) ]\n{ FOR VALUES partitionboundspec | DEFAULT }\nSERVER servername\n[ OPTIONS ( option 'value' [, ... ] ) ]\n\nwhere columnconstraint is:\n\n[ CONSTRAINT constraintname ]\n{ NOT NULL |\nNULL |\nCHECK ( expression ) [ NO INHERIT ] |\nDEFAULT defaultexpr |\nGENERATED ALWAYS AS ( generationexpr ) STORED }\n\nand tableconstraint is:\n\n[ CONSTRAINT constraintname ]\nCHECK ( expression ) [ NO INHERIT ]\n\nand partitionboundspec is:\n\nIN ( partitionboundexpr [, ...] ) |\nFROM ( { partitionboundexpr | MINVALUE | MAXVALUE } [, ...] )\nTO ( { partitionboundexpr | MINVALUE | MAXVALUE } [, ...] ) |\nWITH ( MODULUS numericliteral, REMAINDER numericliteral )\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "CREATE FOREIGN TABLE creates a new foreign table in the current database. The table will be\nowned by the user issuing the command.\n\nIf a schema name is given (for example, CREATE FOREIGN TABLE myschema.mytable ...) then the\ntable is created in the specified schema. Otherwise it is created in the current schema. The\nname of the foreign table must be distinct from the name of any other foreign table, table,\nsequence, index, view, or materialized view in the same schema.\n\nCREATE FOREIGN TABLE also automatically creates a data type that represents the composite\ntype corresponding to one row of the foreign table. Therefore, foreign tables cannot have the\nsame name as any existing data type in the same schema.\n\nIf PARTITION OF clause is specified then the table is created as a partition of parenttable\nwith specified bounds.\n\nTo be able to create a foreign table, you must have USAGE privilege on the foreign server, as\nwell as USAGE privilege on all column types used in the table.\n",
                "subsections": []
            },
            "PARAMETERS": {
                "content": "IF NOT EXISTS\nDo not throw an error if a relation with the same name already exists. A notice is issued\nin this case. Note that there is no guarantee that the existing relation is anything like\nthe one that would have been created.\n\ntablename\nThe name (optionally schema-qualified) of the table to be created.\n\ncolumnname\nThe name of a column to be created in the new table.\n\ndatatype\nThe data type of the column. This can include array specifiers. For more information on\nthe data types supported by PostgreSQL, refer to Chapter 8.\n\nCOLLATE collation\nThe COLLATE clause assigns a collation to the column (which must be of a collatable data\ntype). If not specified, the column data type's default collation is used.\n\nINHERITS ( parenttable [, ... ] )\nThe optional INHERITS clause specifies a list of tables from which the new foreign table\nautomatically inherits all columns. Parent tables can be plain tables or foreign tables.\nSee the similar form of CREATE TABLE for more details.\n\nPARTITION OF parenttable { FOR VALUES partitionboundspec | DEFAULT }\nThis form can be used to create the foreign table as partition of the given parent table\nwith specified partition bound values. See the similar form of CREATE TABLE for more\ndetails. Note that it is currently not allowed to create the foreign table as a partition\nof the parent table if there are UNIQUE indexes on the parent table. (See also ALTER\nTABLE ATTACH PARTITION.)\n\nCONSTRAINT constraintname\nAn optional name for a column or table constraint. If the constraint is violated, the\nconstraint name is present in error messages, so constraint names like col must be\npositive can be used to communicate helpful constraint information to client\napplications. (Double-quotes are needed to specify constraint names that contain spaces.)\nIf a constraint name is not specified, the system generates a name.\n\nNOT NULL\nThe column is not allowed to contain null values.\n\nNULL\nThe column is allowed to contain null values. This is the default.\n\nThis clause is only provided for compatibility with non-standard SQL databases. Its use\nis discouraged in new applications.\n\nCHECK ( expression ) [ NO INHERIT ]\nThe CHECK clause specifies an expression producing a Boolean result which each row in the\nforeign table is expected to satisfy; that is, the expression should produce TRUE or\nUNKNOWN, never FALSE, for all rows in the foreign table. A check constraint specified as\na column constraint should reference that column's value only, while an expression\nappearing in a table constraint can reference multiple columns.\n\nCurrently, CHECK expressions cannot contain subqueries nor refer to variables other than\ncolumns of the current row. The system column tableoid may be referenced, but not any\nother system column.\n\nA constraint marked with NO INHERIT will not propagate to child tables.\n\nDEFAULT defaultexpr\nThe DEFAULT clause assigns a default data value for the column whose column definition it\nappears within. The value is any variable-free expression (subqueries and\ncross-references to other columns in the current table are not allowed). The data type of\nthe default expression must match the data type of the column.\n\nThe default expression will be used in any insert operation that does not specify a value\nfor the column. If there is no default for a column, then the default is null.\n\nGENERATED ALWAYS AS ( generationexpr ) STORED\nThis clause creates the column as a generated column. The column cannot be written to,\nand when read the result of the specified expression will be returned.\n\nThe keyword STORED is required to signify that the column will be computed on write. (The\ncomputed value will be presented to the foreign-data wrapper for storage and must be\nreturned on reading.)\n\nThe generation expression can refer to other columns in the table, but not other\ngenerated columns. Any functions and operators used must be immutable. References to\nother tables are not allowed.\n\nservername\nThe name of an existing foreign server to use for the foreign table. For details on\ndefining a server, see CREATE SERVER (CREATESERVER(7)).\n\nOPTIONS ( option 'value' [, ...] )\nOptions to be associated with the new foreign table or one of its columns. The allowed\noption names and values are specific to each foreign data wrapper and are validated using\nthe foreign-data wrapper's validator function. Duplicate option names are not allowed\n(although it's OK for a table option and a column option to have the same name).\n",
                "subsections": []
            },
            "NOTES": {
                "content": "Constraints on foreign tables (such as CHECK or NOT NULL clauses) are not enforced by the\ncore PostgreSQL system, and most foreign data wrappers do not attempt to enforce them either;\nthat is, the constraint is simply assumed to hold true. There would be little point in such\nenforcement since it would only apply to rows inserted or updated via the foreign table, and\nnot to rows modified by other means, such as directly on the remote server. Instead, a\nconstraint attached to a foreign table should represent a constraint that is being enforced\nby the remote server.\n\nSome special-purpose foreign data wrappers might be the only access mechanism for the data\nthey access, and in that case it might be appropriate for the foreign data wrapper itself to\nperform constraint enforcement. But you should not assume that a wrapper does that unless its\ndocumentation says so.\n\nAlthough PostgreSQL does not attempt to enforce constraints on foreign tables, it does assume\nthat they are correct for purposes of query optimization. If there are rows visible in the\nforeign table that do not satisfy a declared constraint, queries on the table might produce\nerrors or incorrect answers. It is the user's responsibility to ensure that the constraint\ndefinition matches reality.\n\nCaution\nWhen a foreign table is used as a partition of a partitioned table, there is an implicit\nconstraint that its contents must satisfy the partitioning rule. Again, it is the user's\nresponsibility to ensure that that is true, which is best done by installing a matching\nconstraint on the remote server.\n\nWithin a partitioned table containing foreign-table partitions, an UPDATE that changes the\npartition key value can cause a row to be moved from a local partition to a foreign-table\npartition, provided the foreign data wrapper supports tuple routing. However, it is not\ncurrently possible to move a row from a foreign-table partition to another partition. An\nUPDATE that would require doing that will fail due to the partitioning constraint, assuming\nthat that is properly enforced by the remote server.\n\nSimilar considerations apply to generated columns. Stored generated columns are computed on\ninsert or update on the local PostgreSQL server and handed to the foreign-data wrapper for\nwriting out to the foreign data store, but it is not enforced that a query of the foreign\ntable returns values for stored generated columns that are consistent with the generation\nexpression. Again, this might result in incorrect query results.\n",
                "subsections": []
            },
            "EXAMPLES": {
                "content": "Create foreign table films, which will be accessed through the server filmserver:\n\nCREATE FOREIGN TABLE films (\ncode        char(5) NOT NULL,\ntitle       varchar(40) NOT NULL,\ndid         integer NOT NULL,\ndateprod   date,\nkind        varchar(10),\nlen         interval hour to minute\n)\nSERVER filmserver;\n\nCreate foreign table measurementy2016m07, which will be accessed through the server\nserver07, as a partition of the range partitioned table measurement:\n\nCREATE FOREIGN TABLE measurementy2016m07\nPARTITION OF measurement FOR VALUES FROM ('2016-07-01') TO ('2016-08-01')\nSERVER server07;\n",
                "subsections": []
            },
            "COMPATIBILITY": {
                "content": "The CREATE FOREIGN TABLE command largely conforms to the SQL standard; however, much as with\nCREATE TABLE, NULL constraints and zero-column foreign tables are permitted. The ability to\nspecify column default values is also a PostgreSQL extension. Table inheritance, in the form\ndefined by PostgreSQL, is nonstandard.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "ALTER FOREIGN TABLE (ALTERFOREIGNTABLE(7)), DROP FOREIGN TABLE (DROPFOREIGNTABLE(7)),\nCREATE TABLE (CREATETABLE(7)), CREATE SERVER (CREATESERVER(7)), IMPORT FOREIGN SCHEMA\n(IMPORTFOREIGNSCHEMA(7))\n\n\n\nPostgreSQL 14.23                                2026                         CREATE FOREIGN TABLE(7)",
                "subsections": []
            }
        }
    }
}