Postgresql查询数据库的表和表结构的方式

查看数据库的表

查看所在库的所有表信息

SELECT * FROM pg_catalog.pg_tables;

如果只是想获取表名

SELECT tablename FROM pg_catalog.pg_tables;

查看表结构

查看的方式较为繁琐,总结在如下代码

SELECT a.attnum, a.attname AS field, t.typname AS type, a.attlen AS length, a.atttypmod AS lengthvar
    , a.attnotnull AS notnull, b.description AS comment
FROM pg_class c, pg_attribute a
    LEFT JOIN pg_description b
    ON a.attrelid = b.objoid
        AND a.attnum = b.objsubid, pg_type t
WHERE c.relname = '实际的表名'
    AND a.attnum > 0
    AND a.attrelid = c.oid
    AND a.atttypid = t.oid
ORDER BY a.attnum;
作者:Qingyun
                
comments powered by Disqus