1
1
mirror of https://github.com/dbcli/pgcli.git synced 2024-11-23 11:14:07 +03:00
Postgres CLI with autocompletion and syntax highlighting
Go to file
Chris Novakovic 9b6925e705
Always exit unsuccessfully with sys.exit (#1481)
`exit` is undefined when the `site` module isn't loaded (i.e. when
Python is executed with the `-S` option), and `exit` isn't explicitly
defined as a function or imported from elsewhere in `main.py`, so
reaching any code path that attempts to execute `exit(1)` in this
environment results in a `NameError` being raised:

```
Traceback (most recent call last):
    File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
    File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
    File "[...]/__main__.py", line 415, in <module>
    File "[...]/__main__.py", line 399, in run
    File "[...]/__main__.py", line 389, in main
    File "/usr/lib/python3.10/runpy.py", line 220, in run_module
    mod_name, mod_spec, code = _get_module_details(mod_name)
    File "/usr/lib/python3.10/runpy.py", line 157, in _get_module_details
    code = loader.get_code(mod_name)
    File "[...]/__main__.py", line 264, in get_code
    File "[...]/__main__.py", line 204, in load_module
    File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
    File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
    File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
    File "<frozen importlib._bootstrap_external>", line 883, in exec_module
    File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
    File "third_party/python3/__pgcli_main__.py", line 3, in <module>
    File "[...]/click/core.py", line 1157, in __call__
    File "[...]/click/core.py", line 1078, in main
    File "[...]/click/core.py", line 1434, in invoke
    File "[...]/click/core.py", line 783, in invoke
    File "[...]/pgcli/main.py", line 1617, in cli
    File "[...]/pgcli/main.py", line 766, in connect
NameError: name 'exit' is not defined. Did you mean: 'atexit'?
```

