作者:Asher.hu
原文連結:https://pgfans.cn/a/1756
【雲貝學院】PostgreSQL認證課程已上線,學院有不定期公開課 需要的同學可以加老師微信:19941464235
Asql 是一個用于執行psql指令的工具,在運維工作中,不僅要經常切換不同的資料庫,還經常會使用大量的SQL語句。在工作中我們或多或少都積累了一些自身的SQL腳本,但由于SQL語句中可能會帶有變量,原SQL的psql不太好處理。是以ASQL就出來了。 就是它是一個依托于psql的執行檔案。我們可以提前配置好連接配接串,和準備好SQL檔案就可以。下面我們來示範一下吧。看看它有什麼功能,如果你有更好的想法或者需求,也可以聯系我更新完善 ,其期待大家一起完善相應的SQL腳本。
1.工能介紹
- 可以直接執行psql 原始指令如\l ,在asql中執行 \l
- 支援切換資料庫,快捷鍵 c 或 \c
- 支援直接執行.sql 檔案
- 支援sql 檔案中帶參數
- 可以快速選擇其它資料庫
2.準備工作
準備環境變更,除了要求有權限運作psql外,還需要設定一個SQLDIR目錄。 同時我們提前編寫好.dblist檔案,需要連接配接的資料庫。
2.1環境準備
# 1.環境變量,如果有設定,會在進入的時候提示使輸入路徑
export SQLDIR=/home/postgres/dba
# 2.設定連接配接串, 格式需要按照要求寫
cat .dblist
# pg
業務資料庫: s2db: psql -U user02 -w user02 -h 192.168.1.55 -p 5432 -d s2db
監控資料庫: zcloud : psql -U user01 -w user01 -h 192.168.1.55 -p 5432 -d zcloud
#3.執行權限
chmod u+x asql
2.2SQL腳本
這裡我就用getpar.sql 和 db_connect.sql 這2個SQL腳本,getpar.sql 是帶有參數&parname 。
#參數查詢
postgres@s2ahumysqlpg01-> cat getpar.sql
\echo 資料庫參數
select name,setting,unit,boot_val,reset_val from pg_settings where name like '&parname%' ;
# 連接配接數統計
postgres@s2ahumysqlpg01-> cat db_connect.sql
\echo 資料庫連接配接數檢視
select max_conn, max_conn-now_conn as resi_conn, now_conn from (select setting::int8 as max_conn,(select count(*) from pg_stat_activity) as now_conn from pg_settings where name = 'max_connections') t;
3.使用示例
3.1 選擇資料庫
postgres@s2ahumysqlpg01-> ./asql
*******************************************
*******************************************
*******************************************
*******************************************
********* 歡迎使用ASQL *********
*******************************************
*******************************************
*******************************************
*******************************************
1 # pg
2 業務資料庫: s2db: psql -U user02 -w user02 -h 192.168.1.55 -p 5432 -d s2db
3 監控資料庫: zcloud : psql -U user01 -w user01 -h 192.168.1.55 -p 5432 -d zcloud
請輸入資料庫NO:2
你将要在 192.168.1.55:s2db 資料庫上執行操作
192.168.1.55:user02@s2db>
3.2 執行psql原使命
192.168.1.55:user02@s2db>\\du
List of roles
Role name | Attributes | Member of
------------------+------------------------------------------------------------+-----------------------
app1 | | {}
app2 | | {}
asher | | {}
barman | Superuser, Create role, Create DB | {}
huyi | | {}
huyi2 | | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {pg_stat_scan_tables}
repluser | Replication | {}
streaming_barman | Replication | {}
test | | {}
user01 | Superuser | {}
user02 | Superuser | {}
3.3執行sql檔案不帶參數
192.168.1.55:user02@s2db>db_connect.sql
資料庫連接配接數檢視
max_conn | resi_conn | now_conn
----------+-----------+----------
100 | 90 | 10
(1 row)
3.4執行sql檔案帶參數
192.168.1.55:user02@s2db>getpar.sql
Please input &parname :max_conne
輸入的參數值是: max_conne
資料庫參數
name | setting | unit | boot_val | reset_val
-----------------+---------+------+----------+-----------
max_connections | 100 | | 100 | 100
(1 row)
3.5 切換dbname
192.168.1.55:user01@zcloud>c
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------------+------------------------------
db01 | user01 | UTF8 | C | zh_CN.UTF-8 |
db02 | user02 | UTF8 | C | zh_CN.UTF-8 |
huyidb1 | postgres | UTF8 | C | C |
huyidb2 | postgres | UTF8 | C | C |
postgres | postgres | UTF8 | C | C |
s2db | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | C | C |
testdb | postgres | UTF8 | C | C |
zcloud | postgres | UTF8 | C | C | =Tc/postgres +
| | | | | postgres=CTc/postgres +
| | | | | zcloud_repl=CTc/postgres +
| | | | | zcloud_bk=CTc/postgres +
| | | | | zcloud_ha=CTc/postgres +
| | | | | zcloud_monitor=CTc/postgres +
| | | | | zcloud_platform=CTc/postgres
(11 rows)
請輸入你要切換的資料庫:huyidb2
請輸入要切換的使用者:user02
請輸入你密碼:user02
你的資料庫是: user02@huyidb2
192.168.1.55:user02@huyidb2>
3.5 檢視幫助
192.168.1.55:user02@s2db>h
press d list db connect
press c switch dbname
press s list sqlhelp
press l list sql file in the sqldir
press r reset SQLDIR
press h list help
You can directly enter the SQL statement to execute
You can execute the sql file, files that must end with .sql
if you want execute pg command like \d, please add '\', like this \\d
if you want exit, please enter : ctrl + c ora Q or q
192.168.1.55:user02@s2db>
192.168.1.55:user02@s2db>
192.168.1.55:user02@s2db>q
postgres@s2ahumysqlpg01->
4.下載下傳連接配接
墨天輪下載下傳: https://www.modb.pro/download/483370
*禁止轉載,可轉發(轉發文章請注明出處)