IMPORT_FOREIGN_SCHEMA(7pg13)

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

πŸ’… 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 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. 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 🌈

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