← Back to articles

Postgres Foreign Data Wrapper

PostgreSQL FDW extension is cool. It allows us to query a table on a remote database server as if it was a local one. Many different database server software types are supported.

As an example (must be superuser on our PostgreSQL instance) 👇

create extension if not exists postgres_fdw;
-- Cleaning instructions if needed
--drop foreign table if exists my_remote_table;
--drop user mapping if exists for my_user server remote_server;
--drop server if exists remote_server;
create server if not exists remote_server
  foreign data wrapper postgres_fdw
  options (host 'remote_server.domain', port '5432', dbname 'remote_db');
create user mapping if not exists for my_user
  server remote_server
  options (user 'my_user', password 'my_password');
create foreign table if not exists my_remote_table (
  id uuid,
  name text
)
  server remote_server
  options (schema_name 'public', table_name 'my_table');

Given that:

We should be able to query my_remote_table on the local server and fetch records from the remote one.

Some considerations

🔒Beware of security

⚡Beware of performance issues on cross-server joins

🚨Beware of unavailability of the remote server