PSQL工具提示

來源:互聯網
上載者:User

標籤:建立   exists   func   event   tin   mod   edit   page   執行時間   

psql功能強大,技巧怎麼用只能後續慢慢補充了,看一個工具先看怎麼用help
$ psql #串連到資料庫伺服器,可以在其中輸入相應的SQL語句或者psql的命令,psql的命令都以\開始
$ \l #列出所有的資料庫
$ \d #查看錶的結構
$ \d 表名 #查看指定表的具體結構
$ create database 新資料庫名 #建立新的資料庫
$ \c 已經有的資料庫名 #切換當前資料庫到指定的資料庫
$ psql -h 主機名稱/ip地址 -p 連接埠號碼 資料庫名稱 使用者名稱稱 #串連到指定的資料庫
$ \d #顯示所有的表
$ \d 表名 #顯示指定的表的結構
$ \d 索引名 #顯示指定索引的結構
$ \d 後跟萬用字元或者?等 #?代表匹配一個 代表匹配多個,如\d s*會匹配以s開頭後跟任意個字元的表或者索引並顯示出來,\d s?會匹配以s開頭後跟一個字元的表或者索引並顯示出來
$ \d+ #該命令會比\d命令顯示更詳細的資訊,除了前面介紹的那些,它還會顯示任何與表相關聯的注釋,以及表中出現的OID,\d+可以代替之前的任意以\d開頭的命令
$ \dt #只顯示表
$ \di #只顯示所有的索引
$ \ds #只顯示序列
$ \dv #只顯示視圖
$ \df #只顯示函數
$ \timing on #顯示SQL的執行時間
$ \timing off #關閉顯示SQL的執行時間
$ \dn #顯示所有的schema
$ \db #顯示所有的資料表空間
$ \du 或者 \dg #顯示資料庫中的所有角色和使用者
$ \dp 或者 \z 表名 #顯示表的許可權分配情況
$ \encoding utf8 #當用戶端的字元編碼和伺服器的不一樣時,可能會顯示亂碼,可以使用\encoding命令來指定用戶端的字元編碼,如使用\encoding utf8來指定用戶端的編碼方式為utf8
\pset命令用於指定輸出的格式,具體如下:
\pset border 0 : 表示輸出內容物邊框
\pset border 1 : 表示邊框只在內部,預設情況下採用的是該條命令
\pset border 2 : 表示內外都存在邊框
曆史命令與補全功能
使用上下鍵可以查看曆史命令
兩次tab鍵可以實現命令補全或者給出輸入提示
在psql中事務是自動認可的,比方說,執行完一條create table語句之後,事務就會被自動的提交,如果不想自動的提交,方法有兩種:
方案一:運行begin命令,然後執行相應的SQL語句,最後在執行commit或者rollback語句
方案二:直接使用psql中的命令關閉自動認可的功能,\set AUTOCOMMIT off ,該命令設定自動認可關閉
實際上,MySQL等資料庫也是這樣的情況,當我們不使用顯示的事務語句時,每一條SQL語句都被作為一個事務,或者說資料庫內部將每一個SQL語句封裝為一個事務,在PostgreSQL中仍然是這樣的,平時使用時要注意。
得到psql中真實執行的SQL語句
aoldbs=# help
You are using psql, the command-line interface to PostgreSQL.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
aoldbs=#

eg:查看SQL commands
aoldbs=# \h drop table
Command: DROP TABLE
Description: remove a table
Syntax:
DROP TABLE [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]

aoldbs=#



\?查看PSQL命令

General
\copyright show PostgreSQL usage and distribution terms
\g [FILE] or ; execute query (and send results to file or |pipe)
\gset [PREFIX] execute query and store results in psql variables
\q quit psql
\watch [SEC] execute query every SEC seconds

Help
\? [commands] show help on backslash commands
\? options show help on psql command-line options
\? variables show help on special variables
\h [NAME] help on syntax of SQL commands, * for all commands

Query Buffer
\e [FILE] [LINE] edit the query buffer (or file) with external editor
\ef [FUNCNAME [LINE]] edit function definition with external editor
\p show the contents of the query buffer
\r reset (clear) the query buffer
\w FILE write query buffer to file

Input/Output
\copy ... perform SQL COPY with data stream to the client host
\echo [STRING] write string to standard output
\i FILE execute commands from file
\ir FILE as \i, but relative to location of current script
\o [FILE] send all query results to file or |pipe
\qecho [STRING] write string to query output stream (see \o)

Informational
(options: S = show system objects, + = additional detail)
\d[S+] list tables, views, and sequences
\d[S+] NAME describe table, view, sequence, or index
\da[S] [PATTERN] list aggregates
\db[+] [PATTERN] list tablespaces
\dc[S+] [PATTERN] list conversions
\dC[+] [PATTERN] list casts
\dd[S] [PATTERN] show object descriptions not displayed elsewhere
\ddp [PATTERN] list default privileges
\dD[S+] [PATTERN] list domains
\det[+] [PATTERN] list foreign tables
\des[+] [PATTERN] list foreign servers
\deu[+] [PATTERN] list user mappings
\dew[+] [PATTERN] list foreign-data wrappers
\df[antw][S+] [PATRN] list [only agg/normal/trigger/window] functions
\dF[+] [PATTERN] list text search configurations
\dFd[+] [PATTERN] list text search dictionaries
\dFp[+] [PATTERN] list text search parsers
\dFt[+] [PATTERN] list text search templates
\dg[+] [PATTERN] list roles
\di[S+] [PATTERN] list indexes
\dl list large objects, same as \lo_list
\dL[S+] [PATTERN] list procedural languages
\dm[S+] [PATTERN] list materialized views
\dn[S+] [PATTERN] list schemas
\do[S] [PATTERN] list operators
\dO[S+] [PATTERN] list collations
\dp [PATTERN] list table, view, and sequence access privileges
\drds [PATRN1 [PATRN2]] list per-database role settings
\ds[S+] [PATTERN] list sequences
\dt[S+] [PATTERN] list tables
\dT[S+] [PATTERN] list data types
\du[+] [PATTERN] list roles
\dv[S+] [PATTERN] list views
\dE[S+] [PATTERN] list foreign tables
\dx[+] [PATTERN] list extensions
\dy [PATTERN] list event triggers
\l[+] [PATTERN] list databases
\sf[+] FUNCNAME show a function‘s definition
\z [PATTERN] same as \dp

Formatting
\a toggle between unaligned and aligned output mode
\C [STRING] set table title, or unset if none
\f [STRING] show or set field separator for unaligned query output
\H toggle HTML output mode (currently off)
\pset [NAME [VALUE]] set table output option
(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] show only rows (currently off)
\T [STRING] set HTML <table> tag attributes, or unset if none
\x [on|off|auto] toggle expanded output (currently off)

Connection
\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}
connect to new database (currently "aoldbs")
\encoding [ENCODING] show or set client encoding
\password [USERNAME] securely change the password for a user
\conninfo display information about current connection

Operating System
\cd [DIR] change the current working directory
\setenv NAME [VALUE] set or unset environment variable
\timing [on|off] toggle timing of commands (currently off)
! [COMMAND] execute command in shell or start interactive shell

Variables
\prompt [TEXT] NAME prompt user to set internal variable
\set [NAME [VALUE]] set internal variable, or list all if no parameters
\unset NAME unset (delete) internal variable

Large Objects
\lo_export LOBOID FILE
\lo_import FILE [COMMENT]
\lo_list
\lo_unlink LOBOID large object operations

PSQL工具提示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.