IMPORT_FOREIGN_SCHEMA(7pg13)
π IMPORT_FOREIGN_SCHEMA π
(that's the manual page for PostgreSQL 13.23, bestie, and we're about to make it make sense)
okay so picture this: you've got a whole entire schema living on some remote server, right? and you're like "omg I wish I had all those tables here locally, but making them one by one is such a vibe killer." enter this command. it literally reaches into the foreign server and yoinks the table definitions for you. no cap. it's giving "personal assistant who actually does their job." π
β¨ NAME β¨
IMPORT_FOREIGN_SCHEMA β import table definitions from a foreign server. (yes, that's literally the whole point, and it slays) π
πΈ SYNOPSIS πΈ
IMPORT FOREIGN SCHEMA remote_schema
[ { LIMIT TO | EXCEPT } ( table_name [, ...] ) ]
FROM SERVER server_name
INTO local_schema
[ OPTIONS ( option 'value' [, ... ] ) ]
look at that syntax. gorgeous. we love a structured queen. π
π¦ DESCRIPTION π¦
so IMPORT FOREIGN SCHEMA creates foreign tables that represent tables existing on a foreign server. the new foreign tables will be owned by the user issuing the command (that's you, bestie, no pressure) and they're created with the correct column definitions and options to match the remote tables. it's honestly a whole matchmaking service for your schemas. π
by default, all tables and views existing in a particular schema on the foreign server are imported. like, the full set. no picking and choosing unless you ask. optionally, the list of tables can be limited to a specified subset, or specific tables can be excluded (because sometimes you do have favorites, and that's valid). the new foreign tables are all created in the target schema, which must already exist β so don't come for this command if you haven't done your homework and made the schema first. π«Ά
ok important though: to useIMPORT FOREIGN SCHEMA, the user must haveUSAGEprivilege on the foreign server, as well asCREATEprivilege on the target schema. both. not one. both. the way it's non-negotiable. π
π PARAMETERS π
remote_schemaβ the remote schema to import from. and like, the specific meaning of a remote schema depends on the foreign data wrapper in use. so it's a little "you know who you are" situation for each 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 literally the VIP list. if you're not on it, you're not getting in. πEXCEPT ( table_name [, ...] )β the opposite energy: exclude specified foreign tables from the import. all tables existing in the foreign schema will be imported except the ones listed here. it's giving "blocked." π«server_nameβ the foreign server to import from. you know, the source. the origin story. βlocal_schemaβ the schema in which the imported foreign tables will be created. this is the destination. the new home. make it cute. π‘OPTIONS ( option 'value' [, ...] )β options to be used during the import. the allowed option names and values are specific to each foreign data wrapper. so like, check with your FDW first. communication is key, bestie. π¬
π₯ EXAMPLES π₯
okay so let's say you wanna import table definitions from a remote schema foreign_films on server film_server, creating the foreign tables in local schema films. boom:
IMPORT FOREIGN SCHEMA foreign_films
FROM SERVER film_server INTO films;
iconic. effortless. needs no notes. β¨
and now the same thing but you're being picky and only want the two tables actors and directors (if they exist β if not, don't blame me):
IMPORT FOREIGN SCHEMA foreign_films LIMIT TO (actors, directors)
FROM SERVER film_server INTO films;
see how easy that was? main character energy. π¬
π COMPATIBILITY π
the IMPORT FOREIGN SCHEMA command conforms to the SQL standard, except that the OPTIONS clause is a PostgreSQL extension. so it's standard-issue but with a lil extra flavor. we stay extending. periodt. π
π SEE ALSO π
CREATE FOREIGN TABLE(CREATE_FOREIGN_TABLE(7))CREATE SERVER(CREATE_SERVER(7))
go read those if you want the full lore. iykyk. π¦
π okay that's the whole page bestie. shoutout to the PostgreSQL 13.23 documentation, dated 2025 β she's thorough, she's detailed, and now she's cute too. go import some schemas and slay responsibly. mwah. πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-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