Postgresql 导出表结构信息

摘要:
--查询所有表字段信息(带表名)select(selectrelname | |'--'| | |(selectdescriptionfrompg_descriptionwhereobjoid=oindobjsubid=0)as commentfromPg_classwhereoid=a.attrelid)astable_ name,format_ type(a.atttyid,

项目用了Postgresql 数据库,项目组要出表结构的文档,手写太麻烦,想用slq脚本导出一份。查了一番资料,似乎没有多好的方法。dump方式导出的脚本太乱,没法直接写在word文档里。只能自己写sql查询出表结构,然后利用pgadmin的导出查询结果的功能,能最快的获取一份整个数据库的表结构信息。虽然不能实现全自动的导出文档,但是对整理文档来说,还是节省了不少时间。

--查询所有的表字段信息(带表名)

select

(select relname||'--'||(select description from pg_description where objoid=oid and objsubid=0) as comment from pg_class where oid=a.attrelid) as table_name,

a.attname as column_name,

format_type(a.atttypid,a.atttypmod) as data_type,

(case when atttypmod-4>0 then atttypmod-4 else 0 end)data_length,

(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='p')>0 then 'Y' else 'N' end) as 主键约束,

(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='u')>0 then 'Y' else 'N' end) as 唯一约束,

(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='f')>0 then 'Y' else 'N' end) as 外键约束,

(case when a.attnotnull=true then 'Y' else 'N' end) as nullable,

col_description(a.attrelid,a.attnum) as comment

from pg_attribute a

where attstattarget=-1 and attrelid in (select oid from pg_class where relname in(select relname from pg_class where relkind ='r' and relname like 'exg_%'))

order by table_name,a.attnum;

执行sql语句,然后将查询结果导出

Postgresql 导出表结构信息第1张

使用英文逗号做分隔符,导出csv文件,可以直接在excle中编辑

使用本地字符集可以防止乱码

Postgresql 导出表结构信息第2张

 用excle打开导出的csv文件,因为导出的是所有的表结构信息,所以要先分表,拷贝第一行表头信息,分别插入到每张表的前面(插入行的快捷键自己网上找吧)。

Postgresql 导出表结构信息第3张

做完上面的步骤,就可以把表结构信息粘帖到word文档里了;

Postgresql 导出表结构信息第4张

在粘贴完毕后,要选择使用目标格式

Postgresql 导出表结构信息第5张Postgresql 导出表结构信息第6张

表格出来了

Postgresql 导出表结构信息第7张

这个时候所有的表结构是一张大表格,可以用Ctrl+shift+enter把表格分开

附:其它sql脚本

--查询表名和描述

select relname as table_name,(select description from pg_description where objoid=oid and objsubid=0) as comment from pg_class where relkind ='r' and relname like 'exg_%' order by table_name;

--查询表字段信息

select

a.attname as column_name,

format_type(a.atttypid,a.atttypmod) as data_type,

(case when atttypmod-4>0 then atttypmod-4 else 0 end)data_length,

(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='p')>0 then 'Y' else 'N' end) as 主键约束,

(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='u')>0 then 'Y' else 'N' end) as 唯一约束,

(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='f')>0 then 'Y' else 'N' end) as 外键约束,

(case when a.attnotnull=true then 'Y' else 'N' end) as nullable,

col_description(a.attrelid,a.attnum) as comment

from pg_attribute a

where attstattarget=-1 and attrelid = (select oid from pg_class where relname ='exg_ms_alarm');--表名

免责声明:文章转载自《Postgresql 导出表结构信息》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇TensorFlow 编程基础网络安全开发包介绍下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Pycharm2018中DataBase的使用

1、点击右侧边栏的DataBase,在出现的Database窗口下点击绿色小加号,选择Data Source,选择需要的数据库类型,此处选择Sqlite 2、配置数据库连接信息 3、选择schema,可以看到数据表信息 4、打开sql面板,输入查询SQL执行...

mysql中DDL库和表的管理

1 #DDL 2 /* 3 数据定义语言 4 5 库和表的管理 6 7 一、库的管理 8 创建、修改、删除 9 二、表的管理 10 创建、修改、删除 11 12 创建:create 13 修改:alter 14 删除:drop 15 */ 16 17 #一、库的管理 18 #1、库的创建 19...

win10 IIS 10.0 无法安装 URL Rewrite Module 重写模块

  打开注册表 win+R 输入 regidit在HKEY_LOCAL_MACHINESOFTWAREMicrosoftInetStp位置 修改注册表 把MajorVersion的值改为9 安装Rewrite Module 恢复注册表 安装完成之后,再把MajorVersion的值改回10 重启一下iis...

关于ZFS、GPT、4K、Geom Label的一些说明

日前重建了之前被玩坏的zfs,碰到几个概念,查了很多资料,理清一下一、关于硬盘分区表目前常用的有两种分区表,MBR和GPT,但是严格来说ZFS并不需要任何一种分区表,ZFS可以直接管理裸盘,换句话说,zfs对硬盘有自己的管理方式,不需要经过MBR或者GPT这一层二、Geom Label和GPT分区表冲突的问题Geom Label使用glabel命令创建,会...

excel文件使用navicat工具导入mysql的方法

1、在excel文件的sheet上,第1行下面插入一行,对应DB里面的字段名称,方便后面导入时做字段匹配: 2、使用Navicat ,打开工具,选择表所在的数据库,然后点击数据库名字,右键Tables,出来下拉菜单选择import wizard(中文版:导入向导).弹出一个选择界面,选择excel file文件 3、点击next(下一步),选择对应...

HyperLedger/Fabric JAVA-SDK with 1.1

HyperLedger/Fabric JAVA-SDK with 1.1 该项目可直接在github上访问。 该项目介绍如何使用fabric-sdk-java框架,基于fabric-sdk-java v1.1正式版开发,可向下兼容1.0版本。 该项目没有对原JAVA-SDK做修改,主要是结合HyperLedger Fabric与fabric-sdk-jav...