Skip to content

Instantly share code, notes, and snippets.

@slabad
Last active July 1, 2020 14:48
Show Gist options
  • Save slabad/f6bb49d49e5242cb9374bfe08e01282d to your computer and use it in GitHub Desktop.
Save slabad/f6bb49d49e5242cb9374bfe08e01282d to your computer and use it in GitHub Desktop.
Cross Database Querying on Postgres #psql #postgres
--for a cross db query on the same host
--no hostname or password is needed.
--the following script adds the extension,
-- creates a server, maps the postgres user,
-- and imports the foreign db into a new schema
BEGIN;
CREATE EXTENSION IF NOT EXISTS postgres_fdw;
DROP SERVER IF EXISTS fdw_server CASCADE;
CREATE SERVER fdw_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname 'mydatabasename');
CREATE USER MAPPING FOR postgres
SERVER fdw_server
OPTIONS (user 'postgres');
DROP SCHEMA IF EXISTS fdw;
CREATE SCHEMA fdw;
IMPORT FOREIGN SCHEMA public
FROM SERVER fdw_server
INTO fdw;
COMMIT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment