Foreign Data Wrapper
suggest changeTo access a whole schema on another PostgreSQL server, instead of a single table, follow the steps below. postgres_fdw must be available on the local server.
- Create the extension:
CREATE EXTENSION postgres_fdw;
- Create the server:
CREATE SERVER server_name FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'host_ip',
dbname 'db_name', port 'port_number');
- Create a user mapping:
CREATE USER MAPPING FOR CURRENT_USER
SERVER server_name
OPTIONS (user 'user_name', password 'password');
- Create a local schema to hold the foreign tables:
CREATE SCHEMA schema_name;
- Import the remote schema:
IMPORT FOREIGN SCHEMA schema_name_to_import_from_remote_db
FROM SERVER server_name
INTO schema_name;
- Read a remote table through the local schema:
SELECT * FROM schema_name.table_name;
Repeat the import for each remote schema you need. The remote user in the user mapping needs privileges on those tables.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents