postgresql获取表结构,表名、表注释、字段名、字段类型及长度和字段注释(转载)

转载地址:https://blog.csdn.net/weixin_38924323/article/details/80982760


场景描述:navicate 将postgresql表结构导出到Excel。

1、查询表名和表注释

select relname as tabname,
cast(obj_description(relfilenode,'pg_class') as varchar) as comment 
from pg_class c 
where   relname ='table_name' ;

2、查询字段名、字段类型及字段长度和字段注释

select a.attnum AS "序号",
c.relname AS "表名",
cast(obj_description(relfilenode,'pg_class') as varchar) AS "表名描述",
a.attname AS "列名",
concat_ws('',t.typname,SUBSTRING(format_type(a.atttypid,a.atttypmod) from '\(.*\)')) as "字段类型",
d.description AS "备注"
from pg_class c, pg_attribute a , pg_type t, pg_description d 
where  c.relname = '表名'
and a.attnum>0 
and a.attrelid = c.oid 
and a.atttypid = t.oid 
and  d.objoid=a.attrelid
and d.objsubid=a.attnum
ORDER BY c.relname DESC,a.attnum ASC


其他:通过查询sql,可以导出结果到EXCEL中。

在这里插入图片描述

再对导出的结果进一步格式化即可。

猜你喜欢

转载自blog.csdn.net/qq_31156277/article/details/85260325