How to get a list column names and datatype of a table in PostgreSQL?

前端 未结 11 2243
[愿得一人]
[愿得一人] 2020-12-02 10:02

With the following query, we can get a list of column names and datatype of a table in PostgreSQL.

11条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 10:38

    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)
            );
    

    Change the hello world with your table name

    More info on it : http://www.postgresql.org/docs/9.3/static/catalog-pg-attribute.html

提交回复
热议问题