patternsqlMinor
Get a list of databases that have a certain table
Viewed 0 times
databasestablegetthatlistcertainhave
Problem
I'm trying to get a list of databases from a Postgresql instance that contains a certain table. I've started using the system catalog tables to try and figure this out. I was trying to use pg_database, pg_class, pg_tables but I couldn't seem to find a way to link pg_class/pg_table back to pg_database to get the database for a given table.
If anyone has any ideas please let me know. It's probably just something stupid that I'm misising.
Thanks in advance.
If anyone has any ideas please let me know. It's probably just something stupid that I'm misising.
Thanks in advance.
Solution
You need to query each database individually to check whether the table is there. Here is an example of a shell script:
for db in $(psql -At -c 'select datname from pg_database where not datistemplate'); do
psql -d $db -c 'select * from pg_tables where ...'
doneCode Snippets
for db in $(psql -At -c 'select datname from pg_database where not datistemplate'); do
psql -d $db -c 'select * from pg_tables where ...'
doneContext
StackExchange Database Administrators Q#1449, answer score: 3
Revisions (0)
No revisions yet.