通过psql命令查看PostgreSQL系统对象

本文介绍了PostgreSQL系统对象概念,阐述了Catalog是sql环境中schema的命名集合,创建表时表定义会存储在系统目录。还说明了数据库系统对象范围对比,重点讲解了使用psql命令查看表和索引信息,并给出了psql命令参考列表。

本文介绍PostgreSQL系统对象概念,并使用psql命令查看其信息。

1. 概述

Catalog(目录) 是sql环境中schema(模式)的命名集合。当创建表时,PostgreSQL 存储表定义在系统目录中。系统目录是PostgreSQL 表的集合。我们像对待普通表一样查询这些系统表,但有更简便的方式查看表和索定义。

数据库系统对象范围对比:

Cluster > Catalog > Schema > Table > Columns & Rows

对应图示如下:

一个服务器上可有一个或多个集群.
数据库服务是一个集群.
集群有目录. ( 目录 = 数据库 )
目录包括模式(schema). (Schema 即表的命名空间, 安全边界)
模式包括多个表.
表有多行.
行通过列存储多个值.

2. 通过psql命令查看系统对象

2.1 查看表信息

通过\d查看表信息,\dS+返回更多信息:

postgres=# \d

          List of relations
 Schema |       Name        |   Type   |  Owner   
--------+-------------------+----------+----------
 public | batch             | table    | postgres
 public | cellphones        | table    | postgres
 public | cellphones_id_seq | sequence | postgres
 public | distributorc      | table    | u8

postgres=# \dS+
                                       List of relations
   Schema   |              Name               |   Type   |  Owner   |    Size    | Description 
------------+---------------------------------+----------+----------+------------+-------------
 pg_catalog | pg_views                        | view     | postgres | 0 bytes    | 
 public     | batch                           | table    | postgres | 8192 bytes | 
 public     | cellphones                      | table    | postgres | 0 bytes    | 
 public     | cellphones_id_seq               | sequence | postgres | 8192 bytes | 
 public     | distributorc                    | table    | u8       | 0 bytes    | 
 public     | distributors                    | table    | u8       | 0 bytes    | 
 public     | distributorsct                  | table    | u8       | 0 bytes    | 

查看特定表信息:

postgres=# \d batch
          Table "public.batch"
   Column   |     Type      | Modifiers 
------------+---------------+-----------
 batch_id   | integer       | 
 batch_name | character(35) | 
 members    | integer       | 
Indexes:
    "mul_uni_key" UNIQUE CONSTRAINT, btree (batch_id, members)

2.2 查看索引信息

通过\di 查看索引信息:

postgres=# \di
                         List of relations
 Schema |          Name           | Type  |  Owner   |    Table     
--------+-------------------------+-------+----------+--------------
 public | cellphones_pkey         | index | postgres | cellphones
 public | code_title              | index | u8       | primtab
 public | distributors_pkey       | index | u8       | distributors
 public | mobiles_pkey            | index | postgres | mobiles
 public | mul_uni_key             | index | postgres | batch
postgres=# \diS+
                                                      List of relations
   Schema   |                  Name                   | Type  |  Owner   |          Table          |    Size    | Description 
------------+-----------------------------------------+-------+----------+-------------------------+------------+-------------
 pg_catalog | pg_user_mapping_user_server_index       | index | postgres | pg_user_mapping         | 8192 bytes | 
 .
 .
 .
 public     | cellphones_pkey                         | index | postgres | cellphones              | 8192 bytes | 
 public     | code_title                              | index | u8       | primtab                 | 8192 bytes | 
 public     | distributors_pkey                       | index | u8       | distributors            | 8192 bytes | 
 public     | mobiles_pkey                            | index | postgres | mobiles                 | 16 kB      | 
 public     | mul_uni_key                             | index | postgres | batch                   | 16 kB      | 
 public     | pkey                                    | index | postgres | postgres3               | 16 kB      | 
 public     | postgres1_order_no_key                  | index | postgres | postgres1               | 16 kB      | 
 public     | postgres_ord_no_key                     | index | postgres | postgres                | 8192 bytes | 
 public     | prikey                                  | index | u8       | films                   | 8192 bytes | 
 public     | production                              | index | u8       | k                       | 8192 bytes | 
 public     | students_student_id_key                 | index | postgres | students                | 16 kB      | 
 public     | uni_constraint                          | index | postgres | cellphones              | 8192 bytes | 
 public     | uni_constraint2                         | index | postgres | mobiles                 | 16 kB      | 
 public     | uni_index                               | index | postgres | cellphones              | 8192 bytes | 
 public     | uni_key                                 | index | postgres | india                   | 8192 bytes | 

查询特定索引信息:

postgres=# \diS+ uni_key
                           List of relations
 Schema |  Name   | Type  |  Owner   | Table |    Size    | Description 
--------+---------+-------+----------+-------+------------+-------------
 public | uni_key | index | postgres | india | 8192 bytes | 
(1 row)

3. psql命令参考

上面介绍一些常用命令,下面给出命令参考列表。

命令描述
\dd?object-nameDisplay comments for?object-name
\dbList all tablespaces
\dnList all schemas
\d_\dtList all tables
\diList all indexes
\dsList all sequences
\dvList all views
\dSList all PostgreSQL-defined tables
\d table-nameShow table definition
\d index-nameShow index definition
\d view-nameShow view definition
\d sequence-nameShow sequence definition
\dpList all privileges
\dlList all large objects
\daList all aggregates
\dfList all functions
\dcList all conversions
\dCList all casts
\df function-nameList all functions with given name
\doList all operators
\do operator-nameList all operators with given name
\dTList all types
\dDList all domains
\dgList all groups
\duList all users
\lList all databases in this cluster
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值