Some database management systems allow to query data from different databases. PostgreSQL implements this capability via the feature known as Foreign Data Wrapper. It allows create "foreign tables" in a PostgreSQL databas that actually are proxies for external data sources. When foreign table is included in a query, Foreign Data Wrapper locates the correspoding data source, extracts the required data and return result as it would come from the current database table.
PostgreSQL offers two foreign data wrappers:
This is how to use a Foreign Data Wrapper on example of postgres_fdw:
CREATE EXTENSION postgres_fdw;
GRANT USAGE ON FOREIGN DATA WRAPPER postgres_fdw to dest_user;
CREATE SERVER app_database_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (dbname 'database_name', host 'server_name', port '5432');
CREATE USER MAPPING for dest_user SERVER server_name OPTIONS (user 'src_user', password 'some_password');
CREATE FOREIGN TABLE tbl1(id f1, f2 character varying(20)) SERVER server_name OPTIONS (schema_name 'public', table_name 'tbl1');
Now all necessary things are set up and it is reasonable to check if we can query the foreign table:
SELECT * FROM tbl1;
Have questions? Contact us