snippetsqlMinor
Create Materialized View from foreign table
Viewed 0 times
creatematerializedforeignviewfromtable
Problem
I'd like to create a simple materialized view from a table which lies in a different database. The two databases are on the same server.
What do I have to add to make the query access the foreign database and the table there?
I tried using the fully qualified table name (db name before the schema name) but this results in an error:
References to other dbs are not implemented
What do I have to add to make the query access the foreign database and the table there?
CREATE MATERIALIZED VIEW mv_table_1 AS
SELECT *
FROM public.mv_table_1 --The schema & table from the different DB
WITH DATA;I tried using the fully qualified table name (db name before the schema name) but this results in an error:
References to other dbs are not implemented
Solution
PostgreSQL databases cannot "see" each other by default. This is a good thing.
You can make one pg database see another by using the postgres_fdw Foreign Data Wrapper: https://www.postgresql.org/docs/current/static/postgres-fdw.html
Steps:
You can make one pg database see another by using the postgres_fdw Foreign Data Wrapper: https://www.postgresql.org/docs/current/static/postgres-fdw.html
Steps:
- Add the extension
- Create the foreign server
- Create the user mapping
- Create the foreign table. See https://www.postgresql.org/docs/current/static/sql-importforeignschema.html if you have 9.5+
- Create the materialized view pointing to the foreign table
Context
StackExchange Database Administrators Q#140341, answer score: 6
Revisions (0)
No revisions yet.