postgresql的psql經常使用命令-4

psql是PostgreSQL的一個命令行交互式客戶端工具html

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/bashgit

2.登錄
[root@localhost ~]# su - postgres
-bash-4.1$ psql #鏈接到數據庫服務器,能夠在其中輸入相應的SQL語句或者psql的命令,psql的命令都以\開始
psql (9.6.3)
Type "help" for help.sql

3.查看幫助
postgres=# \? #能夠看到全部的psql的可執行命令,**請注意該命令顯示的是psql客戶端可以使用的命令**
General
\copyright 顯示 PostgreSQL 的使用和發行條款
\errverbose 儘量的顯示全部當前的錯誤信息
\g [FILE] or ; 執行查詢命令 (而且把結果輸出到文件或者管道|)
\gexec 執行查詢命令, 而後在結果中顯示執行完成的全部的值
\gset [PREFIX] 執行查詢命令並把結果存儲在psql變量中
\q 退出psql
\crosstabview [COLUMNS] 執行插敘命令並把結果顯示在交叉表中
\watch [SEC] 每SEC秒執行查詢命令shell

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

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

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.htmlhttp://blog.csdn.net/jpzhu16/article/details/51931357

相關文章
相關標籤/搜索