pgcli 是針對PostgreSQL的命令行工具,他的特點是對SQL可以語法高亮顯示,而且能對輸入進行自動提示。(同時,針對MySQL也有一個相似的命令行工具mycli)。html
網站: http://pgcli.com/python
受權協議: BSD 3-clause licensegit
開發語言: Pythongithub
支持系統: Linux ,OS X , Windows理論上能夠但還沒有測試過。sql
安裝試用數據庫
環境:centos 32bit.bootstrap
數據庫:PostgreSQL9.4centos
編譯安裝或者安裝包圖形化安裝都可。可參考:PostgreSQL在Linux下的源碼編譯安裝。app
安裝python。可參考:python開發環境搭建與鏈接PostgreSQL數據庫。curl
首先試用yum安裝python-pip,結果顯示沒有可用的包。
[root@localhost ~]# yum install python-pip Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.cqu.edu.cn * updates: mirrors.163.com base | 3.7 kB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 updates/primary_db | 3.1 MB 00:04 Setting up Install Process No package python-pip available. Error: Nothing to do
而後經過制定具體地址來安裝,安裝成功:
[root@localhost ~]# curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1379k 100 1379k 0 0 67361 0 0:00:20 0:00:20 --:--:-- 2612k [root@localhost ~]# python get-pip.py Collecting pip Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ProtocolError('Connection aborted.', gaierror(-2, 'Name or service not known'))': /simple/pip/ /tmp/tmpEAjwcs/pip.zip/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning. Downloading pip-7.1.2-py2.py3-none-any.whl (1.1MB) 100% |████████████████████████████████| 1.1MB 209kB/s Collecting setuptools Downloading setuptools-19.1.1-py2.py3-none-any.whl (463kB) 100% |████████████████████████████████| 466kB 617kB/s Collecting wheel Downloading wheel-0.26.0-py2.py3-none-any.whl (63kB) 100% |████████████████████████████████| 65kB 1.4MB/s Collecting argparse (from wheel) Downloading argparse-1.4.0-py2.py3-none-any.whl Installing collected packages: pip, setuptools, argparse, wheel Successfully installed argparse-1.4.0 pip-7.1.2 setuptools-19.1.1 wheel-0.26.0
[root@localhost ~]# pip install pgcli
報錯:
Error: pg_config executable not found. Please add the directory containing pg_config to the PATH or specify the full executable path with the option: python setup.py build_ext --pg-config /path/to/pg_config build ... or with the pg_config option in 'setup.cfg'. ---------------------------------------- Command python setup.py egg_info failed with error code 1 in /tmp/pip-build/psycopg2
解決方法:pg_config在PostgreSQL的bin目錄下, 因此要在環境變量PATH中配置bin路徑。
[root@localhost ~]# export PATH=$PATH:/opt/PostgreSQL/9.4/bin/
從新執行安裝便可:
[root@localhost ~]# pip install pgcli
[root@localhost ~]# pgcli --help Usage: pgcli [OPTIONS] [DATABASE] [USERNAME] Options: -h, --host TEXT Host address of the postgres database. -p, --port INTEGER Port number at which the postgres instance is listening. -U, --user TEXT User name to connect to the postgres database. -W, --password Force password prompt. -w, --no-password Never prompt for password. -v, --version Version of pgcli. -d, --dbname TEXT database name to connect to. --pgclirc TEXT Location of pgclirc file. --help Show this message and exit. [root@localhost ~]# pgcli -h -p 5433 -U postgres could not translate host name "-p" to address: Name or service not known [root@localhost ~]# pgcli -h 127.0.0.1 -p 5433 -U postgres -d postgres Password: Version: 0.20.1 Chat: https://gitter.im/dbcli/pgcli Mail: https://groups.google.com/forum/#!forum/pgcli Home: http://pgcli.com postgres> Time: 0.000s postgres> select count(*) from public.a0; +---------+ | count | |---------| | 0 | +---------+ SELECT 1 Time: 0.030s
圖1.能夠看到pgcli對sql語句有語法高亮顯示。
圖2.能夠看到pgcli對輸入有自動提示功能(在簡單的測試環境下反應速度很理想的,其餘狀況有待測試)。
備註:本文僅限於測試試用,須要結合其餘測試工具進一步測試。不適用於生產環境。