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

前端 未结 11 2219
[愿得一人]
[愿得一人] 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条回答
  •  萌比男神i
    2020-12-02 10:41

    Updated Pratik answer to support more schemas and nullables:

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

提交回复
热议问题