ostgreSQL-psql常用命令

文章索引

[隐藏]

\d命令

 
1
2
3
格式:
\d [ pattern ]
\d [ pattern ] +

该命令将显示每个匹配关系(表,视图,索引,序列)的信息,包括对象所有的列,它们的类型,表空间(如果不是默认的)和任何特殊属性(如NOT NULL或默认值等)。与唯一约束相关的索引,规则,约束,触发器也同样会显示出来。如果关系是一个视图,还会显示视图的定义。
1.如果\d命令什么都不带,将列出当前数据库中的所有表。

 
1
2
3
4
5
6
7
8
9
10
11
12
sample_db=# \d
             List of relations
Schema |      Name      | Type  |  Owner  
--------+----------------+-------+---------
public | account        | table | postgre
public | book           | table | postgre
public | customers      | table | postgre
public | fruits         | table | postgre
...
public | view_t         | view  | postgre
public | view_t2        | view  | postgre
(42 rows)

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

 
1
2
3
4
5
6
7
8
9
10
11
12
sample_db=# \d tb_dept2
           Table "public.tb_dept2"
  Column  |         Type          | Modifiers
----------+-----------------------+-----------
id       | integer               | not null
name     | character varying(22) |
location | character varying(50) |
Indexes:
    "tb_dept2_pkey" PRIMARY KEY, btree (id)
    "tb_dept2_name_key" UNIQUE CONSTRAINT, btree (name)
Referenced by:
    TABLE "tb_tmp" CONSTRAINT "fk_emp_dept" FOREIGN KEY (deptid) REFERENCES tb_dept2(id)

3.\d也可以显示索引信息

 
1
2
3
4
5
6
sample_db=# \d tb_dept2_pkey
Index "public.tb_dept2_pkey"
Column |  Type   | Definition
--------+---------+------------
id     | integer | id
primary key, btree, for table "public.tb_dept2"

4.\d后面可以跟一通配符"*"或"?"

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
sample_db=# \d boo?
                  Table "public.book"
      Column      |          Type          | Modifiers
------------------+------------------------+-----------
bookid           | integer                | not null
bookname         | character varying(255) | not null
authors          | character varying(255) | not null
info             | character varying(255) |
comment          | character varying(255) |
year_publication | date                   | not null
Indexes:
    "uniqididx" UNIQUE, btree (bookid)
    "bkcmtidx" btree (comment)
    "bknameidx" btree (bookname)
sample_db=# \d a*
       Table "public.account"
Column |     Type      | Modifiers
--------+---------------+-----------
id     | integer       |
name   | character(20) |
Triggers:
    account_stamp BEFORE INSERT ON account FOR EACH ROW EXECUTE PROCEDURE account_stam()

5.\d+命令,该命令将显示比\d命令更详细的信息,除了前面介绍的那些,它还会显示任何与表列关联的注释,以及表中出现的ODI。

 
1
2
3
4
5
6
7
sample_db=# \d+ t
                           Table "public.t"
  Column  |  Type   | Modifiers | Storage | Stats target | Description
----------+---------+-----------+---------+--------------+-------------
quantity | integer |           | plain   |              |
price    | integer |           | plain   |              |
Has OIDs: no

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

 
1
2
3
4
5
如果想只显示匹配的表,可以使用\dt命令
如果想只显示索引,可以使用\di命令
如果想只显示序号,可以使用\ds命令
如果想只显示视图,可以使用\dv命令
如果想只显示函数,可以使用\df命令

7.如果想显示SQL已执行的时间,可以用\timing命令

 
1
2
3
4
5
6
7
8
9
sample_db=# \timing on
Timing is on.
sample_db=# select count(*) from t;
count
-------
     1
(1 row)
 
Time: 0.348 ms

8.列出所有的schemas可以使用\dn命令

 
1
2
3
4
5
6
sample_db=# \dn
List of schemas
  Name  |  Owner  
--------+---------
public | postgre
(1 row)

9.显示所有的表空间可以用\db命令

 
1
2
3
4
5
6
7
sample_db=# \db
       List of tablespaces
    Name    |  Owner  | Location
------------+---------+----------
pg_default | postgre |
pg_global  | postgre |
(2 rows)

表空间就是对一个目录,放在这个表空间的表,就是把表的数据文件放到这个表空间下。
10.列出数据库所有角色或用户\du或\dg

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
sample_db=# \dg
                             List of roles
Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
bob       |                                                | {}
linux78   |                                                | {}
post4     |                                                | {}
post5     | Superuser, Cannot login                        | {}
post6     | Create DB, Cannot login                        | {}
post7     | Create role, Cannot login                      | {}
post8     | Cannot login                                   | {}
postgre   | Superuser, Create role, Create DB, Replication | {}
 
