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)
|
