2017-06-12 05:20:24 +03:00
A REPL for Postgres
2014-10-13 02:19:34 +04:00
-------------------
2017-07-27 08:55:04 +03:00
|Build Status| |CodeCov| |PyPI| |Landscape| |Gitter|
2015-01-05 10:34:31 +03:00
2014-10-13 02:19:34 +04:00
This is a postgres client that does auto-completion and syntax highlighting.
2014-10-13 02:17:39 +04:00
2015-07-24 18:51:22 +03:00
Home Page: http://pgcli.com
2016-04-07 03:23:53 +03:00
MySQL Equivalent: http://mycli.net
2015-04-11 19:26:09 +03:00
2015-07-24 02:19:14 +03:00
.. image :: screenshots/pgcli.gif
2014-12-01 09:02:04 +03:00
.. image :: screenshots/image01.png
2014-10-13 02:17:39 +04:00
2014-12-01 09:27:18 +03:00
Quick Start
-----------
2015-01-07 04:15:45 +03:00
If you already know how to install python packages, then you can simply do:
2014-12-01 09:27:18 +03:00
::
2017-02-18 20:25:13 +03:00
$ pip install -U pgcli
2014-10-13 02:17:39 +04:00
2015-01-14 09:25:03 +03:00
or
2017-10-13 02:57:06 +03:00
$ sudo apt-get install pgcli # Only on Debian based Linux (e.g. Ubuntu, Mint, etc)
2017-07-20 17:30:15 +03:00
$ brew install pgcli # Only on macOS
2015-01-14 09:25:03 +03:00
2017-06-12 05:20:24 +03:00
If you don't know how to install python packages, please check the
2018-03-23 21:11:50 +03:00
`detailed instructions`_ .
2014-12-01 09:02:04 +03:00
2019-05-30 20:03:47 +03:00
If you are restricted to using psycopg2 2.7.x then pip will try to install it from a binary. There are some known issues with the psycopg2 2.7 binary - see the `psycopg docs`_ for more information about this and how to force installation from source. psycopg2 2.8 has fixed these problems, and will build from source.
2018-03-23 21:11:50 +03:00
.. _`detailed instructions`: https://github.com/dbcli/pgcli#detailed-installation-instructions
2019-05-30 20:03:47 +03:00
.. _`psycopg docs`: http://initd.org/psycopg/docs/install.html#change-in-binary-packages-between-psycopg-2-7-and-2-8
2014-12-01 09:02:04 +03:00
2014-12-01 09:27:18 +03:00
Usage
-----
2014-12-01 09:02:04 +03:00
2017-06-12 05:20:24 +03:00
::
2014-10-13 02:17:39 +04:00
2014-12-01 09:27:18 +03:00
$ pgcli [database_name]
or
2018-05-14 16:56:05 +03:00
$ pgcli postgresql://[user[:password]@][netloc][:port][/dbname][?extra=value[&other=other-value]]
2014-12-01 09:27:18 +03:00
2017-06-12 05:20:24 +03:00
Examples:
2014-12-01 09:27:18 +03:00
2017-06-12 05:20:24 +03:00
::
2014-12-01 09:27:18 +03:00
$ pgcli local_database
2018-05-14 16:56:05 +03:00
$ pgcli postgres://amjith:pa$$w0rd@example.com:5432/app_db?sslmode=verify-ca&sslrootcert=/myrootcert
2014-12-01 09:27:18 +03:00
2020-03-30 15:50:06 +03:00
For more details:
::
$ pgcli --help
Usage: pgcli [OPTIONS] [DBNAME] [USERNAME]
Options:
-h, --host TEXT Host address of the postgres database.
-p, --port INTEGER Port number at which the postgres instance is
listening.
-U, --username TEXT Username to connect to the postgres database.
-u, --user TEXT Username to connect to the postgres database.
-W, --password Force password prompt.
-w, --no-password Never prompt for password.
--single-connection Do not use a separate connection for completions.
-v, --version Version of pgcli.
-d, --dbname TEXT database name to connect to.
--pgclirc PATH Location of pgclirc file.
-D, --dsn TEXT Use DSN configured into the [alias_dsn] section of
pgclirc file.
--list-dsn list of DSN configured into the [alias_dsn] section
of pgclirc file.
--row-limit INTEGER Set threshold for row limit prompt. Use 0 to disable
prompt.
--less-chatty Skip intro on startup and goodbye on exit.
--prompt TEXT Prompt format (Default: "\u@\h:\d> ").
--prompt-dsn TEXT Prompt format for connections using DSN aliases
(Default: "\u@\h:\d> ").
-l, --list list available databases, then exit.
--auto-vertical-output Automatically switch to vertical output mode if the
result is wider than the terminal width.
--warn / --no-warn Warn before running a destructive query.
--help Show this message and exit.
`` pgcli `` also supports many of the same `environment variables`_ as `` psql `` for login options (e.g. `` PGHOST `` , `` PGPORT `` , `` PGUSER `` , `` PGPASSWORD `` , `` PGDATABASE `` ).
.. _environment variables: https://www.postgresql.org/docs/current/libpq-envars.html
2014-12-01 09:27:18 +03:00
Features
--------
The `pgcli` is written using prompt_toolkit_.
2015-10-29 06:13:54 +03:00
* Auto-completes as you type for SQL keywords as well as tables and
2014-12-01 09:27:18 +03:00
columns in the database.
* Syntax highlighting using Pygments.
2014-12-19 09:32:47 +03:00
* Smart-completion (enabled by default) will suggest context-sensitive
completion.
2014-12-01 09:27:18 +03:00
2017-06-12 05:20:24 +03:00
- `` SELECT * FROM <tab> `` will only show table names.
- `` SELECT * FROM users WHERE <tab> `` will only show column names.
2014-12-01 09:02:04 +03:00
2017-06-12 05:20:24 +03:00
* Primitive support for `` psql `` back-slash commands.
2015-01-07 12:57:46 +03:00
* Pretty prints tabular data.
2014-12-01 09:27:18 +03:00
.. _prompt_toolkit: https://github.com/jonathanslenders/python-prompt-toolkit
2018-03-16 13:49:29 +03:00
.. _tabulate: https://pypi.python.org/pypi/tabulate
2014-12-01 09:02:04 +03:00
2017-04-12 12:38:11 +03:00
Config
------
A config file is automatically created at `` ~/.config/pgcli/config `` at first launch.
See the file itself for a description of all available options.
2014-12-19 11:32:26 +03:00
Contributions:
--------------
If you're interested in contributing to this project, first of all I would like
to extend my heartfelt gratitude. I've written a small doc to describe how to
get this running in a development setup.
2015-04-19 08:54:58 +03:00
https://github.com/dbcli/pgcli/blob/master/DEVELOP.rst
2014-12-19 11:32:26 +03:00
2017-06-12 05:20:24 +03:00
Please feel free to reach out to me if you need help.
2014-12-19 11:32:26 +03:00
My email: amjith.r@gmail.com, Twitter: `@amjithr <http://twitter.com/amjithr> `_
2014-12-01 09:02:04 +03:00
Detailed Installation Instructions:
2014-12-01 09:27:18 +03:00
-----------------------------------
2014-12-01 09:02:04 +03:00
2017-06-02 22:05:48 +03:00
macOS:
======
2014-12-01 09:02:04 +03:00
2017-07-20 17:30:15 +03:00
The easiest way to install pgcli is using Homebrew.
2015-01-14 09:25:03 +03:00
::
$ brew install pgcli
Done!
Alternatively, you can install `` pgcli `` as a python package using a package
manager called called `` pip `` . You will need postgres installed on your system
2017-06-12 05:20:24 +03:00
for this to work.
2015-01-27 17:07:41 +03:00
In depth getting started guide for `` pip `` - https://pip.pypa.io/en/latest/installing.html.
2014-12-01 09:02:04 +03:00
2017-06-12 05:20:24 +03:00
::
2014-12-01 09:02:04 +03:00
$ which pip
If it is installed then you can do:
2017-06-12 05:20:24 +03:00
::
2014-12-01 09:02:04 +03:00
$ pip install pgcli
If that fails due to permission issues, you might need to run the command with
2017-06-12 05:20:24 +03:00
sudo permissions.
2014-12-01 09:02:04 +03:00
::
$ sudo pip install pgcli
If pip is not installed check if easy_install is available on the system.
2017-06-12 05:20:24 +03:00
::
2014-12-01 09:02:04 +03:00
$ which easy_install
2015-01-07 13:09:30 +03:00
$ sudo easy_install pgcli
2014-12-01 09:02:04 +03:00
Linux:
2014-12-01 09:27:18 +03:00
======
2014-12-01 09:02:04 +03:00
2015-01-27 17:07:41 +03:00
In depth getting started guide for `` pip `` - https://pip.pypa.io/en/latest/installing.html.
2014-12-01 09:14:08 +03:00
Check if pip is already available in your system.
2014-12-01 09:02:04 +03:00
2017-06-12 05:20:24 +03:00
::
2014-12-01 09:14:08 +03:00
$ which pip
2014-12-19 09:32:47 +03:00
If it doesn't exist, use your linux package manager to install `pip` . This
2017-06-12 05:20:24 +03:00
might look something like:
2014-12-01 09:14:08 +03:00
::
2015-01-14 09:25:03 +03:00
$ sudo apt-get install python-pip # Debian, Ubuntu, Mint etc
2014-12-01 09:14:08 +03:00
or
2015-01-14 09:25:03 +03:00
$ sudo yum install python-pip # RHEL, Centos, Fedora etc
`` pgcli `` requires python-dev, libpq-dev and libevent-dev packages. You can
2017-06-12 05:20:24 +03:00
install these via your operating system package manager.
2015-01-14 09:25:03 +03:00
::
$ sudo apt-get install python-dev libpq-dev libevent-dev
2017-06-12 05:20:24 +03:00
or
2015-01-14 09:25:03 +03:00
$ sudo yum install python-devel postgresql-devel
2014-12-01 09:14:08 +03:00
2017-06-12 05:20:24 +03:00
Then you can install pgcli:
2014-12-01 09:14:08 +03:00
2017-06-12 05:20:24 +03:00
::
2014-12-01 09:14:08 +03:00
$ sudo pip install pgcli
2014-12-01 09:02:04 +03:00
2017-03-03 01:30:40 +03:00
Docker
======
Pgcli can be run from within Docker. This can be useful to try pgcli without
installing it, or any dependencies, system-wide.
To build the image:
::
2017-04-25 11:44:58 +03:00
2017-03-03 01:30:40 +03:00
$ docker build -t pgcli .
To create a container from the image:
::
2017-04-25 11:44:58 +03:00
2017-03-03 01:30:40 +03:00
$ docker run --rm -ti pgcli pgcli <ARGS>
To access postgresql databases listening on localhost, make sure to run the
docker in "host net mode". E.g. to access a database called "foo" on the
postgresql server running on localhost:5432 (the standard port):
::
2017-04-25 11:44:58 +03:00
2017-03-03 01:30:40 +03:00
$ docker run --rm -ti --net host pgcli pgcli -h localhost foo
To connect to a locally running instance over a unix socket, bind the socket to
the docker container:
::
2017-04-25 11:44:58 +03:00
2017-03-03 01:30:40 +03:00
$ docker run --rm -ti -v /var/run/postgres:/var/run/postgres pgcli pgcli foo
2017-08-06 23:55:24 +03:00
IPython
=======
2017-08-07 00:00:11 +03:00
Pgcli can be run from within `IPython <https://ipython.org> `_ console. When working on a query,
it may be useful to drop into a pgcli session without leaving the IPython console, iterate on a
query, then quit pgcli to find the query results in your IPython workspace.
2017-08-06 23:55:24 +03:00
Assuming you have IPython installed:
::
$ pip install ipython-sql
After that, run ipython and load the `` pgcli.magic `` extension:
::
$ ipython
In [1]: %load_ext pgcli.magic
Connect to a database and construct a query:
::
In [2]: %pgcli postgres://someone@localhost:5432/world
Connected: someone@world
someone@localhost:world> select * from city c where countrycode = 'USA' and population > 1000000;
+------+--------------+---------------+--------------+--------------+
| id | name | countrycode | district | population |
|------+--------------+---------------+--------------+--------------|
| 3793 | New York | USA | New York | 8008278 |
| 3794 | Los Angeles | USA | California | 3694820 |
| 3795 | Chicago | USA | Illinois | 2896016 |
| 3796 | Houston | USA | Texas | 1953631 |
| 3797 | Philadelphia | USA | Pennsylvania | 1517550 |
| 3798 | Phoenix | USA | Arizona | 1321045 |
| 3799 | San Diego | USA | California | 1223400 |
| 3800 | Dallas | USA | Texas | 1188580 |
| 3801 | San Antonio | USA | Texas | 1144646 |
+------+--------------+---------------+--------------+--------------+
SELECT 9
Time: 0.003s
Exit out of pgcli session with `` Ctrl + D `` and find the query results:
::
someone@localhost:world>
Goodbye!
9 rows affected.
Out[2]:
[(3793, u'New York', u'USA', u'New York', 8008278),
(3794, u'Los Angeles', u'USA', u'California', 3694820),
(3795, u'Chicago', u'USA', u'Illinois', 2896016),
(3796, u'Houston', u'USA', u'Texas', 1953631),
(3797, u'Philadelphia', u'USA', u'Pennsylvania', 1517550),
(3798, u'Phoenix', u'USA', u'Arizona', 1321045),
(3799, u'San Diego', u'USA', u'California', 1223400),
(3800, u'Dallas', u'USA', u'Texas', 1188580),
(3801, u'San Antonio', u'USA', u'Texas', 1144646)]
The results are available in special local variable `` _ `` , and can be assigned to a variable of your
choice:
::
In [3]: my_result = _
2020-03-10 06:14:51 +03:00
Pgcli only runs on Python3.6+ since 2.2.0, if you use an old version of Python,
you should use install `` pgcli <= 2.2.0 `` .
2017-08-06 23:55:24 +03:00
2014-12-11 11:51:38 +03:00
Thanks:
2014-12-19 11:32:26 +03:00
-------
2014-12-08 11:43:21 +03:00
2014-12-11 21:10:26 +03:00
A special thanks to `Jonathan Slenders <https://twitter.com/jonathan_s> `_ for
2017-06-12 05:20:24 +03:00
creating `Python Prompt Toolkit <http://github.com/jonathanslenders/python-prompt-toolkit> `_ ,
2014-12-19 09:32:47 +03:00
which is quite literally the backbone library, that made this app possible.
Jonathan has also provided valuable feedback and support during the development
of this app.
2014-12-08 11:43:21 +03:00
2016-06-21 07:27:55 +03:00
`Click <http://click.pocoo.org/> `_ is used for command line option parsing
2014-12-19 09:32:47 +03:00
and printing error messages.
2014-12-08 11:43:21 +03:00
2014-12-19 09:32:47 +03:00
Thanks to `psycopg <http://initd.org/psycopg/> `_ for providing a rock solid
2015-01-08 01:20:53 +03:00
interface to Postgres database.
2014-12-19 09:32:47 +03:00
Thanks to all the beta testers and contributors for your time and patience. :)
2015-01-05 10:34:31 +03:00
2015-04-19 08:54:58 +03:00
.. |Build Status| image :: https://api.travis-ci.org/dbcli/pgcli.svg?branch=master
:target: https://travis-ci.org/dbcli/pgcli
2015-01-05 10:34:31 +03:00
2017-07-21 10:04:25 +03:00
.. |CodeCov| image :: https://codecov.io/gh/dbcli/pgcli/branch/master/graph/badge.svg
2017-07-27 08:49:43 +03:00
:target: https://codecov.io/gh/dbcli/pgcli
2017-07-21 10:04:25 +03:00
:alt: Code coverage report
2017-07-27 08:55:04 +03:00
.. |Landscape| image :: https://landscape.io/github/dbcli/pgcli/master/landscape.svg?style=flat
:target: https://landscape.io/github/dbcli/pgcli/master
:alt: Code Health
2015-10-11 19:58:05 +03:00
.. |PyPI| image :: https://img.shields.io/pypi/v/pgcli.svg
2015-01-05 10:34:31 +03:00
:target: https://pypi.python.org/pypi/pgcli/
:alt: Latest Version
2015-01-30 09:49:23 +03:00
.. |Gitter| image :: https://badges.gitter.im/Join%20Chat.svg
2015-04-19 08:54:58 +03:00
:target: https://gitter.im/dbcli/pgcli?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
2015-01-30 09:49:23 +03:00
:alt: Gitter Chat