173

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?

7 Answers 7

244

You can also do

 select table_name from information_schema.columns where column_name = 'your_column_name'
Sign up to request clarification or add additional context in comments.

3 Comments

oddly, I've seen instances where this query shows tables that @RomanPekar's query does not; I wonder why that would be
@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
This 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!
86

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

3 Comments

Note that this query doesn't seem to accept '%' wildcards, while the query in Ravi's answer does.
@SkippyleGrandGourou It does accepts " like 'id%' "
this didn't work for me with or without wildcards, i've had to use information.schema to search
13

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

Comments

5

Simply:

$ psql mydatabase -c '\d *' | grep -B10 'mycolname'

Enlarge -B offset to get table name, if need

1 Comment

kinda nifty little shortcut here, nice.
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;

Comments

2

If you are unsure about the exact column name, and know only a part of it or having a wild guess; this might help.

select table_name, column_name from information_schema.columns where column_name like '%po%'

1 Comment

This is also working fine. I checked.
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;

1 Comment

Please edit your answer 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.