IMPORT_FOREIGN_SCHEMA(7pg14)

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

πŸ’« IMPORT FOREIGN SCHEMA πŸ’«

okay bestie, so this is the IMPORT FOREIGN SCHEMA page from the PostgreSQL 14.24 docs, and honestly? it's giving "magically pull someone else's tables into your own schema" and I'm obsessed. let's get into it 🫢


✨ SYNOPSIS ✨

like, here's the whole vibe in code form:

IMPORT FOREIGN SCHEMA remote_schema
    [ { LIMIT TO | EXCEPT } ( table_name [, ...] ) ]
    FROM SERVER server_name
    INTO local_schema
    [ OPTIONS ( option 'value' [, ... ] ) ]

🌸 DESCRIPTION 🌸

IMPORT FOREIGN SCHEMA literally creates foreign tables that represent tables that already exist on a foreign server. so you're not copying data, you're making little local stand-ins that know how to reach back to the originals. no cap.

the new foreign tables? they're owned by whoever runs the command, and they get created with the correct column definitions and options so they actually match the remote tables. it's giving "matching outfits" energy πŸ‘‘

by default, all tables and views that exist in a particular schema on the foreign server get imported. but you can optionally limit the list to a specified subset, or exclude specific tables if you're feeling picky. all the new foreign tables get created in the target schema, which β€” heads up β€” must already exist. you can't just vibe your way into a schema that isn't there πŸ’€

to use IMPORT FOREIGN SCHEMA, the user must have USAGE privilege on the foreign server, and CREATE privilege on the target schema. both. no skipping. periodt.


πŸŽ€ PARAMETERS πŸŽ€

  • remote_schema β€” the remote schema you're importing from. the specific meaning of a remote schema depends on the foreign data wrapper in use, so like... it's a little bit iykyk per-FDW.
  • LIMIT TO ( table_name [, ...] ) β€” import only the foreign tables matching one of the given table names. everything else hanging out in the foreign schema just gets ignored. sorry to them πŸ’…
  • EXCEPT ( table_name [, ...] ) β€” the opposite vibe: exclude the specified foreign tables from the import. all tables existing in the foreign schema get imported except the ones you list here.
  • server_name β€” the foreign server you're importing from. the source of the slay.
  • local_schema β€” the schema where the imported foreign tables get created. (remember: it has to already exist!)
  • OPTIONS ( option 'value' [, ...] ) β€” options to be used during the import. the allowed option names and values are specific to each foreign data wrapper, so check your FDW's own manual, bestie πŸ¦‹

πŸ”₯ EXAMPLES πŸ”₯

okay so picture this: you wanna import table definitions from a remote schema foreign_films on server film_server, and you want the foreign tables to land in local schema films. iconic. here's the move:

IMPORT FOREIGN SCHEMA foreign_films
    FROM SERVER film_server INTO films;

and now the same thing, but you only want 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 just slides in there? main character energy, honestly ✨


🌈 COMPATIBILITY 🌈

the IMPORT FOREIGN SCHEMA command conforms to the SQL standard β€” except that the OPTIONS clause is a PostgreSQL extension. so yeah, mostly standard, one lil PostgreSQL-flavored bonus 🧚


πŸ’œ SEE ALSO πŸ’œ

go read those next, fr fr πŸ“


ngl the whole point is: import the definitions, not the data. don't forget the privileges, and don't forget that local_schema has to exist first. that's the vibe check πŸ’€

that's the whole page, bestie β€” straight from the PostgreSQL 14.24 documentation, dated 2026, under the IMPORT FOREIGN SCHEMA(7) manual entry. now go forth and import something iconic πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from