Skip to content
\d命令的格式如下:

   \d [ pattern ]

   \d [ pattern ] +

   该命令用于显示每个匹配关系(表,视图,索引,序列)的信息
   
如果\d 后面什么都不带,会显示数据库所有的表,视图,索引,序列

\d后面可以跟视图

\d+命令显示比\d更多的信息包括表和列的注释等相关信息

 匹配不同对象类型的\d命令

           \dt   显示匹配的表

           \di   显示匹配的索引

           \ds  显示匹配的序列

           \dv  显示匹配的视图

           \df   显示匹配的函数
\d命令的格式如下:

   \d [ pattern ]

   \d [ pattern ] +

   该命令用于显示每个匹配关系(表,视图,索引,序列)的信息
   
如果\d 后面什么都不带,会显示数据库所有的表,视图,索引,序列

\d后面可以跟视图

\d+命令显示比\d更多的信息包括表和列的注释等相关信息

 匹配不同对象类型的\d命令

           \dt   显示匹配的表

           \di   显示匹配的索引

           \ds  显示匹配的序列

           \dv  显示匹配的视图

           \df   显示匹配的函数

1.查看数据库

[ptgres@beta ~]$ psql -U postgres -h 127.0.0.1  -p 5532
psql (12.2)
Type "help" for help.

postgres=# \l
postgres=# \l
                                    List of databases
      Name       |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------------+----------+----------+------------+------------+-----------------------
 advertisedb     | postgres | UTF8     | en_US.UTF8 | en_US.UTF8 | 
 agile           | postgres | UTF8     | en_US.UTF8 | en_US.UTF8 | 
 eidu            | postgres | UTF8     | en_US.UTF8 | en_US.UTF8 |
[ptgres@beta ~]$ psql -U postgres -h 127.0.0.1  -p 5532
psql (12.2)
Type "help" for help.

postgres=# \l
postgres=# \l
                                    List of databases
      Name       |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------------+----------+----------+------------+------------+-----------------------
 advertisedb     | postgres | UTF8     | en_US.UTF8 | en_US.UTF8 | 
 agile           | postgres | UTF8     | en_US.UTF8 | en_US.UTF8 | 
 eidu            | postgres | UTF8     | en_US.UTF8 | en_US.UTF8 |

2.查看表

postgres=# \c han
You are now connected to database "han" as user "postgres".

han=# \d
        List of relations
 Schema | Name | Type  |  Owner   
--------+------+-------+----------
 public | you  | table | postgres
(1 row)
postgres=# \c han
You are now connected to database "han" as user "postgres".

han=# \d
        List of relations
 Schema | Name | Type  |  Owner   
--------+------+-------+----------
 public | you  | table | postgres
(1 row)

3.查看表结构

\d后面跟一个表名,表示显示这个表的结构定义

han=# \d you
                     Table "public.you"
 Column |       Type        | Collation | Nullable | Default 
--------+-------------------+-----------+----------+---------
 name11 | character varying |           |          | 
 fdf    | character varying |
han=# \d you
                     Table "public.you"
 Column |       Type        | Collation | Nullable | Default 
--------+-------------------+-----------+----------+---------
 name11 | character varying |           |          | 
 fdf    | character varying |

4.查看索引名字

\d 后跟一个索引,可以显示索引的信息
\d 后跟一个索引,可以显示索引的信息

5.模糊查询

\d后面跟通配符如“*” 或“?”

han=# \d y*
                     Table "public.you"
 Column |       Type        | Collation | Nullable | Default 
--------+-------------------+-----------+----------+---------
 name11 | character varying |           |          | 
 fdf    | character varying |           |          |
han=# \d y*
                     Table "public.you"
 Column |       Type        | Collation | Nullable | Default 
--------+-------------------+-----------+----------+---------
 name11 | character varying |           |          | 
 fdf    | character varying |           |          |

6.查看表大小

\d+命令显示比\d更多的信息包括表和列的注释等相关信息

han=# \d+
                   List of relations
 Schema | Name | Type  |  Owner   | Size  | Description 
--------+------+-------+----------+-------+-------------
 public | you  | table | postgres | 16 kB | 
(1 row)
han=# \d+
                   List of relations
 Schema | Name | Type  |  Owner   | Size  | Description 
--------+------+-------+----------+-------+-------------
 public | you  | table | postgres | 16 kB | 
(1 row)

7.显示执行时间

可以使用\timing on 命令开启计时功能,如果要关闭,使用\timing off关闭计时功能

han=# \timing on
Timing is on.

han=# \d+
                   List of relations
 Schema | Name | Type  |  Owner   | Size  | Description 
--------+------+-------+----------+-------+-------------
 public | you  | table | postgres | 16 kB | 
(1 row)

han=# select * from you ;
 name11 |   fdf   
--------+---------
 han    | student
(1 row)

Time: 0.479 ms
han=# \timing on
Timing is on.

han=# \d+
                   List of relations
 Schema | Name | Type  |  Owner   | Size  | Description 
--------+------+-------+----------+-------+-------------
 public | you  | table | postgres | 16 kB | 
(1 row)

han=# select * from you ;
 name11 |   fdf   
--------+---------
 han    | student
(1 row)

Time: 0.479 ms

8.列出所有schema

han=# \dn
  List of schemas
  Name  |  Owner   
--------+----------
 public | postgres
(1 row)
han=# \dn
  List of schemas
  Name  |  Owner   
--------+----------
 public | postgres
(1 row)

9.列出所有表空间

han=# \db
       List of tablespaces
    Name    |  Owner   | Location 
------------+----------+----------
 pg_default | postgres | 
 pg_global  | postgres | 
(2 rows)
han=# \db
       List of tablespaces
    Name    |  Owner   | Location 
------------+----------+----------
 pg_default | postgres | 
 pg_global  | postgres | 
(2 rows)

10.列出所有用户

han=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

#或者

han=# \dg
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
han=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

#或者

han=# \dg
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

11.权限分配情况

\dp或者\z

\dp talbe_name
\dp talbe_name

12.执行文件中的命令

一般\i用于执行sql文件的脚本
一般\i用于执行sql文件的脚本

13.指定字符集编译的命令

当客户端的字符编码与服务器不一致时,可能出现乱码现象,可以使用\encoding命令指定客户端的字符编码。

    比如:

   \encoding gbk    设置客户端的字符编码为gbk.

   \encoding utf8    设置客户端的字符编码为uft8
当客户端的字符编码与服务器不一致时,可能出现乱码现象,可以使用\encoding命令指定客户端的字符编码。

    比如:

   \encoding gbk    设置客户端的字符编码为gbk.

   \encoding utf8    设置客户端的字符编码为uft8

14.pset 命令

 \pset命令用于设置输出的格式

    \pset border 0:表示输出内容无边框

    \pset border 1:表示边框只在内部存在

    \pset border 2:表示内外都有边框
 \pset命令用于设置输出的格式

    \pset border 0:表示输出内容无边框

    \pset border 1:表示边框只在内部存在

    \pset border 2:表示内外都有边框

15.x命令

使用\x命令,可以把表中的每一行的每列数据都拆分为单行展示,这种适合列比较多,界面显示不全的情况下比较有用

通过使用? 可以获取到更多的命令的解释