PostgreSQL查询表名称和表结构

2020-03-04   阅读(137)  

-- 查询所有表名称
SELECT tablename FROM pg_tables;

-- 查询所有视图名称
SELECT viewname FROM pg_views;

-- 查询某个表的表结构
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 OUTER JOIN pg_description b ON a.attrelid=b.objoid AND a.attnum = b.objsubid,
pg_type t
WHERE c.relname = 'your tablename'
and a.attnum > 0
and a.attrelid = c.oid
and a.atttypid = t.oid
ORDER BY a.attnum;

 

ConstXiong 备案号:苏ICP备16009629号-3