2012-10-07 05:04:39 +04:00
|
|
|
Introduction
|
|
|
|
============
|
|
|
|
|
|
|
|
.. sectionauthor:: Daniele Varrazzo <daniele.varrazzo@gmail.com>
|
|
|
|
|
|
|
|
Psycopg is a PostgreSQL_ adapter for the Python_ programming language. It is a
|
|
|
|
wrapper for the libpq_, the official PostgreSQL client library.
|
|
|
|
|
|
|
|
The `psycopg2` package is the current mature implementation of the adapter: it
|
|
|
|
is a C extension and as such it is only compatible with CPython_. If you want
|
|
|
|
to use Psycopg on a different Python implementation (PyPy, Jython, IronPython)
|
|
|
|
there is an experimental `porting of Psycopg for Ctypes`__, but it is not as
|
|
|
|
mature as the C implementation yet.
|
|
|
|
|
|
|
|
The current `!psycopg2` implementation supports:
|
|
|
|
|
2014-08-05 01:39:41 +04:00
|
|
|
..
|
|
|
|
NOTE: keep consistent with setup.py and the /features/ page.
|
|
|
|
|
2016-08-15 03:06:42 +03:00
|
|
|
- Python 2 versions from 2.6 to 2.7
|
2017-02-07 02:03:48 +03:00
|
|
|
- Python 3 versions from 3.2 to 3.6
|
2017-01-03 21:12:44 +03:00
|
|
|
- PostgreSQL server versions from 7.4 to 9.6
|
2016-08-15 04:17:47 +03:00
|
|
|
- PostgreSQL client library version from 9.1
|
2012-10-07 05:04:39 +04:00
|
|
|
|
|
|
|
.. _PostgreSQL: http://www.postgresql.org/
|
|
|
|
.. _Python: http://www.python.org/
|
|
|
|
.. _libpq: http://www.postgresql.org/docs/current/static/libpq.html
|
|
|
|
.. _CPython: http://en.wikipedia.org/wiki/CPython
|
|
|
|
.. _Ctypes: http://docs.python.org/library/ctypes.html
|
|
|
|
.. __: https://github.com/mvantellingen/psycopg2-ctypes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. index::
|
2017-02-16 20:17:13 +03:00
|
|
|
single: Install; from PyPI
|
2012-10-07 05:04:39 +04:00
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
Binary install from PyPI
|
|
|
|
------------------------
|
2012-10-07 05:04:39 +04:00
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
`!psycopg2` is `available on PyPI`__ in the form of wheel_ packages for the
|
|
|
|
most common platform (Linux, OSX, Windows): this should make you able to
|
|
|
|
install a binary version of the module including all the dependencies simply
|
2017-03-03 16:43:38 +03:00
|
|
|
using:
|
2012-10-07 05:04:39 +04:00
|
|
|
|
2017-03-03 16:43:38 +03:00
|
|
|
.. code-block:: console
|
|
|
|
|
|
|
|
$ pip install psycopg2
|
2012-10-07 05:04:39 +04:00
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
Make sure to use an up-to-date version of :program:`pip` (you can upgrade it
|
|
|
|
using something like ``pip install -U pip``)
|
2012-10-07 05:04:39 +04:00
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
.. __: PyPI_
|
|
|
|
.. _PyPI: https://pypi.python.org/pypi/psycopg2/
|
|
|
|
.. _wheel: http://pythonwheels.com/
|
2012-10-07 05:04:39 +04:00
|
|
|
|
2017-03-10 04:46:30 +03:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
The binary packages come with their own versions of a few C libraries,
|
2017-06-14 03:45:47 +03:00
|
|
|
among which ``libpq`` and ``libssl``, which will be used regardless of other
|
2017-03-10 04:46:30 +03:00
|
|
|
libraries available on the client: upgrading the system libraries will not
|
|
|
|
upgrade the libraries used by `!psycopg2`. Please build `!psycopg2` from
|
|
|
|
source if you want to maintain binary upgradeability.
|
|
|
|
|
2017-06-14 03:45:47 +03:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
Because the `!psycopg` wheel package uses its own ``libssl`` binary, it is
|
|
|
|
incompatible with other extension modules binding with ``libssl`` as well,
|
|
|
|
for instance the Python `ssl` module: the result will likely be a
|
|
|
|
segfault. If you need using both `!psycopg2` and other libraries using
|
|
|
|
``libssl`` please :ref:`install psycopg from source
|
|
|
|
<install-from-source>`.
|
|
|
|
|
2017-03-10 04:46:30 +03:00
|
|
|
If you prefer to use the system libraries available on your client you can use
|
|
|
|
the :command:`pip` ``--no-binary`` option:
|
2017-03-03 16:43:38 +03:00
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
|
|
|
$ pip install --no-binary psycopg2
|
|
|
|
|
|
|
|
which can be specified in your :file:`requirements.txt` files too, e.g. use:
|
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
psycopg2>=2.7,<2.8 --no-binary :all:
|
|
|
|
|
|
|
|
to use the last bugfix release of the `!psycopg2` 2.7 package, specifying to
|
|
|
|
always compile it from source. Of course in this case you will have to meet
|
|
|
|
the :ref:`build prerequisites <build-prerequisites>`.
|
|
|
|
|
2012-10-07 05:04:39 +04:00
|
|
|
|
|
|
|
|
|
|
|
.. index::
|
2017-02-16 20:17:13 +03:00
|
|
|
single: Install; from source
|
2016-07-01 19:56:29 +03:00
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
.. _install-from-source:
|
2016-07-01 19:56:29 +03:00
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
Install from source
|
|
|
|
-------------------
|
2016-07-01 19:56:29 +03:00
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
.. _source-package:
|
2012-10-07 05:04:39 +04:00
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
You can download a copy of Psycopg source files from the `Psycopg download
|
|
|
|
page`__ or from PyPI_.
|
2012-10-07 05:04:39 +04:00
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
.. __: http://initd.org/psycopg/download/
|
2012-10-07 05:04:39 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
.. _build-prerequisites:
|
2012-10-07 05:04:39 +04:00
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
Build prerequisites
|
|
|
|
^^^^^^^^^^^^^^^^^^^
|
2012-10-07 05:04:39 +04:00
|
|
|
|
|
|
|
These notes illustrate how to compile Psycopg on Linux. If you want to compile
|
|
|
|
Psycopg on other platforms you may have to adjust some details accordingly.
|
|
|
|
|
2017-06-14 03:45:47 +03:00
|
|
|
Psycopg is a C wrapper around the libpq_ PostgreSQL client library. To install
|
|
|
|
it from sources you will need:
|
2012-10-07 05:04:39 +04:00
|
|
|
|
|
|
|
- A C compiler.
|
|
|
|
|
|
|
|
- The Python header files. They are usually installed in a package such as
|
|
|
|
**python-dev**. A message such as *error: Python.h: No such file or
|
|
|
|
directory* is an indication that the Python headers are missing.
|
|
|
|
|
|
|
|
- The libpq header files. They are usually installed in a package such as
|
|
|
|
**libpq-dev**. If you get an *error: libpq-fe.h: No such file or directory*
|
|
|
|
you are missing them.
|
|
|
|
|
|
|
|
- The :program:`pg_config` program: it is usually installed by the
|
|
|
|
**libpq-dev** package but sometimes it is not in a :envvar:`PATH` directory.
|
|
|
|
Having it in the :envvar:`PATH` greatly streamlines the installation, so try
|
|
|
|
running ``pg_config --version``: if it returns an error or an unexpected
|
|
|
|
version number then locate the directory containing the :program:`pg_config`
|
|
|
|
shipped with the right libpq version (usually
|
2017-03-03 16:43:38 +03:00
|
|
|
``/usr/lib/postgresql/X.Y/bin/``) and add it to the :envvar:`PATH`:
|
|
|
|
|
|
|
|
.. code-block:: console
|
2012-10-07 05:04:39 +04:00
|
|
|
|
|
|
|
$ export PATH=/usr/lib/postgresql/X.Y/bin/:$PATH
|
2017-02-16 20:17:13 +03:00
|
|
|
|
|
|
|
You only need :program:`pg_config` to compile `!psycopg2`, not for its
|
|
|
|
regular usage.
|
|
|
|
|
2017-03-13 15:13:47 +03:00
|
|
|
Once everything is in place it's just a matter of running the standard:
|
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
|
|
|
$ python setup.py build
|
|
|
|
$ python setup.py install
|
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
|
|
|
|
Runtime requirements
|
|
|
|
^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Unless you compile `!psycopg2` as a static library, or you install it from a
|
|
|
|
self-contained wheel package, it will need the libpq_ library at runtime
|
|
|
|
(usually distributed in a ``libpq.so`` or ``libpq.dll`` file). `!psycopg2`
|
|
|
|
relies on the host OS to find the library if the library is installed in a
|
|
|
|
standard location there is usually no problem; if the library is in a
|
|
|
|
non-standard location you will have to tell somehow Psycopg how to find it,
|
|
|
|
which is OS-dependent (for instance setting a suitable
|
|
|
|
:envvar:`LD_LIBRARY_PATH` on Linux).
|
2012-10-07 05:04:39 +04:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
The libpq header files used to compile `!psycopg2` should match the
|
|
|
|
version of the library linked at runtime. If you get errors about missing
|
|
|
|
or mismatching libraries when importing `!psycopg2` check (e.g. using
|
|
|
|
:program:`ldd`) if the module ``psycopg2/_psycopg.so`` is linked to the
|
|
|
|
right ``libpq.so``.
|
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
.. note::
|
2012-10-07 05:04:39 +04:00
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
Whatever version of libpq `!psycopg2` is compiled with, it will be
|
|
|
|
possible to connect to PostgreSQL servers of any supported version: just
|
|
|
|
install the most recent libpq version or the most practical, without
|
|
|
|
trying to match it to the version of the PostgreSQL server you will have
|
|
|
|
to connect to.
|
2012-10-07 05:04:39 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: setup.py
|
|
|
|
single: setup.cfg
|
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
Non-standard builds
|
|
|
|
^^^^^^^^^^^^^^^^^^^
|
2012-10-07 05:04:39 +04:00
|
|
|
|
|
|
|
If you have less standard requirements such as:
|
|
|
|
|
|
|
|
- creating a :ref:`debug build <debug-build>`,
|
|
|
|
- using :program:`pg_config` not in the :envvar:`PATH`,
|
|
|
|
- supporting ``mx.DateTime``,
|
|
|
|
|
|
|
|
then take a look at the ``setup.cfg`` file.
|
|
|
|
|
|
|
|
Some of the options available in ``setup.cfg`` are also available as command
|
|
|
|
line arguments of the ``build_ext`` sub-command. For instance you can specify
|
2017-03-03 16:43:38 +03:00
|
|
|
an alternate :program:`pg_config` location using:
|
|
|
|
|
|
|
|
.. code-block:: console
|
2012-10-07 05:04:39 +04:00
|
|
|
|
|
|
|
$ python setup.py build_ext --pg-config /path/to/pg_config build
|
|
|
|
|
|
|
|
Use ``python setup.py build_ext --help`` to get a list of the options
|
|
|
|
supported.
|
|
|
|
|
2014-08-23 21:57:12 +04:00
|
|
|
|
2012-10-07 05:04:39 +04:00
|
|
|
.. index::
|
|
|
|
single: debug
|
|
|
|
single: PSYCOPG_DEBUG
|
|
|
|
|
|
|
|
.. _debug-build:
|
|
|
|
|
|
|
|
Creating a debug build
|
2017-02-16 20:17:13 +03:00
|
|
|
^^^^^^^^^^^^^^^^^^^^^^
|
2012-10-07 05:04:39 +04:00
|
|
|
|
|
|
|
In case of problems, Psycopg can be configured to emit detailed debug
|
|
|
|
messages, which can be very useful for diagnostics and to report a bug. In
|
|
|
|
order to create a debug package:
|
|
|
|
|
|
|
|
- `Download`__ and unpack the Psycopg source package.
|
|
|
|
|
|
|
|
- Edit the ``setup.cfg`` file adding the ``PSYCOPG_DEBUG`` flag to the
|
|
|
|
``define`` option.
|
|
|
|
|
|
|
|
- :ref:`Compile and install <source-package>` the package.
|
|
|
|
|
2017-03-03 16:43:38 +03:00
|
|
|
- Set the :envvar:`PSYCOPG_DEBUG` environment variable:
|
|
|
|
|
|
|
|
.. code-block:: console
|
2012-10-07 05:04:39 +04:00
|
|
|
|
|
|
|
$ export PSYCOPG_DEBUG=1
|
|
|
|
|
|
|
|
- Run your program (making sure that the `!psycopg2` package imported is the
|
|
|
|
one you just compiled and not e.g. the system one): you will have a copious
|
2014-08-23 21:57:12 +04:00
|
|
|
stream of informations printed on stderr.
|
2012-10-07 05:04:39 +04:00
|
|
|
|
|
|
|
.. __: http://initd.org/psycopg/download/
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
.. index::
|
|
|
|
single: tests
|
|
|
|
|
|
|
|
.. _test-suite:
|
|
|
|
|
|
|
|
Running the test suite
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
Once `!psycopg2` is installed you can run the test suite to verify it is
|
2017-03-03 16:43:38 +03:00
|
|
|
working correctly. You can run:
|
|
|
|
|
|
|
|
.. code-block:: console
|
2017-02-16 20:17:13 +03:00
|
|
|
|
2017-03-03 16:43:38 +03:00
|
|
|
$ python -c "from psycopg2 import tests; tests.unittest.main(defaultTest='tests.test_suite')" --verbose
|
2017-02-16 20:17:13 +03:00
|
|
|
|
|
|
|
The tests run against a database called ``psycopg2_test`` on UNIX socket and
|
|
|
|
the standard port. You can configure a different database to run the test by
|
|
|
|
setting the environment variables:
|
|
|
|
|
|
|
|
- :envvar:`PSYCOPG2_TESTDB`
|
|
|
|
- :envvar:`PSYCOPG2_TESTDB_HOST`
|
|
|
|
- :envvar:`PSYCOPG2_TESTDB_PORT`
|
|
|
|
- :envvar:`PSYCOPG2_TESTDB_USER`
|
|
|
|
|
|
|
|
The database should already exist before running the tests.
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-07 05:04:39 +04:00
|
|
|
.. _other-problems:
|
|
|
|
|
|
|
|
If you still have problems
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
Try the following. *In order:*
|
|
|
|
|
2017-02-16 20:17:13 +03:00
|
|
|
- Read again the :ref:`build-prerequisites`.
|
2012-10-07 05:04:39 +04:00
|
|
|
|
|
|
|
- Read the :ref:`FAQ <faq-compile>`.
|
|
|
|
|
|
|
|
- Google for `!psycopg2` *your error message*. Especially useful the week
|
|
|
|
after the release of a new OS X version.
|
|
|
|
|
|
|
|
- Write to the `Mailing List`__.
|
|
|
|
|
|
|
|
- Complain on your blog or on Twitter that `!psycopg2` is the worst package
|
|
|
|
ever and about the quality time you have wasted figuring out the correct
|
|
|
|
:envvar:`ARCHFLAGS`. Especially useful from the Starbucks near you.
|
|
|
|
|
2017-03-10 04:46:30 +03:00
|
|
|
.. __: https://lists.postgresql.org/mj/mj_wwwusr?func=lists-long-full&extra=psycopg
|