I have seveval databases in a postgresql db server
each database has its own username/password, and only access by nominate schema (in fact, the schema is same name of the username)
I'd like to create a all_tables view, so i can do query later.
I did some research on dblink extension, but still can't make it work.
SELECT dblink_connect('source_db_1', 'postgres://db_1:[email protected]/db_1?sslmode=require');
CREATE SERVER source_db_1 FOREIGN DATA WRAPPER dblink_fdw OPTIONS (hostaddr 'psqlserver.postgres.database.azure.com', dbname 'db_1');
CREATE USER MAPPING FOR current_user SERVER source_db_1 OPTIONS (user 'db_1', password 'password');
SELECT dblink_connect('source_db_2', 'postgres://db_2:[email protected]/db_2?sslmode=require');
CREATE SERVER source_db_2 FOREIGN DATA WRAPPER dblink_fdw OPTIONS (hostaddr 'psqlserver.postgres.database.azure.com', dbname 'db_2');
CREATE USER MAPPING FOR current_user SERVER source_db_2 OPTIONS (user 'db_2', password 'password');
CREATE OR REPLACE VIEW all_tables_in_db_1 AS
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'db_1';
CREATE OR REPLACE VIEW all_tables_in_db_2 AS
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'db_2';
CREATE OR REPLACE VIEW all_tables_in_db_3 AS
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'db_3';
CREATE OR REPLACE VIEW all_tables AS
SELECT * FROM all_tables_in_db_1
UNION ALL
SELECT * FROM all_tables_in_db_2
UNION ALL
SELECT * FROM all_tables_in_db_3;
all commands run fine, but when query in all_tables, there is no any tables
dblink_fdwcould do, but it's recommended that you use the nativepostgres_fdwinstead.dblink_connectand set up theserveranduser mapping. You use the former to manage connections to calldblink_execute, the latter to set up the foreign data wrapper and subsequently, aforeign tablethat uses them.information_schema. You can either run queries throughdblink_execand work with their output, or set up theforeign tableorimport foreign schemafor many tables at once. The second option most resembles what you expected it to do.Here's an example showing how to set up an
information_schema.tablesbut for multiple databases. Demo at db<>fiddle:Unfortunately,
create foreign tabledoesn't accept(like another_table)syntax so you need to list the column names and types on your own.In different versions of PostgreSQL Built-in tables and views can have different column names in different order, which you need to bear in mind whenever you link them as
foreign table: here is an example of view definition re-use going wrong.