如何在PostgreSQL中获取表的列表列名称和数据类型?


Answers:


133

打开psql命令行并输入:

\d+ table_name

1
我不明白为什么这不是最受好评的答案。
mjezzi

18
这是不完整的,因为OP可能希望以SQL代码(而不只是通过psql)以编程方式执行此操作。
路加福音

Postgres以编程方式执行此操作,因此只需以'-E'标志启动postgres:psql -E对于每个反斜杠命令,相应的SQL将显示在命令结果之前。
karatedog

115
select column_name,data_type 
from information_schema.columns 
where table_name = 'table_name';

通过上面的查询,您可以列及其数据类型


4
对于用户定义的类型(例如,由ogr2ogr创建的Geometry和Geography列,形式为geometry(Geometry,[SRID])),这不会给出正确的答案。
GT。

1
为了从特定数据库的特定架构的特定表中仅获取列,可能还会使用table_catalog = 'my_database'table_schema = 'my_schema'
Marco Mannes

24
SELECT
        a.attname as "Column",
        pg_catalog.format_type(a.atttypid, a.atttypmod) as "Datatype"
    FROM
        pg_catalog.pg_attribute a
    WHERE
        a.attnum > 0
        AND NOT a.attisdropped
        AND a.attrelid = (
            SELECT c.oid
            FROM pg_catalog.pg_class c
                LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
            WHERE c.relname ~ '^(hello world)$'
                AND pg_catalog.pg_table_is_visible(c.oid)
        );

使用表格名称更改世界

有关更多信息:http : //www.postgresql.org/docs/9.3/static/catalog-pg-attribute.html


2
可行,但是为什么要使用c.relname ~ '^(hello world)$而不是简单地使用c.relname = 'hello world'
托马斯

18

如果您有多个具有相同表名的架构,请不要忘记添加架构名称。

SELECT column_name, data_type 
FROM information_schema.columns
WHERE table_name = 'your_table_name' AND table_schema = 'your_schema_name';

或使用psql:

\d+ your_schema_name.your_table_name

7

更新了Pratik答案以支持更多架构和可为空的内容:

SELECT
    "pg_attribute".attname                                                    as "Column",
    pg_catalog.format_type("pg_attribute".atttypid, "pg_attribute".atttypmod) as "Datatype",

    not("pg_attribute".attnotnull) AS "Nullable"
FROM
    pg_catalog.pg_attribute "pg_attribute"
WHERE
    "pg_attribute".attnum > 0
    AND NOT "pg_attribute".attisdropped
    AND "pg_attribute".attrelid = (
        SELECT "pg_class".oid
        FROM pg_catalog.pg_class "pg_class"
            LEFT JOIN pg_catalog.pg_namespace "pg_namespace" ON "pg_namespace".oid = "pg_class".relnamespace
        WHERE
            "pg_namespace".nspname = 'schema'
            AND "pg_class".relname = 'table'
    );

7

一个版本,该版本支持在特定架构中查找表的列名和类型,并使用JOIN而不包含任何子查询

SELECT
    pg_attribute.attname AS column_name,
    pg_catalog.format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS data_type
FROM
    pg_catalog.pg_attribute
INNER JOIN
    pg_catalog.pg_class ON pg_class.oid = pg_attribute.attrelid
INNER JOIN
    pg_catalog.pg_namespace ON pg_namespace.oid = pg_class.relnamespace
WHERE
    pg_attribute.attnum > 0
    AND NOT pg_attribute.attisdropped
    AND pg_namespace.nspname = 'my_schema'
    AND pg_class.relname = 'my_table'
ORDER BY
    attnum ASC;

2
    SELECT DISTINCT
        ROW_NUMBER () OVER (ORDER BY pgc.relname , a.attnum) as rowid , 
        pgc.relname as table_name ,
        a.attnum as attr,
        a.attname as name,
        format_type(a.atttypid, a.atttypmod) as typ,
        a.attnotnull as notnull, 
        com.description as comment,
        coalesce(i.indisprimary,false) as primary_key,
        def.adsrc as default
    FROM pg_attribute a 
    JOIN pg_class pgc ON pgc.oid = a.attrelid
    LEFT JOIN pg_index i ON 
        (pgc.oid = i.indrelid AND i.indkey[0] = a.attnum)
    LEFT JOIN pg_description com on 
        (pgc.oid = com.objoid AND a.attnum = com.objsubid)
    LEFT JOIN pg_attrdef def ON 
        (a.attrelid = def.adrelid AND a.attnum = def.adnum)
    LEFT JOIN pg_catalog.pg_namespace n ON n.oid = pgc.relnamespace

    WHERE 1=1 
        AND pgc.relkind IN ('r','')
        AND n.nspname <> 'pg_catalog'
        AND n.nspname <> 'information_schema'
        AND n.nspname !~ '^pg_toast'

    AND a.attnum > 0 AND pgc.oid = a.attrelid
    AND pg_table_is_visible(pgc.oid)
    AND NOT a.attisdropped
    ORDER BY rowid
    ;

2

使该主题“更完整”。

我需要SELECT语句(而不是表)上的列名和数据类型。

如果要在SELECT语句而不是实际的现有表上执行此操作,则可以执行以下操作:

DROP TABLE IF EXISTS abc;
CREATE TEMPORARY TABLE abc AS
-- your select statement here!
SELECT 
    *
FROM foo
-- end your select statement
;

select column_name, data_type 
from information_schema.columns 
where table_name = 'abc';
DROP IF EXISTS abc;

简短的解释是,它创建了一个select语句的(临时)表,您可以通过@a_horse_with_no_name和@selva提供的查询来“调用”该表。

希望这可以帮助。


2

不提及架构,您也可以获得所需的详细信息。尝试使用此查询->

从information_schema.columns中选择column_name,data_type,其中table_name ='table_name';


0

要获取有关表的列的信息,可以使用:

\dt+ [tablename]

要获取有关表中数据类型的信息,可以使用:

\dT+ [datatype]

1
\ dt + tablename获取有关该表的信息,但没有该列的信息
zhihong

0

从information_schema.columns中选择column_name,data_type,其中table_name ='your_table_name'和table_catalog ='your_database_name'和table_schema ='your_schema_name';

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.