IMPORT_FOREIGN_SCHEMA(7pg14)
π« 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 π
CREATE FOREIGN TABLE (CREATE_FOREIGN_TABLE(7))CREATE SERVER (CREATE_SERVER(7))
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
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-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