120

我正在使用 PostgreSQL 9.1。我有一个表的列名。是否可以找到具有/具有此列的表?如果是这样,怎么做?

4

6 回答 6

175

你也可以做

 select table_name from information_schema.columns where column_name = 'your_column_name'
于 2015-07-30T06:58:15.697 回答
76

您可以查询系统目录

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

于 2013-08-29T10:39:21.973 回答
9

我使用@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

于 2018-04-09T06:56:02.930 回答
3

简单地:

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

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

于 2019-07-26T14:11:04.207 回答
2

通配符支持 查找包含您要查找的字符串的表架构和表名。

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;
于 2019-10-29T13:18:28.587 回答
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;
于 2019-12-05T20:32:15.513 回答