sample_db=# \du
                             List of roles
Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
bob       |                                                | {}
linux78   |                                                | {}
post4     |                                                | {}
post5     | Superuser, Cannot login                        | {}
post6     | Create DB, Cannot login                        | {}
post7     | Create role, Cannot login                      | {}
post8     | Cannot login                                   | {}
postgre   | Superuser, Create role, Create DB, Replication | {}

11.\dp或\z命令用于显示表的权限分配情况

 
 
1
2
3
4
5
6
sample_db=# \dp t
                          Access privileges
Schema | Name | Type  | Access privileges | Column access privileges
--------+------+-------+-------------------+--------------------------
public | t    | table |                   |
(1 row)

postgresql的/d命令的更多相关文章

  1. PostgreSQL增删数据命令示例

    在PostgreSQL中如何用简单的几条SQL语句生成大量的测试数据呢? 此处,我简单的写一个例子,供参考(在Postgresql9.1下面做的): (1)生成一万条测试数据的表foo mydb=# ...

  2. linux下操作PostgreSQL的常用命令

    一般性: \copyright 显示PostgreSQL的使用和发行许可条款          \g [文件] or; 执行查询 (并把结果写入文件或 |管道) \h [名称] SQL命令语法上的说明 ...

  3. PostgreSql的Explain命令详解

    http://toplchx.iteye.com/blog/2091860 使用EXPLAIN PostgreSQL为每个收到的查询设计一个查询规划.选择正确的匹配查询结构和数据属性的规划对执行效率是 ...

  4. Postgresql数据库实用命令

    Postgresql 命令 pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start 启动数据库 cr ...

  5. Postgresql的一些命令

    显示所有数据表: \dt 显示表结构:  \d YOUR_TABLE 进入数据库: psql DATABASE_NAME 显示所有数据库: \list 退出: \q 删除数据库: dropdb DAT ...

  6. postgresql的psql命令

    1:不进入数据库而执行SQL命令,用参数-c 2:把SQL命令保存在一个外部文件中,用 -f 参数导入并执行 a1.txt文件内容 select * from student; 在shell中用如下命 ...

  7. PostgreSQL常用查看命令

    1. 查看当前库sehcma大小,并按schema排序 SELECT schema_name, pg_size_pretty(sum(table_size)::bigint) as "dis ...

  8. postgresql数据库常用命令

    --获取数据库软件版本select version();--获取数据库启动时间select pg_postmaster_start_time();--获取配置文件最近load时间select pg_c ...

  9. PostgreSQL增强版命令行客户端(pgcli)

    效果: 安装: https://www.pgcli.com/install 官网: https://www.pgcli.com/

随机推荐

  1. PC110304/UVA850

    这题目WA了好几次,主要是我没有理解清楚No solution.这个情况. 如果在match原文做好了,基本map一下就能过了. 与原句match的条件就是: 1.出现了26个字母 2.该空格的地方要 ...

  2. PowerDesigner 的几个使用技巧

    1.  生成sql脚本 Database→Generate Database 选择要输出的文件路径,即文件存储路径,并根据需要修改文件名,单击确定后便会生成sql脚本.   在Options选项卡里, ...

  3. LICAppInfo

    [Common] Enabled=true All=true [AppList] #AppName                       #Enabled            #Validit ...

  4. ueditor富文本编辑在 asp.net MVC下使用步骤

    mvc项目中用到了这个富文本编辑就试着把遇到的问题个使用步骤在这里记录一下,希望大家少走弯路. 1.首先我们先下载net版本的uediot r.

  5. Git关联远程GitHub仓库

    一.本地安装GIT版本控制软件 二.配置Git,设置用户信息 git config --global user.name "jack" git config --global us ...

  6. Warning File `.depend' has modification time 1.6 s in the future

    一.前提知识 主机时间与虚拟机时间不同步所致.我们在某一操作系统所属磁盘空间下创建一个文件,那么这个文件的创建时间是以磁盘所属的操作系统的时钟为基准的. 我们假设主机windows的系统时间是10:0 ...

  7. typedef与define

    一.typedef用法 typedef常用来定义一个标识符及关键字的别名,它生效是在语言编译过程,但它并不实际分配内存空间.typedef可以增强程序的可读性,以及标识符的灵活性,但它也有“非直观性” ...

  8. mysql update from 子查询

    mssql 子查询更新 update log set uin= b.uinfrom log a,logs bwhere a.accountuin = b.accountuin mysql 不支持 up ...

  9. 安装linux系统后要做的事情

    基本安装0 http://www.kali.org.cn/thread-20517-1-1.html 基本安装1 http://defcon.cn/1618.html 基本安装2 http://www ...

  10. IbatisNet开发使用小结

    一.   介绍 平常做企业级应用,需求变化是经常的事,而很多基础代码重复也是很让人头疼的问题.所以很多人会使用一些ORM框架来增强项目的可维护性.可扩展性.IBatis.Net就是一个比较易用的ORM ...