如何在postgresql中查找具有特定列的表

时间:2013-08-29 10:25:44

标签: sql database postgresql postgresql-9.1

我正在使用postgresql 9.1。我有一个表的列名。是否有可能找到拥有此列的表格?怎么样?提前致谢。

6 个答案:

答案 0 :(得分:112)

您也可以

 select table_name from information_schema.columns where column_name = 'your_column_name'

答案 1 :(得分:46)

您可以查询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

答案 2 :(得分:5)

我使用@Roman Pekar的查询作为基础并添加了模式名称(在我的情况下相关)

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

答案 3 :(得分:0)

简单地:

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

如果需要,放大-B偏移量以获得表名

答案 4 :(得分: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 like '%STRING%'
      and t.table_schema not in ('information_schema', 'pg_catalog')
      and t.table_type = 'BASE TABLE'
order by t.table_schema;

答案 5 :(得分: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;