psql是PostgreSQL的一個命令行交互式客戶端工具
1. 查看postgresql賬號
[root@localhost ~]#cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
2.登陸
[root@localhost ~]# su - postgres
-bash-4.1$ psql #連接到數據庫服務器,可以在其中輸入相應的SQL語句或者psql的命令,psql的命令都以\開始
psql (9.6.3)
Type "help" for help.
3.查看幫助
postgres=# \? #可以看到所有的psql的可執行命令,**請注意該命令顯示的是psql客戶端能夠使用的命令**
General
\copyright 顯示 PostgreSQL 的使用和發行條款
\errverbose 盡可能的顯示所有當前的錯誤信息
\g [FILE] or ; 執行查詢命令 (並且把結果輸出到文件或者管道|)
\gexec 執行查詢命令, 然后在結果中顯示執行完成的所有的值
\gset [PREFIX] 執行查詢命令並把結果存儲在psql變量中
\q 退出psql
\crosstabview [COLUMNS] 執行插敘命令並把結果顯示在交叉表中
\watch [SEC] 每SEC秒執行查詢命令
Help
\? [commands] 顯示反斜杠命令的幫助
\? options 顯示在psql命令行選項的幫助
\? variables 顯示某個變量的幫助
\h [NAME] SQL 命令語法上的說明,用 * 顯示全部命令
Query Buffer
\e [FILE] [LINE] edit the query buffer (or file) with external editor
\ef [FUNCNAME [LINE]] edit function definition with external editor
\ev [VIEWNAME [LINE]] edit view definition with external editor
\p show the contents of the query buffer
\r reset (clear) the query buffer
\s [FILE] display history or save it to file
\w FILE 將查詢緩存區寫入檔案
Input/Output
\copy ... 執行 SQL COPY,數據流指向客戶端主機
\echo [STRING] 將字符串寫到標准輸出流
\i FILE 從文件中執行命令
\ir FILE 與\i一樣, 但是相對於當前腳本的位置
\o [FILE] 把所有查詢結果輸出到文件或者管道|
\qecho [STRING] 把字符串寫到查詢輸出流 (see \o)
Informational
(options: S = show system objects, + = additional detail)
\d[S+] 顯示所有的 tables, views, and sequences
\d[S+] NAME 顯示指定名字的 table, view, sequence, or index 的表結構
\da[S] [PATTERN] 顯示所有聚合函數
\dA[+] [PATTERN] 顯示所有訪問方法
\db[+] [PATTERN] 顯示所有表空間
\dc[S+] [PATTERN] 顯示字元編碼轉換
\dC[+] [PATTERN] 顯示所有型別轉換
\dd[S] [PATTERN] 顯示所有物件的注解
\ddp [PATTERN] 顯示所有默認權限
\dD[S+] [PATTERN] 顯示所有共同值域
\det[+] [PATTERN] 顯示所有外部表
\des[+] [PATTERN] 顯示所有遠程服務器
\deu[+] [PATTERN] 顯示所有遠程服務器用戶
\dew[+] [PATTERN] 顯示所有外部數據包裝器
\df[antw][S+] [PATRN] 顯示 [only agg/normal/trigger/window] 函數
\dF[+] [PATTERN] 顯示所有文本搜索配置
\dFd[+] [PATTERN] 顯示所有文本搜索字典
\dFp[+] [PATTERN] 顯示所有文本搜索解析器
\dFt[+] [PATTERN] 顯示所有文本搜索模板
\dg[S+] [PATTERN] 顯示所有角色
\di[S+] [PATTERN] 顯示所有索引
\dl 顯示所有大對象, 與 \lo_list 類似
\dL[S+] [PATTERN] 顯示所有程序語言
\dm[S+] [PATTERN] 顯示所有具體視圖
\dn[S+] [PATTERN] 顯示所有模式
\do[S] [PATTERN] 顯示所有操作符
\dO[S+] [PATTERN] 顯示所有排序規則
\dp [PATTERN] 顯示所有表 視圖 序列 的訪問權限
\drds [PATRN1 [PATRN2]] 顯示每個數據庫的角色設置
\ds[S+] [PATTERN] 顯示所有序列
\dt[S+] [PATTERN] 顯示所有表
\dT[S+] [PATTERN] 顯示所有數據類型
\du[S+] [PATTERN] 顯示所有角色
\dv[S+] [PATTERN] 顯示視圖
\dE[S+] [PATTERN] 顯示所有外部表
\dx[+] [PATTERN] 顯示所有擴展
\dy [PATTERN] 顯示所有事件觸發器
\l[+] [PATTERN] #列出所有的數據庫
\sf[+] FUNCNAME 顯示函數的定義
\sv[+] VIEWNAME 顯示視圖的定義
\z [PATTERN] 與\dp一樣
Formatting
\a 切換不對齊和對齊的輸出模式
\C [STRING] 設置表頭,或者如果是none則不設置
\f [STRING] 顯示或者設置未對齊輸出的字段分隔符
\H 切換HTML輸出模式(默認是off)
\pset [NAME [VALUE]] 設置表的輸出選項
(NAME := {format|border|expanded|fieldsep|fieldsep_zero|footer|null|
numericlocale|recordsep|recordsep_zero|tuples_only|title|tableattr|pager|
unicode_border_linestyle|unicode_column_linestyle|unicode_header_linestyle})
\t [on|off] 只顯示行 (當前off)
\T [STRING] 設置HTML <table> 標簽的屬性, 或者如果是none則不設置
\x [on|off|auto] 切換擴展輸出 (當前默認 off)
Connection
\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo} 連線到新的資料庫 (目前是 "test")
\encoding [ENCODING] 顯示或設定用戶端字元編碼
\password [USERNAME] 修改用戶密碼
\conninfo 顯示當前的連接信息
Operating System
\cd [DIR] 改變目前的工作目錄
\setenv NAME [VALUE] 設置或者取消環境變量
\timing [on|off] 切換命令計時開關 (默認是off)
\! [COMMAND] 執行shell命令 or 打開內部 shell命令
Variables
\prompt [TEXT] NAME 提示用戶設置內部變量
\set [NAME [VALUE]] 設置內部變量, 假如沒有參數則顯示所有參數
\unset NAME 取消(刪除)內部變量
Large Objects
\lo_export LOBOID FILE
\lo_import FILE [COMMENT]
\lo_list
\lo_unlink LOBOID 大對象的操作
4.一些常用命令使用舉例
(1)psql加上-E參數,可以把psql中各種以"\"開頭的命令執行的實際SQL打印出來
-sh-4.1$ psql -E -h /var/opt/gitlab/postgresql -d gitlabhq_production
psql (9.2.18)
Type "help" for help.
gitlabhq_production=# \d
********* QUERY **********
SELECT n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','v','S','f','')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
如果你在使用之后,想立即關閉
postgres=# \set ECHO_HIDDEN off
postgres=# \d
No relations found.
(2)顯示所有表
gitlabhq_production=# \d
List of relations
Schema | Name | Type | Owner
--------+---------------------------------------------+----------+--------
public | abuse_reports | table | gitlab
public | abuse_reports_id_seq | sequence | gitlab
(3)\d后面跟一個表名,表示顯示這個表的結構定義
gitlabhq_production=# \d abuse_reports
Table "public.abuse_reports"
Column | Type | Modifiers
--------------+-----------------------------+------------------------------------------------------------
id | integer | not null default nextval('abuse_reports_id_seq'::regclass)
reporter_id | integer |
user_id | integer |
message | text |
created_at | timestamp without time zone |
updated_at | timestamp without time zone |
message_html | text |
Indexes:
"abuse_reports_pkey" PRIMARY KEY, btree (id)
(4)\d后面可以跟一通配符"*"或"?"
gitlabhq_production=# \d a*
Table "public.abuse_reports"
Column | Type
| Modifiers
--------------+-----------------------------+------------------------------------------------------------
id | integer | not null default nextval('abuse_reports_id_seq'::regclass)
reporter_id | integer |
user_id | integer |
message | text |
created_at | timestamp without time zone |
updated_at | timestamp without time zone |
message_html | text |
Indexes:
"abuse_reports_pkey" PRIMARY KEY, btree (id)
(5)\d+命令,該命令將顯示比\d命令更詳細的信息
gitlabhq_production=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+---------------------------------------------+----------+--------+------------+-------------
public | abuse_reports | table | gitlab | 8192 bytes |
public | abuse_reports_id_seq | sequence | gitlab | 8192 bytes |
public | appearances | table | gitlab | 8192 bytes |
(6)顯示所有表空間
gitlabhq_production=# \db
List of tablespaces
Name | Owner | Location
------------+-------------+----------
pg_default | gitlab-psql |
pg_global | gitlab-psql |
(2 rows)
(7)匹配不同對象類型的\d命令
只顯示匹配的表,可以使用\dt命令
只顯示索引,可以使用\di命令
只顯示序號,可以使用\ds命令
只顯示視圖,可以使用\dv命令
只顯示函數,可以使用\df命令
(8)想顯示SQL已執行的時間,可以用\timing命令
gitlabhq_production=# \timing on
Timing is on.
sample_db=# select count(*) from abuse_reports;
count
-------
69
(69 row)
Time: 3.48 ms
(9)顯示所有用戶或者角色
gitlabhq_production=# \du
List of roles
Role name | Attributes | Member of
-------------------+------------------------------------------------+-----------
gitlab | | {}
gitlab-psql | Superuser, Create role, Create DB, Replication | {}
gitlab_replicator | Replication | {}
gitlabhq_production=# \dg
List of roles
Role name | Attributes | Member of
-------------------+------------------------------------------------+-----------
gitlab | | {}
gitlab-psql | Superuser, Create role, Create DB, Replication | {}
gitlab_replicator | Replication | {}
(10)\dp或\z命令用於顯示表的權限分配情況
gitlabhq_production=# \dp abuse_reports
Access privileges
Schema | Name | Type | Access privileges | Column access privileges
--------+---------------+-------+-------------------+--------------------------
public | abuse_reports | table | |
(1 row)
gitlabhq_production=# \z abuse_reports
Access privileges
Schema | Name | Type | Access privileges | Column access privileges
--------+---------------+-------+-------------------+--------------------------
public | abuse_reports | table | |
(1 row)
(10)\x命令-可以把表中的每一行的每列數據都拆分為單行展示
gitlabhq_production=# \x
Expanded display is on.
gitlabhq_production=# \dp
Access privileges
-[ RECORD 1 ]------------+--------------------------------------------
Schema | public
Name | abuse_reports
Type | table
Access privileges |
Column access privileges |
(11)當客戶端的字符編碼和服務器的不一樣時,可能會顯示亂碼,可以使用\encoding命令來指定客戶端的字符編碼,如使用\encoding utf8來指定客戶端的編碼方式為utf8
gitlabhq_production=# \encoding utf8
(12)\pset命令
\pset命令用於指定輸出的格式,具體如下:
\pset border 0 : 表示輸出內容物邊框
\pset border 1 : 表示邊框只在內部,默認情況下采用的是該條命令
\pset border 2 : 表示內外都存在邊框
(13)\i <SQL文件的路徑>
\i <SQL文件的路徑>可以在pg中執行外部的SQL語句,這樣方便我們執行很復雜的SQL語句。在MySQL中也存在類似的功能,但是實現的方式不一樣,在MySQL中執行存儲在外部文件中的SQL命令的方式:source <SQL文件的全路徑> 或者 \. <SQL文件的全路徑>
參考:
http://www.cnblogs.com/wangkangluo1/archive/2012/05/29/2523654.html
http://blog.csdn.net/jpzhu16/article/details/51931357