Overview

An FDW (built on the SQL/MED standard) presents an external source as a foreign table that joins against local tables. Reach for one when a report must combine Postgres rows with another database or file without building an ETL job first. A foreign table is unrelated to a foreign-key.

Rules

  • Check pushdown with EXPLAIN VERBOSE: the Foreign Scan node shows the SQL sent to the remote side. Without pushdown, the whole remote table crosses the network and is filtered locally. Select only the columns you need.
  • postgres_fdw and file_fdw ship with PostgreSQL. mysql_fdw, sqlite_fdw, and Supabase Wrappers (Stripe, S3, ClickHouse, and others) are third-party.
  • Use FDWs for ad-hoc federation and low-volume lookups. For latency-sensitive or high-volume analytics, copy the data into a local table.
CREATE EXTENSION postgres_fdw;
CREATE SERVER orders_db FOREIGN DATA WRAPPER postgres_fdw
  OPTIONS (host 'orders.internal', dbname 'orders', port '5432');
CREATE USER MAPPING FOR current_user SERVER orders_db
  OPTIONS (user 'reader', password 'change-me');
IMPORT FOREIGN SCHEMA public LIMIT TO (orders) FROM SERVER orders_db INTO local_schema;