從Psql的元素周期表來看psql命令如何使用

2019-11-16     波波說運維

概述

今天主要分享下psql的元素周期表,下面一起來看看吧~


一、psql優點

psql 可謂是最強大的資料庫命令行客戶端,具備以下優點:

  • 支持豐富的LIST對象信息的功能(包括資料庫、權限、類型、聚合、域、轉換、外部表、外部server、大對象、全文檢索、邏輯訂閱、插件、事件觸發器….等資料庫所有對象),
  • 支持編程
  • 支持query buffer管理
  • 支持IO操作
  • 其他,支持格式、變量、作業系統命令等。
  • 支持行列變換。
  • 支持大對象操作。

二、psql元素周期表


三、psql 命令詳情

postgres=# \\?


四、實例

1、列出psql 幫助

 \\? [commands] show help on backslash commands 
\\? options show help on psql command-line options
\\? variables show help on special variables

2、列出SQL COMMAND語法幫助

\\h [NAME] help on syntax of SQL commands, * for all commands

3、對查詢結果進行行列變換

\\crosstabview [ colV [ colH [ colD [ sortcolH ] ] ] ]

Executes the current query buffer (like \\g) and shows the results in a crosstab grid.

create table abc (uid int, class text, score float); 
insert into abc select random()*100, (array['語文','數學','英語','物理','化學'])[ceil(random()*5)::int], round((random()*100)::numeric,0) from generate_series(1,500);
select * from abc limit 10;
select class, level, count(*) from (select *,width_bucket(score,0,101,5) as level from abc) t group by 1,2 order by 1,2;

--指定X軸、Y周進行變換,執行前一條SQL(當前query buffer的SQL)。
postgres=# \\crosstabview level class

4、列印當前query buffe

\\p

\\w FILE write query buffer to file

postgres=# select 1; 
postgres=# \\p
--將QUERY BUFFER寫入文件
postgres=# \\w '/tmp/abc'
--執行作業系統命令 \\!
postgres=# \\! cat '/tmp/abc'

5、列印所有query buffer(實際上是.psql_history)

\\s [FILE] display history or save it to file


psql還有很多其他用法,篇幅有限,就介紹到這了,覺得有用的朋友多幫忙轉發哦!後面會分享更多devops和DBA方面的內容,感興趣的朋友可以關注下~

文章來源: https://twgreatdaily.com/zh-sg/cbFfcW4BMH2_cNUgOAT4.html