Created
June 20, 2015 18:28
-
-
Save sknutsonsf/a04a50a7bb13bca9c08b to your computer and use it in GitHub Desktop.
Postgres: find all tables containing a specific column name
This file contains 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
-- locate all table names (relname) with a company_id column: | |
-- extracted from http://www.postgresonline.com/journal/archives/215-Querying-table,-view,-column-and-function-descriptions.html | |
select n.nspname, t.spcname, c.relname | |
FROM pg_class As c | |
INNER JOIN pg_attribute As a ON c.oid = a.attrelid | |
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace | |
LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace | |
where a.attname = 'company_id' | |
and c.relkind='r' | |
order by c.relname; | |
-- I did not test with multiple databases in the same postgres instance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment