IMPORT_FOREIGN_SCHEMA(7pg15)

πŸ“¦ version 15.19, in opensuse-leap 16.0

πŸ’– 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 use IMPORT FOREIGN SCHEMA, the user must have USAGE privilege on the foreign server, as well as CREATE privilege 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 🌟

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