IMPORT_FOREIGN_SCHEMA(7pg16)
π NAME π
okay so bestie, this is the IMPORT FOREIGN SCHEMA(7) page, straight out of the PostgreSQL 16.15 Documentation β¨ and the vibe is: IMPORT_FOREIGN_SCHEMA β importing table definitions from a foreign server π«Ά
πΈ SYNOPSIS πΈ
IMPORT FOREIGN SCHEMA remote_schema
[ { LIMIT TO | EXCEPT } ( table_name [, ...] ) ]
FROM SERVER server_name
INTO local_schema
[ OPTIONS ( option 'value' [, ... ] ) ]
π¦ DESCRIPTION π¦
So IMPORT FOREIGN SCHEMA creates foreign tables that represent tables that already exist on a foreign server. The new foreign tables get owned by the user issuing the command, and they're created with the correct column definitions and options so they match the remote tables. it's giving "mirror, mirror" π
By default, all tables and views existing in a particular schema on the foreign server get imported. But you can optionally limit the list of tables to a specified subset, or exclude specific tables. The new foreign tables are all created in the target schema, which must already exist β no auto-creating it for you, sorry bestie π
To use IMPORT FOREIGN SCHEMA, the user must have USAGE privilege on the foreign server, plus CREATE privilege on the target schema. both. no shortcuts π₯
β¨ PARAMETERS β¨
remote_schemaβ the remote schema you're importing from. what a "remote schema" actually means depends on the foreign data wrapper in use, so like... iykyk π§LIMIT TO ( table_name [, ...] )β import only the foreign tables matching one of the given table names. every other table hanging out in the foreign schema gets ignored. cherry-picking, but make it SQL πEXCEPT ( table_name [, ...] )β the opposite energy: exclude the specified foreign tables from the import. all tables in the foreign schema get imported except the ones you list here. not those ones, bestie πserver_nameβ the foreign server you're importing from. that's the one πlocal_schemaβ the schema where the imported foreign tables get created. it has to exist already, remember πOPTIONS ( option 'value' [, ...] )β options used during the import. the allowed option names and values are specific to each foreign data wrapper, so check with your wrapper, not with me π
π 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;
Same thing as above, but only import the two tables actors and directors (if they exist):
IMPORT FOREIGN SCHEMA foreign_films LIMIT TO (actors, directors)
FROM SERVER film_server INTO films;
two little examples, both iconic, both literally doing their job β
πΊ COMPATIBILITY πΊ
The IMPORT FOREIGN SCHEMA command conforms to the SQL standard β with one tiny caveat: the OPTIONS clause is a PostgreSQL extension. so yeah, standard-ish, but not 100% standard. ngl it ate though π
π« 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!! π brought to you by PostgreSQL 16.15, dated 2026, filed under IMPORT FOREIGN SCHEMA(7) β slay, periodt, go import some schemas πβ¨π¦
π¦ where this page comes from
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-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