How to find a table having a specific column in postgresql
I'm using PostgreSQL 9.1. I have the column name of a table. Is it possible to find the table(s) that has/have this column? If so, how?
Solution 1:
You can also do
select table_name from information_schema.columns where column_name = 'your_column_name'
Solution 2:
you can query system catalogs:
select c.relname
from pg_class as c
inner join pg_attribute as a on a.attrelid = c.oid
where a.attname = <column name> and c.relkind = 'r'
sql fiddle demo
Solution 3:
I've used the query of @Roman Pekar as a base and added schema name (relevant in my case)
select n.nspname as schema ,c.relname
from pg_class as c
inner join pg_attribute as a on a.attrelid = c.oid
inner join pg_namespace as n on c.relnamespace = n.oid
where a.attname = 'id_number' and c.relkind = 'r'
sql fiddle demo
Solution 4:
Simply:
$ psql mydatabase -c '\d *' | grep -B10 'mycolname'
Enlarge -B offset to get table name, if need