Last active
July 1, 2020 14:48
-
-
Save slabad/f6bb49d49e5242cb9374bfe08e01282d to your computer and use it in GitHub Desktop.
Cross Database Querying on Postgres #psql #postgres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --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