We have a around 100+ schema maintained in PostgreSQL. Now we want to query on all schema, is there any way to do that? other than views, procedures and union all? Any postgres functions which let you query on multiple schemas
Asked
Active
Viewed 5,301 times
7
-
No, there is no such function built-in – Oct 12 '18 at 11:01
-
Do you know any extensions which can do that? I tried madlib, couldnt find any – Nisa Anwaar Oct 12 '18 at 14:08
2 Answers
9
The following catalog query will produce valid queries for every table on all schemas of your database. You can copy this to a valid SQL file.
SELECT 'SELECT * FROM ' || table_schema || '.' || table_name || ';' AS query
FROM information_schema.tables
WHERE table_schema IN
(
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name NOT LIKE 'pg_%' AND schema_name != 'information_schema'
);
Does this help?

Reto Buchli
- 124
- 6
-
1Is there a way to tell Postgres to execute the generated queries on the spot? – payne Dec 03 '21 at 22:37
-
re: Is there a way to tell Postgres to execute the generated queries on the spot? in psql use \gexec at the end of the query instead of semicolon – srk Mar 14 '22 at 00:02
0
Use this query
set search_path = schema_name_1, schema_name_2, schema_name_3, schema_name_4;
SELECT * FROM table_1;

Fayiz Fareed
- 35
- 2
- 9