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?
Asked
Active
Viewed 9.1k times
6 Answers
208
You can also do
select table_name from information_schema.columns where column_name = 'your_column_name'

Chankey Pathak
- 21,187
- 12
- 85
- 133

Ravi Shekhar
- 2,633
- 4
- 19
- 19
-
2oddly, I've seen instances where this query shows tables that @RomanPekar's query does not; I wonder why that would be – Ken Bellows Jul 18 '16 at 16:05
-
2@KenBellows I guess pg_class / pg_attirbute can change with new versions of Postgresql while information_schema is defined in ANSI specification. So for general queries I'd say this answer is better. Sometimes I need to have object id for example, in this case I need to use db-engine specific tables. Plus, information_schema views are always an additional step over db engine specific tables and sometimes can lead to a (slightly) worse performance – Roman Pekar Feb 18 '19 at 08:44
-
1This was the more accurate of the two solutions offered - in my case. The pg_class query missed two (of 150) tables. The information_schema query captured all of the tables. I'll have to dig around to see why two tables fell outside of the join. In any event thanks for the info! – Thomas Altfather Good Aug 26 '19 at 15:30
81
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'

Roman Pekar
- 107,110
- 28
- 195
- 197
-
2Note that this query doesn't seem to accept '%' wildcards, while the query in Ravi's answer does. – Skippy le Grand Gourou Feb 13 '17 at 10:26
-
1
-
1this didn't work for me with or without wildcards, i've had to use information.schema to search – Lrawls Feb 14 '19 at 15:50
10
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'

jutky
- 3,895
- 6
- 31
- 45
4
Simply:
$ psql mydatabase -c '\d *' | grep -B10 'mycolname'
Enlarge -B offset to get table name, if need

Dmitry
- 846
- 1
- 7
- 20
4
Wildcard Support Find the table schema and table name that contains the string you want to find.
select t.table_schema,
t.table_name
from information_schema.tables t
inner join information_schema.columns c on c.table_name = t.table_name
and c.table_schema = t.table_schema
where c.column_name like '%STRING%'
and t.table_schema not in ('information_schema', 'pg_catalog')
and t.table_type = 'BASE TABLE'
order by t.table_schema;

jjj
- 2,594
- 7
- 36
- 57
0
select t.table_schema,
t.table_name
from information_schema.tables t
inner join information_schema.columns c on c.table_name = t.table_name
and c.table_schema = t.table_schema
where c.column_name = 'name_colum'
and t.table_schema not in ('information_schema', 'pg_catalog')
and t.table_type = 'BASE TABLE'
order by t.table_schema;

romuloMendes
- 47
- 6
-
6Please [edit your answer](https://stackoverflow.com/posts/59202865/edit) to include an explanation for your code. The question is more than *six years old*, and already has an accepted answer in addition to several well-upvoted, well-explained ones. Without such an explanation on your answer, it stands to be downvoted or removed. Adding that extra info would help justify your answer's continued existence here. – Das_Geek Dec 05 '19 at 20:52