Tutorials Courses Interview Prep (100 Q&A) AI Tools (50) Developer Tools (50) Tracks Pricing Plans Blog & Articles Contact Us
PostgreSQL Enterprise & Performance 8-10 Years (Tech Lead) Hard Production Scenario

How does PostgreSQL Foreign Data Wrapper (`postgres_fdw`) enable cross-database federated queries?

Real-World Scenario / Incident Description

Joining customer CRM data stored in PostgreSQL Database A with billing records stored in PostgreSQL Database B.

794 views • Updated 1 month ago

Configuring postgres_fdw

CREATE EXTENSION postgres_fdw;

CREATE SERVER billing_server
    FOREIGN DATA WRAPPER postgres_fdw
    OPTIONS (host '10.0.0.50', dbname 'billing_db', port '5432');

CREATE USER MAPPING FOR current_user
    SERVER billing_server
    OPTIONS (user 'db_reader', password 'secret');

IMPORT FOREIGN SCHEMA public FROM SERVER billing_server INTO foreign_billing;

-- Query local and remote tables seamlessly in a single SQL statement
SELECT c.name, b.invoice_total 
FROM customers c
JOIN foreign_billing.invoices b ON c.id = b.customer_id;
Related Concepts: #postgres_fdw #Foreign Data Wrapper #Federated Queries