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.

Kindextension
Example LanguageSQL

📝 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

More case studies coming soon.

📚 Related Reference

❓ FAQ

Qpostgres_fdw versus dblink?
Apostgres_fdw is declarative: you declare a server and foreign tables, then write ordinary SQL that can join with local tables and take part in indexing and query planning. dblink is imperative: every call sends a hand-built SQL string to the remote server, with no table metadata and no planner involvement.
QHow do I speed up slow foreign-table queries?
AStart with EXPLAIN to confirm that predicates are actually pushed to the remote server; index the filtered columns on the remote side; then ANALYZE the foreign table to pull remote statistics into the local catalog; and, if needed, lower fdw_startup_cost / fdw_tuple_cost so the planner is more willing to push filters down.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