Retrieving Comments from a PostgreSQL DB

后端 未结 13 2033
别那么骄傲
别那么骄傲 2020-12-05 13:13

I\'m running a project on a Postgres database and need to retrieve the comments on columns within the DB to be used as table headings and such. I have seen that there are a

相关标签:
13条回答
  • 2020-12-05 14:08

    I just found this here. It will provide you with all kind of metadata on one specific table (type, default value, not null flag, length, comment, foreign key name, primary key name). It seems to work well.

    SELECT pg_tables.tablename, pg_attribute.attname AS field, 
        format_type(pg_attribute.atttypid, NULL) AS "type", 
        pg_attribute.atttypmod AS len,
        (SELECT col_description(pg_attribute.attrelid, 
                pg_attribute.attnum)) AS comment, 
        CASE pg_attribute.attnotnull 
            WHEN false THEN 1  ELSE 0  
        END AS "notnull", 
        pg_constraint.conname AS "key", pc2.conname AS ckey, 
        (SELECT pg_attrdef.adsrc FROM pg_attrdef 
            WHERE pg_attrdef.adrelid = pg_class.oid 
            AND pg_attrdef.adnum = pg_attribute.attnum) AS def 
    FROM pg_tables, pg_class 
    JOIN pg_attribute ON pg_class.oid = pg_attribute.attrelid 
        AND pg_attribute.attnum > 0 
    LEFT JOIN pg_constraint ON pg_constraint.contype = 'p'::"char" 
        AND pg_constraint.conrelid = pg_class.oid AND
        (pg_attribute.attnum = ANY (pg_constraint.conkey)) 
    LEFT JOIN pg_constraint AS pc2 ON pc2.contype = 'f'::"char" 
        AND pc2.conrelid = pg_class.oid 
        AND (pg_attribute.attnum = ANY (pc2.conkey)) 
    WHERE pg_class.relname = pg_tables.tablename  
    --    AND pg_tables.tableowner = "current_user"() 
        AND pg_attribute.atttypid <> 0::oid  
        AND tablename='your_table' 
    ORDER BY field ASC
    

    Source: http://golden13.blogspot.de/2012/08/how-to-get-some-information-about_7.html

    0 讨论(0)
提交回复
热议问题