Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sknutsonsf/a04a50a7bb13bca9c08b to your computer and use it in GitHub Desktop.
Save sknutsonsf/a04a50a7bb13bca9c08b to your computer and use it in GitHub Desktop.
Postgres: find all tables containing a specific column name
-- 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