IMPORT_FOREIGN_SCHEMA(7pg15)
π IMPORT FOREIGN SCHEMA π
okay so bestie, gather round β this is the IMPORT FOREIGN SCHEMA page from the PostgreSQL 15.19 Documentation, and it's genuinely one of the slayest little commands out there. it's giving "i don't have those tables, but i know a server who does." let's get into it π«Ά
β¨ NAME β¨
IMPORT_FOREIGN_SCHEMA β import table definitions from a foreign server. literally that's the whole vibe: you're not copying data, you're copying the shape of the tables. no cap. π
π SYNOPSIS π
IMPORT FOREIGN SCHEMA remote_schema
[ { LIMIT TO | EXCEPT } ( table_name [, ...] ) ]
FROM SERVER server_name
INTO local_schema
[ OPTIONS ( option 'value' [, ... ] ) ]
look at that structure. it's iconic. the LIMIT TO / EXCEPT bit is optional, the OPTIONS bit is optional, but FROM SERVER and INTO are the main character energy you cannot skip. π¦
πΈ DESCRIPTION πΈ
IMPORT FOREIGN SCHEMA creates foreign tables that represent tables already existing on a foreign server. think of it as a group-chat invite β the tables stay living their best life on the remote side, but now you've got a local handle to them. π₯
the new foreign tables will be owned by the user issuing the command, and they're created with the correct column definitions and options to match the remote tables. so it's not a lazy placeholder, it's a full fit-check against the real thing. π―
by default, all tables and views existing in a particular schema on the foreign server are imported. but optionally, the list of tables can be limited to a specified subset, or specific tables can be excluded. no crumbs left behind, unless you want them left behind.
the new foreign tables are all created in the target schema, which must already exist β so please, bestie, make the schema first. it will not do it for you, it's not your mom. π
π¨ permission check: to useIMPORT FOREIGN SCHEMA, the user must haveUSAGEprivilege on the foreign server, as well asCREATEprivilege on the target schema. both. not one. both. periodt.
β PARAMETERS β
remote_schemaβ the remote schema to import from. the specific meaning of a "remote schema" depends on the foreign data wrapper in use, so it's a little bit iykyk β check your FDW. π§LIMIT TO ( table_name [, ...] )β import only foreign tables matching one of the given table names. other tables existing in the foreign schema will be ignored. it's the "i'm only inviting these people" option. πEXCEPT ( table_name [, ...] )β exclude specified foreign tables from the import. all tables existing in the foreign schema will be imported except the ones listed here. it's the "everyone's invited but these two" option. πserver_nameβ the foreign server to import from. this is the source of truth, the one we're pulling from.local_schemaβ the schema in which the imported foreign tables will be created. remember: it has to exist already. πOPTIONS ( option 'value' [, ... ] )β options to be used during the import. the allowed option names and values are specific to each foreign data wrapper, so again β your FDW, your rules. π
π EXAMPLES π
import table definitions from a remote schema foreign_films on server film_server, creating the foreign tables in local schema films:
IMPORT FOREIGN SCHEMA foreign_films
FROM SERVER film_server INTO films;
as above, but import only the two tables actors and directors (if they exist):
IMPORT FOREIGN SCHEMA foreign_films LIMIT TO (actors, directors)
FROM SERVER film_server INTO films;
see how the LIMIT TO (...) slid right in there between the schema and the FROM SERVER? obsession. π
π¦ COMPATIBILITY π¦
the IMPORT FOREIGN SCHEMA command conforms to the SQL standard β except that the OPTIONS clause is a PostgreSQL extension. so it's standards-compliant with one little extra sparkle on top. β¨
π SEE ALSO π
CREATE FOREIGN TABLE(CREATE_FOREIGN_TABLE(7))CREATE SERVER(CREATE_SERVER(7))
go read those next, bestie, they're the prequels. π
and that's the whole page!! you ate that. go import some tables responsibly π
mwah π β this has been IMPORT FOREIGN SCHEMA(7) from PostgreSQL 15.19, dated 2026. see you in the next group chat! πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-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