PostgreSQL命令行工具 - pgcli

前端之家收集整理的这篇文章主要介绍了PostgreSQL命令行工具 - pgcli前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

简介

pgcli 是针对Postgresql的命令行工具,他的特色是对sql能够语法高亮显示,并且能对输入进行自动提示。(同时,针对MysqL也有一个类似的命令行工具mycli)。

网站:http://pgcli.com/

授权协议:BSD 3-clause license

开发语言:Python

支持系统:LinuxOS X, Windows理论上可以但尚未测试过。


安装试用

环境:centos 32bit.

数据库:Postgresql9.4

1.安装Postgresql

编译安装或者安装包图形化安装均可。可参考:PostgreSQL在Linux下的源码编译安装

2.安装python

安装python。可参考:python开发环境搭建与连接PostgreSQL数据库

3.安装python-pip

首先试用yum安装python-pip,结果显示没有可用的包。

[root@localhost~]#yuminstallpython-pip
Loadedplugins:fastestmirror,refresh-packagekit,security
Loadingmirrorspeedsfromcachedhostfile
*base:mirrors.163.com
*extras:mirrors.cqu.edu.cn
*updates:mirrors.163.com
base|3.7kB00:00
extras|3.4kB00:00
updates|3.4kB00:00
updates/primary_db|3.1MB00:04
SettingupInstallProcess
Nopackagepython-pipavailable.
Error:Nothingtodo

然后通过制定具体地址来安装,安装成功:

[root@localhost~]#curl"https://bootstrap.pypa.io/get-pip.py"-o"get-pip.py"
%Total%Received%XferdAverageSpeedTimeTimeTimeCurrent
DloadUploadTotalSpentLeftSpeed
1001379k1001379k006736100:00:200:00:20--:--:--2612k
[root@localhost~]#pythonget-pip.py
Collectingpip
Retrying(Retry(total=4,connect=None,read=None,redirect=None))afterconnectionbrokenby'ProtocolError('Connectionaborted.',gaierror(-2,'Nameorservicenotknown'))':/simple/pip/
/tmp/tmpEAjwcs/pip.zip/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90:InsecurePlatformWarning:AtrueSSLContextobjectisnotavailable.Thispreventsurllib3fromconfiguringSSLappropriatelyandmaycausecertainSSLconnectionstofail.Formoreinformation,seehttps://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
Downloadingpip-7.1.2-py2.py3-none-any.whl(1.1MB)
100%|████████████████████████████████|1.1MB209kB/s
Collectingsetuptools
Downloadingsetuptools-19.1.1-py2.py3-none-any.whl(463kB)
100%|████████████████████████████████|466kB617kB/s
Collectingwheel
Downloadingwheel-0.26.0-py2.py3-none-any.whl(63kB)
100%|████████████████████████████████|65kB1.4MB/s
Collectingargparse(fromwheel)
Downloadingargparse-1.4.0-py2.py3-none-any.whl
Installingcollectedpackages:pip,setuptools,argparse,wheel
Successfullyinstalledargparse-1.4.0pip-7.1.2setuptools-19.1.1wheel-0.26.0

4.安装pgcli

[root@localhost~]#pipinstallpgcli

报错:

Error:pg_configexecutablenotfound.
Pleaseaddthedirectorycontainingpg_configtothePATH
orspecifythefullexecutablepathwiththeoption:
pythonsetup.pybuild_ext--pg-config/path/to/pg_configbuild...
orwiththepg_configoptionin'setup.cfg'.
----------------------------------------
Commandpythonsetup.pyegg_infoFailedwitherrorcode1in/tmp/pip-build/psycopg2

解决方法:pg_config在Postgresql的bin目录下, 所以要在环境变量PATH中配置bin路径。

[root@localhost~]#exportPATH=$PATH:/opt/Postgresql/9.4/bin/

重新执行安装即可:

[root@localhost~]#pipinstallpgcli

5.运行pgcli并连接Postgresql执行查询

[root@localhost~]#pgcli--help
Usage:pgcli[OPTIONS][DATABASE][USERNAME]
Options:
-h,--hostTEXTHostaddressofthepostgresdatabase.
-p,--portINTEGERPortnumberatwhichthepostgresinstanceislistening.
-U,--userTEXTUsernametoconnecttothepostgresdatabase.
-W,--passwordForcepasswordprompt.
-w,--no-passwordNeverpromptforpassword.
-v,--versionVersionofpgcli.
-d,--dbnameTEXTdatabasenametoconnectto.
--pgclircTEXTLocationofpgclircfile.
--helpShowthismessageandexit.
[root@localhost~]#pgcli-h-p5433-Upostgres
couldnottranslatehostname"-p"toaddress:Nameorservicenotknown
[root@localhost~]#pgcli-h127.0.0.1-p5433-Upostgres-dpostgres
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>selectcount(*)frompublic.a0;
+---------+
|count|
|---------|
|0|
+---------+
SELECT1
Time:0.030s

图1.可以看到pgcli对sql语句有语法高亮显示

图2.可以看到pgcli对输入有自动提示功能(在简单的测试环境下反应速度很理想的,其他情况有待测试)。

备注:本文仅限于测试试用,需要结合其他测试工具进一步测试。不适用于生产环境。

原文链接:https://www.f2er.com/postgresql/194785.html

猜你在找的Postgre SQL相关文章