postgres_fdw: Accessing Data in External PostgreSQL Servers
Last updated: 2026-11-04
postgres_fdw: Accessing Data in External PostgreSQL Servers
postgres_fdw is a foreign data wrapper that exposes tables from another PostgreSQL instance as local foreign tables; the local planner pushes qualifying conditions down to the remote server as it would for a local table. Configuration takes four steps: install the extension, CREATE SERVER to describe the remote connection, CREATE USER MAPPING to supply remote credentials, and CREATE FOREIGN TABLE to declare the column mapping. It is the most common way to build cross-database JOINs, cross-database ETL and data federation.
| Kind | extension |
|---|---|
| Example Language | SQL |
📝 Syntax
CREATE EXTENSION IF NOT EXISTS postgres_fdw;
CREATE SERVER remote_pg
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'db.example.com', port '5432', dbname 'analytics');
CREATE USER MAPPING FOR CURRENT_USER
SERVER remote_pg
OPTIONS (user 'readonly', password 'PLACEHOLDER_SECRET');
CREATE FOREIGN TABLE t_remote_orders (
id bigint, sku text, amount numeric(12,2)
) SERVER remote_pg
OPTIONS (schema_name 'public', table_name 'orders');
SELECT * FROM t_remote_orders;
Returns:CREATE EXTENSION, CREATE SERVER, CREATE USER MAPPING and CREATE FOREIGN TABLE are all DDL and return no result set on success. A SELECT against the foreign table returns rows from the remote table, with column types taken from the local declaration.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
SELECT s.srvname,
w.fdwname,
s.srvoptions
FROM pg_foreign_server s
JOIN pg_foreign_data_wrapper w ON w.oid = s.srvfdw
WHERE s.srvname = 'remote_pg'
srvname | fdwname | srvoptions
------------+------------+--------------------------------------------------------
remote_pg | postgres_fdw | {host=db.example.com,port=5432,dbname=analytics}
(1 row)
Note: host / dbname / user / password are all placeholders; with the remote unreachable, the SELECT against the foreign table would fail with a connection error. The query shown inspects the local catalog pg_foreign_server to confirm the server and foreign table were created.
📌 Key Points
- postgres_fdw is not bundled with browser PGlite: it is absent from the contrib package list and there is no official npm package (the 34 shipped contrib modules cover local wrappers such as file_fdw only). Running it in a browser requires building a WASM build yourself; the usual approach is to run this example on a real PostgreSQL server.
- Size: postgres_fdw is not in PGlite's contrib set, so there is no comparable bundle size to quote; for reference, pg_trgm is ~15 KB, btree_gist ~22 KB, uuid-ossp ~17 KB and pgcrypto ~1.1 MB.
- Credentials: a password given in CREATE USER MAPPING is stored in cleartext in pg_user_mapping.umoptions. In production it is better to omit the password option and rely on ~/.pgpass or a secret manager; SCRAM deployments can additionally use use_scram_passthrough to pass the hash through unchanged.
- Options on the server are libpq connection-string options (host, port, dbname, sslmode and friends), except that user and password are not allowed there — they belong in the user mapping.
- Columns of a foreign table are matched to the remote ones by name, not by position, so the local column order may differ and you may declare only the subset you need; postgres_fdw converts mismatched types when it can, but the semantics may then surprise you.
- Querying a foreign table opens a connection to the remote server and may push conditions down, so the main cost is network round-trips; running ANALYZE on the foreign table pulls remote statistics into the local catalog and improves planning.