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

前端 未结 11 2242
[愿得一人]
[愿得一人] 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:43

    A version that supports finding the column names and types of a table in a specific schema, and uses JOINs without any subqueries

    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;
    

提交回复
热议问题