PostgreSql生成数据字典

Posted TurboWay

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PostgreSql生成数据字典相关的知识,希望对你有一定的参考价值。

pg查询元数据,生成表数据字典:

 

效果:

 

  

代码:

SELECT
    A .attname 字段,   
    concat_ws (
        \'\',
        T .typname,
        SUBSTRING (
            format_type (A .atttypid, A .atttypmod)
            FROM
                \'\\(.*\\)\'
        )
    ) AS 类型,
        case when s.pk is not null then \'\'
                               else \'\'
    end as 主键,
    case A.attnotnull when \'f\' then \'\'
                      when \'t\' then \'\'
    end as 空,
    d.description 注释
FROM pg_attribute A
INNER JOIN pg_class C on A .attrelid = C .oid
INNER JOIN pg_type T on A .atttypid = T .oid
LEFT JOIN (SELECT conrelid, unnest(conkey) as pk
                        FROM pg_constraint
                        WHERE contype = \'p\') S ON S.conrelid = C .oid
                                   AND A.attnum = S.pk
LEFT JOIN pg_description d on d.objoid = A .attrelid
                                                    AND d.objsubid = A .attnum
LEFT JOIN pg_namespace n on n.oid = c.relnamespace    
WHERE A.attnum > 0
AND n.nspname = \'public\'   
AND C.relname = \'t_gfecp_bigdata_blacklist\'
ORDER BY
    C .relname,
    A .attnum

 

以上是关于PostgreSql生成数据字典的主要内容,如果未能解决你的问题,请参考以下文章

Python代码阅读(第40篇):通过两个列表生成字典

PostgreSQL 是不是支持表(片段)的透明压缩?

无需编程,基于PostgreSQL零代码生成CRUD增删改查RESTful API接口

13 个非常有用的 Python 代码片段

如何在php脚本中接受PostgreSQL数据库数量?

Python snippet(代码片段)