When exiting unsuccessfully, do so with `sys.exit(1)` - this is already
used in several other places in `main.py`, and is always used (with exit
code 0) when exiting successfully.
2024-10-04 23:14:42 -07:00
.github rst2html -> docutils (#1459) 2024-04-19 21:08:36 -07:00
pgcli Always exit unsuccessfully with sys.exit (#1481) 2024-10-04 23:14:42 -07:00
screenshots Ukraine. 2022-03-09 15:13:00 -08:00
tests Add --ping as a CLI option (#1471) 2024-06-19 21:45:50 -07:00
.coveragerc Added coverage support for functional tests. 2017-03-31 16:00:54 -07:00
.editorconfig Add editorconfig 2017-07-27 13:12:53 +02:00
.git-blame-ignore-revs Add git-blame-ignore revs in anticipation of hyper-blame. 2018-05-17 05:55:05 -07:00
.gitignore Feature: Add new formatter to export data to sql like mycli (#1366) 2022-09-14 17:41:40 -07:00
.pre-commit-config.yaml Go back to black 23.3.0 which supports 3.7 2023-10-06 18:16:30 -07:00
AUTHORS Always exit unsuccessfully with sys.exit (#1481) 2024-10-04 23:14:42 -07:00
changelog.rst Always exit unsuccessfully with sys.exit (#1481) 2024-10-04 23:14:42 -07:00
DEVELOP.rst Changed destructive_warning to take a list of destructive commands (#1328) 2022-10-13 14:42:22 -07:00
Dockerfile Update python version from 2.7 to 3.8 in Dockerfile (#1181) 2020-05-11 09:51:23 -07:00
LICENSE.txt Update license. 2017-06-26 10:45:06 -07:00
MANIFEST.in exclude *.pyc from sdist and lower python3-keyring to >= 11.0.0 2018-07-14 09:41:54 +02:00
pgcli-completion.bash pgcli bash completion script (#892) 2020-04-03 15:15:53 -07:00
post-install Packaging: first cut of working deb and rpm build 2015-04-11 14:42:18 -07:00
post-remove Packaging: first cut of working deb and rpm build 2015-04-11 14:42:18 -07:00
pylintrc Make pylint happier with metadata.py 2017-03-17 03:33:41 +01:00
pyproject.toml Drop python 3.7, add 3.12 (#1426) 2023-10-11 12:34:59 +08:00
README.rst readme: update link to pip installation guide (#1469) 2024-06-26 09:22:26 -07:00
release.py Releasing version 4.0.1 2023-10-30 20:34:14 -07:00
RELEASES.md Explicitly point to release guide. 2022-03-30 16:43:31 -07:00
requirements-dev.txt Go back to black 23.3.0 which supports 3.7 2023-10-06 18:16:30 -07:00
sanity_checks.txt Corrected some typos 2015-01-08 20:45:12 +05:30
setup.py dependencies: add support for sqlparse 0.5.x (#1458) 2024-04-25 10:37:29 -07:00
TODO Move refresh routines outside the loop. 2015-01-31 13:46:32 -08:00
tox.ini Drop python 3.7, add 3.12 (#1426) 2023-10-11 12:34:59 +08:00
Vagrantfile Updated Vagrant file as it wasn't working. (#1255) 2021-03-12 10:20:28 -08:00

We stand with Ukraine
---------------------

Ukrainian people are fighting for their country. A lot of civilians, women and children, are suffering. Hundreds were killed and injured, and thousands were displaced.

This is an image from my home town, Kharkiv. This place is right in the old city center.

.. image:: screenshots/kharkiv-destroyed.jpg

Picture by @fomenko_ph (Telegram).

Please consider donating or volunteering.

* https://bank.gov.ua/en/
* https://savelife.in.ua/en/donate/
* https://www.comebackalive.in.ua/donate
* https://www.globalgiving.org/projects/ukraine-crisis-relief-fund/
* https://www.savethechildren.org/us/where-we-work/ukraine
* https://www.facebook.com/donate/1137971146948461/
* https://donate.wck.org/give/393234#!/donation/checkout
* https://atlantaforukraine.com/


A REPL for Postgres
-------------------

|Build Status| |CodeCov| |PyPI| |netlify|

This is a postgres client that does auto-completion and syntax highlighting.

Home Page: http://pgcli.com

MySQL Equivalent: http://mycli.net

.. image:: screenshots/pgcli.gif
.. image:: screenshots/image01.png

Quick Start
-----------

If you already know how to install python packages, then you can simply do:

::

    $ pip install -U pgcli

    or

    $ sudo apt-get install pgcli # Only on Debian based Linux (e.g. Ubuntu, Mint, etc)
    $ brew install pgcli  # Only on macOS

If you don't know how to install python packages, please check the
`detailed instructions`_.

.. _`detailed instructions`: https://github.com/dbcli/pgcli#detailed-installation-instructions

Usage
-----

::

    $ pgcli [database_name]

    or

    $ pgcli postgresql://[user[:password]@][netloc][:port][/dbname][?extra=value[&other=other-value]]

Examples:

::

    $ pgcli local_database

    $ pgcli postgres://amjith:pa$$w0rd@example.com:5432/app_db?sslmode=verify-ca&sslrootcert=/myrootcert

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 FILE             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 [all|moderate|off]  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``).

The SSL-related environment variables are also supported, so if you need to connect a postgres database via ssl connection, you can set set environment like this:

::

    export PGSSLMODE="verify-full"
    export PGSSLCERT="/your-path-to-certs/client.crt"
    export PGSSLKEY="/your-path-to-keys/client.key"
    export PGSSLROOTCERT="/your-path-to-ca/ca.crt"
    pgcli -h localhost -p 5432 -U username postgres

.. _environment variables: https://www.postgresql.org/docs/current/libpq-envars.html

Features
--------

The `pgcli` is written using prompt_toolkit_.

* Auto-completes as you type for SQL keywords as well as tables and
  columns in the database.
* Syntax highlighting using Pygments.
* Smart-completion (enabled by default) will suggest context-sensitive
  completion.

    - ``SELECT * FROM <tab>`` will only show table names.
    - ``SELECT * FROM users WHERE <tab>`` will only show column names.

* Primitive support for ``psql`` back-slash commands.
* Pretty prints tabular data.

.. _prompt_toolkit: https://github.com/jonathanslenders/python-prompt-toolkit
.. _tabulate: https://pypi.python.org/pypi/tabulate

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.

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.

https://github.com/dbcli/pgcli/blob/master/DEVELOP.rst

Please feel free to reach out to us if you need help.
* Amjith, pgcli author: amjith.r@gmail.com, Twitter: `@amjithr <http://twitter.com/amjithr>`_
* Irina, pgcli maintainer: i.chernyavska@gmail.com, Twitter: `@irinatruong <http://twitter.com/irinatruong>`_

Detailed Installation Instructions:
-----------------------------------

macOS:
======

The easiest way to install pgcli is using Homebrew.

::

    $ 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
for this to work.

In depth getting started guide for ``pip`` - https://pip.pypa.io/en/latest/installation/

::

    $ which pip

If it is installed then you can do:

::

    $ pip install pgcli

If that fails due to permission issues, you might need to run the command with
sudo permissions.

::

    $ sudo pip install pgcli

If pip is not installed check if easy_install is available on the system.

::

    $ which easy_install

    $ sudo easy_install pgcli

Linux:
======

In depth getting started guide for ``pip`` - https://pip.pypa.io/en/latest/installation/

Check if pip is already available in your system.

::

    $ which pip

If it doesn't exist, use your linux package manager to install `pip`. This
might look something like:

::

    $ sudo apt-get install python-pip   # Debian, Ubuntu, Mint etc

    or

    $ sudo yum install python-pip  # RHEL, Centos, Fedora etc

``pgcli`` requires python-dev, libpq-dev and libevent-dev packages. You can
install these via your operating system package manager.


::

    $ sudo apt-get install python-dev libpq-dev libevent-dev

    or

    $ sudo yum install python-devel postgresql-devel

Then you can install pgcli:

::

    $ sudo pip install pgcli


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:

::

    $ docker build -t pgcli .

To create a container from the image:

::

    $ 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):

::

    $ 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:

::

    $ docker run --rm -ti -v /var/run/postgres:/var/run/postgres pgcli pgcli foo


IPython
=======

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.

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 = _

Pgcli dropped support for Python<3.8 as of 4.0.0. If you need it, install ``pgcli <= 4.0.0``.

Thanks:
-------

A special thanks to `Jonathan Slenders <https://twitter.com/jonathan_s>`_ for
creating `Python Prompt Toolkit <http://github.com/jonathanslenders/python-prompt-toolkit>`_,
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.

`Click <http://click.pocoo.org/>`_ is used for command line option parsing
and printing error messages.

Thanks to `psycopg <https://www.psycopg.org/>`_ for providing a rock solid
interface to Postgres database.

Thanks to all the beta testers and contributors for your time and patience. :)


.. |Build Status| image:: https://github.com/dbcli/pgcli/actions/workflows/ci.yml/badge.svg?branch=main
    :target: https://github.com/dbcli/pgcli/actions/workflows/ci.yml

.. |CodeCov| image:: https://codecov.io/gh/dbcli/pgcli/branch/master/graph/badge.svg
   :target: https://codecov.io/gh/dbcli/pgcli
   :alt: Code coverage report

.. |Landscape| image:: https://landscape.io/github/dbcli/pgcli/master/landscape.svg?style=flat
   :target: https://landscape.io/github/dbcli/pgcli/master
   :alt: Code Health

.. |PyPI| image:: https://img.shields.io/pypi/v/pgcli.svg
    :target: https://pypi.python.org/pypi/pgcli/
    :alt: Latest Version

.. |netlify| image:: https://api.netlify.com/api/v1/badges/3a0a14dd-776d-445d-804c-3dd74fe31c4e/deploy-status
     :target: https://app.netlify.com/sites/pgcli/deploys
     :alt: Netlify