2012-10-07 05:04:39 +04:00
|
|
|
.. _usage:
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
Basic module usage
|
|
|
|
==================
|
|
|
|
|
|
|
|
.. sectionauthor:: Daniele Varrazzo <daniele.varrazzo@gmail.com>
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
pair: Example; Usage
|
|
|
|
|
2010-02-09 23:04:49 +03:00
|
|
|
The basic Psycopg usage is common to all the database adapters implementing
|
2010-02-10 00:31:40 +03:00
|
|
|
the |DBAPI|_ protocol. Here is an interactive session showing some of the
|
2010-02-09 07:58:28 +03:00
|
|
|
basic commands::
|
|
|
|
|
|
|
|
>>> import psycopg2
|
|
|
|
|
|
|
|
# Connect to an existing database
|
|
|
|
>>> conn = psycopg2.connect("dbname=test user=postgres")
|
|
|
|
|
2010-02-13 19:06:39 +03:00
|
|
|
# Open a cursor to perform database operations
|
2010-02-09 07:58:28 +03:00
|
|
|
>>> cur = conn.cursor()
|
|
|
|
|
|
|
|
# Execute a command: this creates a new table
|
|
|
|
>>> cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
|
|
|
|
|
2010-02-09 23:04:49 +03:00
|
|
|
# Pass data to fill a query placeholders and let Psycopg perform
|
2010-02-09 07:58:28 +03:00
|
|
|
# the correct conversion (no more SQL injections!)
|
2010-02-10 07:03:30 +03:00
|
|
|
>>> cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)",
|
2010-02-09 07:58:28 +03:00
|
|
|
... (100, "abc'def"))
|
|
|
|
|
|
|
|
# Query the database and obtain data as Python objects
|
|
|
|
>>> cur.execute("SELECT * FROM test;")
|
|
|
|
>>> cur.fetchone()
|
|
|
|
(1, 100, "abc'def")
|
|
|
|
|
|
|
|
# Make the changes to the database persistent
|
|
|
|
>>> conn.commit()
|
|
|
|
|
|
|
|
# Close communication with the database
|
|
|
|
>>> cur.close()
|
|
|
|
>>> conn.close()
|
|
|
|
|
|
|
|
|
2011-11-01 11:09:51 +04:00
|
|
|
The main entry points of Psycopg are:
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
- The function `~psycopg2.connect()` creates a new database session and
|
|
|
|
returns a new `connection` instance.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
- The class `connection` encapsulates a database session. It allows to:
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
- create new `cursor`\s using the `~connection.cursor()` method to
|
2010-02-13 00:51:48 +03:00
|
|
|
execute database commands and queries,
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2012-11-12 05:32:38 +04:00
|
|
|
- terminate transactions using the methods `~connection.commit()` or
|
2010-02-26 03:17:52 +03:00
|
|
|
`~connection.rollback()`.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
- The class `cursor` allows interaction with the database:
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
- send commands to the database using methods such as `~cursor.execute()`
|
|
|
|
and `~cursor.executemany()`,
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-03-03 20:43:24 +03:00
|
|
|
- retrieve data from the database :ref:`by iteration <cursor-iterable>` or
|
|
|
|
using methods such as `~cursor.fetchone()`, `~cursor.fetchmany()`,
|
2010-02-26 03:17:52 +03:00
|
|
|
`~cursor.fetchall()`.
|
2010-02-10 07:03:30 +03:00
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
pair: Query; Parameters
|
|
|
|
|
|
|
|
.. _query-parameters:
|
|
|
|
|
|
|
|
Passing parameters to SQL queries
|
|
|
|
---------------------------------
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
Psycopg casts Python variables to SQL literals by type. Many standard Python types
|
|
|
|
are already `adapted to the correct SQL representation`__.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
.. __: python-types-adaptation_
|
|
|
|
|
|
|
|
Example: the Python function call::
|
|
|
|
|
|
|
|
>>> cur.execute(
|
|
|
|
... """INSERT INTO some_table (an_int, a_date, a_string)
|
|
|
|
... VALUES (%s, %s, %s);""",
|
|
|
|
... (10, datetime.date(2005, 11, 18), "O'Reilly"))
|
|
|
|
|
|
|
|
is converted into the SQL command::
|
|
|
|
|
|
|
|
INSERT INTO some_table (an_int, a_date, a_string)
|
|
|
|
VALUES (10, '2005-11-18', 'O''Reilly');
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
Named arguments are supported too using :samp:`%({name})s` placeholders.
|
|
|
|
Using named arguments the values can be passed to the query in any order and
|
2011-11-01 11:09:51 +04:00
|
|
|
many placeholders can use the same values::
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
>>> cur.execute(
|
|
|
|
... """INSERT INTO some_table (an_int, a_date, another_date, a_string)
|
|
|
|
... VALUES (%(int)s, %(date)s, %(date)s, %(str)s);""",
|
|
|
|
... {'int': 10, 'str': "O'Reilly", 'date': datetime.date(2005, 11, 18)})
|
|
|
|
|
2012-05-30 02:53:02 +04:00
|
|
|
When parameters are used, in order to include a literal ``%`` in the query you
|
|
|
|
can use the ``%%`` string.
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
While the mechanism resembles regular Python strings manipulation, there are a
|
|
|
|
few subtle differences you should care about when passing parameters to a
|
|
|
|
query:
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
- The Python string operator ``%`` is not used: the `~cursor.execute()`
|
2010-02-09 07:58:28 +03:00
|
|
|
method accepts a tuple or dictionary of values as second parameter.
|
|
|
|
|sql-warn|__.
|
2010-02-10 07:03:30 +03:00
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. |sql-warn| replace:: **Never** use ``%`` or ``+`` to merge values
|
|
|
|
into queries
|
|
|
|
|
|
|
|
.. __: sql-injection_
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
- The variables placeholder must *always be a* ``%s``, even if a different
|
|
|
|
placeholder (such as a ``%d`` for integers or ``%f`` for floats) may look
|
|
|
|
more appropriate::
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
>>> cur.execute("INSERT INTO numbers VALUES (%d)", (42,)) # WRONG
|
|
|
|
>>> cur.execute("INSERT INTO numbers VALUES (%s)", (42,)) # correct
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
- For positional variables binding, *the second argument must always be a
|
2010-11-19 16:13:14 +03:00
|
|
|
sequence*, even if it contains a single variable. And remember that Python
|
2010-02-18 06:51:17 +03:00
|
|
|
requires a comma to create a single element tuple::
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
>>> cur.execute("INSERT INTO foo VALUES (%s)", "bar") # WRONG
|
2010-02-18 06:51:17 +03:00
|
|
|
>>> cur.execute("INSERT INTO foo VALUES (%s)", ("bar")) # WRONG
|
2010-02-09 07:58:28 +03:00
|
|
|
>>> cur.execute("INSERT INTO foo VALUES (%s)", ("bar",)) # correct
|
2010-11-19 16:13:14 +03:00
|
|
|
>>> cur.execute("INSERT INTO foo VALUES (%s)", ["bar"]) # correct
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
- Only variable values should be bound via this method: it shouldn't be used
|
|
|
|
to set table or field names. For these elements, ordinary string formatting
|
2010-02-26 03:17:52 +03:00
|
|
|
should be used before running `~cursor.execute()`.
|
2010-02-11 06:15:14 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. index:: Security, SQL injection
|
|
|
|
|
|
|
|
.. _sql-injection:
|
|
|
|
|
|
|
|
The problem with the query parameters
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
The SQL representation for many data types is often not the same of the Python
|
|
|
|
string representation. The classic example is with single quotes in
|
|
|
|
strings: SQL uses them as string constants bounds and requires them to be
|
|
|
|
escaped, whereas in Python single quotes can be left unescaped in strings
|
|
|
|
bounded by double quotes. For this reason a naïve approach to the composition
|
|
|
|
of query strings, e.g. using string concatenation, is a recipe for terrible
|
|
|
|
problems::
|
|
|
|
|
|
|
|
>>> SQL = "INSERT INTO authors (name) VALUES ('%s');" # NEVER DO THIS
|
|
|
|
>>> data = ("O'Reilly", )
|
|
|
|
>>> cur.execute(SQL % data) # THIS WILL FAIL MISERABLY
|
|
|
|
ProgrammingError: syntax error at or near "Reilly"
|
|
|
|
LINE 1: INSERT INTO authors (name) VALUES ('O'Reilly')
|
|
|
|
^
|
|
|
|
|
|
|
|
If the variable containing the data to be sent to the database comes from an
|
|
|
|
untrusted source (e.g. a form published on a web site) an attacker could
|
|
|
|
easily craft a malformed string, either gaining access to unauthorized data or
|
|
|
|
performing destructive operations on the database. This form of attack is
|
|
|
|
called `SQL injection`_ and is known to be one of the most widespread forms of
|
|
|
|
attack to servers. Before continuing, please print `this page`__ as a memo and
|
2010-02-13 00:51:48 +03:00
|
|
|
hang it onto your desk.
|
2010-02-11 06:15:14 +03:00
|
|
|
|
|
|
|
.. _SQL injection: http://en.wikipedia.org/wiki/SQL_injection
|
|
|
|
.. __: http://xkcd.com/327/
|
|
|
|
|
2011-11-01 11:09:51 +04:00
|
|
|
Psycopg can `automatically convert Python objects to and from SQL
|
|
|
|
literals`__: using this feature your code will be more robust and
|
|
|
|
reliable. We must stress this point:
|
2010-02-11 06:15:14 +03:00
|
|
|
|
|
|
|
.. __: python-types-adaptation_
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
Never, **never**, **NEVER** use Python string concatenation (``+``) or
|
|
|
|
string parameters interpolation (``%``) to pass variables to a SQL query
|
|
|
|
string. Not even at gunpoint.
|
|
|
|
|
|
|
|
The correct way to pass variables in a SQL command is using the second
|
2010-02-26 03:17:52 +03:00
|
|
|
argument of the `~cursor.execute()` method::
|
2010-02-11 06:15:14 +03:00
|
|
|
|
2011-10-15 02:59:49 +04:00
|
|
|
>>> SQL = "INSERT INTO authors (name) VALUES (%s);" # Note: no quotes
|
2010-02-11 06:15:14 +03:00
|
|
|
>>> data = ("O'Reilly", )
|
2011-10-15 02:59:49 +04:00
|
|
|
>>> cur.execute(SQL, data) # Note: no % operator
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. index::
|
2010-09-27 05:25:09 +04:00
|
|
|
single: Adaptation
|
2010-02-09 07:58:28 +03:00
|
|
|
pair: Objects; Adaptation
|
|
|
|
single: Data types; Adaptation
|
|
|
|
|
|
|
|
.. _python-types-adaptation:
|
|
|
|
|
|
|
|
Adaptation of Python values to SQL types
|
|
|
|
----------------------------------------
|
|
|
|
|
2012-07-09 01:52:24 +04:00
|
|
|
Many standard Python types are adapted into SQL and returned as Python
|
2010-02-10 07:03:30 +03:00
|
|
|
objects when a query is executed.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
If you need to convert other Python types to and from PostgreSQL data types,
|
2010-02-13 05:10:51 +03:00
|
|
|
see :ref:`adapting-new-types` and :ref:`type-casting-from-sql-to-python`. You
|
2010-02-26 03:17:52 +03:00
|
|
|
can also find a few other specialized adapters in the `psycopg2.extras`
|
2010-02-13 05:10:51 +03:00
|
|
|
module.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
In the following examples the method `~cursor.mogrify()` is used to show
|
2010-02-09 07:58:28 +03:00
|
|
|
the SQL string that would be sent to the database.
|
|
|
|
|
2011-02-19 19:16:28 +03:00
|
|
|
.. _adapt-consts:
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. index::
|
2010-12-02 18:06:27 +03:00
|
|
|
pair: None; Adaptation
|
2010-02-09 07:58:28 +03:00
|
|
|
single: NULL; Adaptation
|
2010-12-02 18:06:27 +03:00
|
|
|
pair: Boolean; Adaptation
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2011-02-19 19:16:28 +03:00
|
|
|
- Python `None` and boolean values `True` and `False` are converted into the
|
|
|
|
proper SQL literals::
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
>>> cur.mogrify("SELECT %s, %s, %s;", (None, True, False))
|
2013-01-09 07:10:32 +04:00
|
|
|
'SELECT NULL, true, false;'
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2011-02-19 19:16:28 +03:00
|
|
|
.. _adapt-numbers:
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. index::
|
2010-12-02 18:06:27 +03:00
|
|
|
single: Adaptation; numbers
|
2010-02-09 07:58:28 +03:00
|
|
|
single: Integer; Adaptation
|
|
|
|
single: Float; Adaptation
|
|
|
|
single: Decimal; Adaptation
|
|
|
|
|
2011-02-19 19:16:28 +03:00
|
|
|
- Numeric objects: `int`, `long`, `float`, `~decimal.Decimal` are converted in
|
|
|
|
the PostgreSQL numerical representation::
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
>>> cur.mogrify("SELECT %s, %s, %s, %s;", (10, 10L, 10.0, Decimal("10.00")))
|
2013-01-09 07:10:32 +04:00
|
|
|
'SELECT 10, 10, 10.0, 10.00;'
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2011-02-19 19:16:28 +03:00
|
|
|
.. _adapt-string:
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. index::
|
2010-12-02 18:06:27 +03:00
|
|
|
pair: Strings; Adaptation
|
2010-02-09 07:58:28 +03:00
|
|
|
single: Unicode; Adaptation
|
2011-02-10 05:15:44 +03:00
|
|
|
|
2011-02-19 19:16:28 +03:00
|
|
|
- String types: `str`, `unicode` are converted in SQL string syntax.
|
2011-02-10 05:15:44 +03:00
|
|
|
`!unicode` objects (`!str` in Python 3) are encoded in the connection
|
|
|
|
`~connection.encoding` to be sent to the backend: trying to send a character
|
|
|
|
not supported by the encoding will result in an error. Received data can be
|
2011-02-19 19:16:28 +03:00
|
|
|
converted either as `!str` or `!unicode`: see :ref:`unicode-handling`.
|
|
|
|
|
|
|
|
.. _adapt-binary:
|
2011-02-10 05:15:44 +03:00
|
|
|
|
|
|
|
.. index::
|
2010-02-09 07:58:28 +03:00
|
|
|
single: Buffer; Adaptation
|
|
|
|
single: bytea; Adaptation
|
2011-02-15 20:30:43 +03:00
|
|
|
single: bytes; Adaptation
|
|
|
|
single: bytearray; Adaptation
|
|
|
|
single: memoryview; Adaptation
|
2010-02-09 07:58:28 +03:00
|
|
|
single: Binary string
|
|
|
|
|
2011-12-16 15:09:20 +04:00
|
|
|
- Binary types: Python types representing binary objects are converted into
|
2011-02-15 20:30:43 +03:00
|
|
|
PostgreSQL binary string syntax, suitable for :sql:`bytea` fields. Such
|
2011-02-19 19:16:28 +03:00
|
|
|
types are `buffer` (only available in Python 2), `memoryview` (available
|
|
|
|
from Python 2.7), `bytearray` (available from Python 2.6) and `bytes`
|
2011-12-16 15:09:20 +04:00
|
|
|
(only from Python 3: the name is available from Python 2.6 but it's only an
|
2011-02-16 05:54:30 +03:00
|
|
|
alias for the type `!str`). Any object implementing the `Revised Buffer
|
|
|
|
Protocol`__ should be usable as binary type where the protocol is supported
|
|
|
|
(i.e. from Python 2.6). Received data is returned as `!buffer` (in Python 2)
|
|
|
|
or `!memoryview` (in Python 3).
|
|
|
|
|
|
|
|
.. __: http://www.python.org/dev/peps/pep-3118/
|
|
|
|
|
|
|
|
.. versionchanged:: 2.4
|
|
|
|
only strings were supported before.
|
2011-02-15 20:30:43 +03:00
|
|
|
|
2011-03-26 14:19:01 +03:00
|
|
|
.. versionchanged:: 2.4.1
|
|
|
|
can parse the 'hex' format from 9.0 servers without relying on the
|
|
|
|
version of the client library.
|
|
|
|
|
2011-02-15 20:30:43 +03:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
In Python 2, if you have binary data in a `!str` object, you can pass them
|
|
|
|
to a :sql:`bytea` field using the `psycopg2.Binary` wrapper::
|
|
|
|
|
|
|
|
mypic = open('picture.png', 'rb').read()
|
|
|
|
curs.execute("insert into blobs (file) values (%s)",
|
|
|
|
(psycopg2.Binary(mypic),))
|
2011-02-10 05:15:44 +03:00
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
2011-03-26 14:19:01 +03:00
|
|
|
Since version 9.0 PostgreSQL uses by default `a new "hex" format`__ to
|
|
|
|
emit :sql:`bytea` fields. Starting from Psycopg 2.4.1 the format is
|
|
|
|
correctly supported. If you use a previous version you will need some
|
|
|
|
extra care when receiving bytea from PostgreSQL: you must have at least
|
2011-11-01 11:09:51 +04:00
|
|
|
libpq 9.0 installed on the client or alternatively you can set the
|
|
|
|
`bytea_output`__ configuration parameter to ``escape``, either in the
|
2011-03-26 14:19:01 +03:00
|
|
|
server configuration file or in the client session (using a query such as
|
|
|
|
``SET bytea_output TO escape;``) before receiving binary data.
|
2012-02-24 04:33:28 +04:00
|
|
|
|
2012-02-28 20:28:07 +04:00
|
|
|
.. __: http://www.postgresql.org/docs/current/static/datatype-binary.html
|
|
|
|
.. __: http://www.postgresql.org/docs/current/static/runtime-config-client.html#GUC-BYTEA-OUTPUT
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2011-02-19 19:16:28 +03:00
|
|
|
.. _adapt-date:
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. index::
|
2010-12-02 18:06:27 +03:00
|
|
|
single: Adaptation; Date/Time objects
|
2010-02-09 07:58:28 +03:00
|
|
|
single: Date objects; Adaptation
|
|
|
|
single: Time objects; Adaptation
|
|
|
|
single: Interval objects; Adaptation
|
|
|
|
single: mx.DateTime; Adaptation
|
|
|
|
|
2011-02-19 19:16:28 +03:00
|
|
|
- Date and time objects: builtin `~datetime.datetime`, `~datetime.date`,
|
|
|
|
`~datetime.time`, `~datetime.timedelta` are converted into PostgreSQL's
|
2010-02-11 08:17:10 +03:00
|
|
|
:sql:`timestamp`, :sql:`date`, :sql:`time`, :sql:`interval` data types.
|
|
|
|
Time zones are supported too. The Egenix `mx.DateTime`_ objects are adapted
|
|
|
|
the same way::
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
>>> dt = datetime.datetime.now()
|
|
|
|
>>> dt
|
|
|
|
datetime.datetime(2010, 2, 8, 1, 40, 27, 425337)
|
|
|
|
|
|
|
|
>>> cur.mogrify("SELECT %s, %s, %s;", (dt, dt.date(), dt.time()))
|
|
|
|
"SELECT '2010-02-08T01:40:27.425337', '2010-02-08', '01:40:27.425337';"
|
|
|
|
|
|
|
|
>>> cur.mogrify("SELECT %s;", (dt - datetime.datetime(2010,1,1),))
|
|
|
|
"SELECT '38 days 6027.425337 seconds';"
|
|
|
|
|
2011-02-19 19:16:28 +03:00
|
|
|
.. _adapt-list:
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. index::
|
|
|
|
single: Array; Adaptation
|
2010-12-02 18:06:27 +03:00
|
|
|
double: Lists; Adaptation
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
- Python lists are converted into PostgreSQL :sql:`ARRAY`\ s::
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
>>> cur.mogrify("SELECT %s;", ([10, 20, 30], ))
|
|
|
|
'SELECT ARRAY[10, 20, 30];'
|
|
|
|
|
2012-02-24 04:33:28 +04:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Reading back from PostgreSQL, arrays are converted to list of Python
|
|
|
|
objects as expected, but only if the types are known one. Arrays of
|
|
|
|
unknown types are returned as represented by the database (e.g.
|
|
|
|
``{a,b,c}``). You can easily create a typecaster for :ref:`array of
|
|
|
|
unknown types <cast-array-unknown>`.
|
|
|
|
|
2011-02-19 19:16:28 +03:00
|
|
|
.. _adapt-tuple:
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. index::
|
2010-12-02 18:06:27 +03:00
|
|
|
double: Tuple; Adaptation
|
2010-02-09 07:58:28 +03:00
|
|
|
single: IN operator
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
- Python tuples are converted in a syntax suitable for the SQL :sql:`IN`
|
2011-01-02 02:34:13 +03:00
|
|
|
operator and to represent a composite type::
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
>>> cur.mogrify("SELECT %s IN %s;", (10, (10, 20, 30)))
|
|
|
|
'SELECT 10 IN (10, 20, 30);'
|
|
|
|
|
2010-02-10 07:03:30 +03:00
|
|
|
.. note::
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
SQL doesn't allow an empty list in the IN operator, so your code should
|
|
|
|
guard against empty tuples.
|
|
|
|
|
2011-01-02 02:34:13 +03:00
|
|
|
If you want PostgreSQL composite types to be converted into a Python
|
|
|
|
tuple/namedtuple you can use the `~psycopg2.extras.register_composite()`
|
|
|
|
function.
|
|
|
|
|
2010-02-10 07:03:30 +03:00
|
|
|
.. versionadded:: 2.0.6
|
2010-02-26 03:37:09 +03:00
|
|
|
the tuple :sql:`IN` adaptation.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-26 03:37:09 +03:00
|
|
|
.. versionchanged:: 2.0.14
|
|
|
|
the tuple :sql:`IN` adapter is always active. In previous releases it
|
|
|
|
was necessary to import the `~psycopg2.extensions` module to have it
|
|
|
|
registered.
|
2010-02-11 08:17:10 +03:00
|
|
|
|
2011-01-02 02:34:13 +03:00
|
|
|
.. versionchanged:: 2.3
|
2011-02-19 19:16:28 +03:00
|
|
|
`~collections.namedtuple` instances are adapted like regular tuples and
|
|
|
|
can thus be used to represent composite types.
|
|
|
|
|
|
|
|
.. _adapt-dict:
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: dict; Adaptation
|
|
|
|
single: hstore; Adaptation
|
2011-01-02 02:34:13 +03:00
|
|
|
|
2011-02-19 19:16:28 +03:00
|
|
|
- Python dictionaries are converted into the |hstore|_ data type. By default
|
|
|
|
the adapter is not enabled: see `~psycopg2.extras.register_hstore()` for
|
|
|
|
further details.
|
2010-09-27 05:25:09 +04:00
|
|
|
|
|
|
|
.. |hstore| replace:: :sql:`hstore`
|
2012-02-28 20:28:07 +04:00
|
|
|
.. _hstore: http://www.postgresql.org/docs/current/static/hstore.html
|
2010-09-27 05:25:09 +04:00
|
|
|
|
2010-11-05 15:38:49 +03:00
|
|
|
.. versionadded:: 2.3
|
2010-09-27 05:25:09 +04:00
|
|
|
the :sql:`hstore` adaptation.
|
|
|
|
|
2011-01-02 02:34:13 +03:00
|
|
|
|
2010-02-11 08:17:10 +03:00
|
|
|
.. index::
|
|
|
|
single: Unicode
|
|
|
|
|
|
|
|
.. _unicode-handling:
|
|
|
|
|
|
|
|
Unicode handling
|
|
|
|
^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Psycopg can exchange Unicode data with a PostgreSQL database. Python
|
2010-02-26 03:17:52 +03:00
|
|
|
`!unicode` objects are automatically *encoded* in the client encoding
|
2010-02-11 08:17:10 +03:00
|
|
|
defined on the database connection (the `PostgreSQL encoding`__, available in
|
2010-03-03 20:43:24 +03:00
|
|
|
`connection.encoding`, is translated into a `Python codec`__ using the
|
2010-02-26 03:17:52 +03:00
|
|
|
`~psycopg2.extensions.encodings` mapping)::
|
2010-02-11 08:17:10 +03:00
|
|
|
|
|
|
|
>>> print u, type(u)
|
|
|
|
àèìòù€ <type 'unicode'>
|
|
|
|
|
|
|
|
>>> cur.execute("INSERT INTO test (num, data) VALUES (%s,%s);", (74, u))
|
|
|
|
|
2012-02-28 20:28:07 +04:00
|
|
|
.. __: http://www.postgresql.org/docs/current/static/multibyte.html
|
2010-02-11 08:17:10 +03:00
|
|
|
.. __: http://docs.python.org/library/codecs.html#standard-encodings
|
|
|
|
|
2011-02-10 05:15:44 +03:00
|
|
|
When reading data from the database, in Python 2 the strings returned are
|
|
|
|
usually 8 bit `!str` objects encoded in the database client encoding::
|
2010-02-11 08:17:10 +03:00
|
|
|
|
|
|
|
>>> print conn.encoding
|
|
|
|
UTF8
|
|
|
|
|
|
|
|
>>> cur.execute("SELECT data FROM test WHERE num = 74")
|
|
|
|
>>> x = cur.fetchone()[0]
|
|
|
|
>>> print x, type(x), repr(x)
|
|
|
|
àèìòù€ <type 'str'> '\xc3\xa0\xc3\xa8\xc3\xac\xc3\xb2\xc3\xb9\xe2\x82\xac'
|
|
|
|
|
|
|
|
>>> conn.set_client_encoding('LATIN9')
|
|
|
|
|
|
|
|
>>> cur.execute("SELECT data FROM test WHERE num = 74")
|
|
|
|
>>> x = cur.fetchone()[0]
|
|
|
|
>>> print type(x), repr(x)
|
|
|
|
<type 'str'> '\xe0\xe8\xec\xf2\xf9\xa4'
|
|
|
|
|
2011-02-10 05:15:44 +03:00
|
|
|
In Python 3 instead the strings are automatically *decoded* in the connection
|
|
|
|
`~connection.encoding`, as the `!str` object can represent Unicode characters.
|
|
|
|
In Python 2 you must register a :ref:`typecaster
|
|
|
|
<type-casting-from-sql-to-python>` in order to receive `!unicode` objects::
|
2010-02-11 08:17:10 +03:00
|
|
|
|
|
|
|
>>> psycopg2.extensions.register_type(psycopg2.extensions.UNICODE, cur)
|
|
|
|
|
|
|
|
>>> cur.execute("SELECT data FROM test WHERE num = 74")
|
|
|
|
>>> x = cur.fetchone()[0]
|
|
|
|
>>> print x, type(x), repr(x)
|
|
|
|
àèìòù€ <type 'unicode'> u'\xe0\xe8\xec\xf2\xf9\u20ac'
|
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
In the above example, the `~psycopg2.extensions.UNICODE` typecaster is
|
2010-02-11 08:17:10 +03:00
|
|
|
registered only on the cursor. It is also possible to register typecasters on
|
|
|
|
the connection or globally: see the function
|
2010-02-26 03:17:52 +03:00
|
|
|
`~psycopg2.extensions.register_type()` and
|
2010-02-11 08:17:10 +03:00
|
|
|
:ref:`type-casting-from-sql-to-python` for details.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2011-11-01 11:09:51 +04:00
|
|
|
In Python 2, if you want to uniformly receive all your database input in
|
2011-02-10 05:15:44 +03:00
|
|
|
Unicode, you can register the related typecasters globally as soon as
|
|
|
|
Psycopg is imported::
|
2010-02-11 08:17:10 +03:00
|
|
|
|
|
|
|
import psycopg2
|
|
|
|
import psycopg2.extensions
|
|
|
|
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
|
|
|
|
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
|
|
|
|
|
|
|
|
and then forget about this story.
|
|
|
|
|
|
|
|
|
2010-05-20 05:07:50 +04:00
|
|
|
.. index::
|
|
|
|
single: Time Zones
|
|
|
|
|
|
|
|
.. _tz-handling:
|
|
|
|
|
|
|
|
Time zones handling
|
|
|
|
^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
The PostgreSQL type :sql:`timestamp with time zone` is converted into Python
|
2011-02-19 19:16:28 +03:00
|
|
|
`~datetime.datetime` objects with a `~datetime.datetime.tzinfo` attribute set
|
|
|
|
to a `~psycopg2.tz.FixedOffsetTimezone` instance.
|
2010-05-20 05:07:50 +04:00
|
|
|
|
|
|
|
>>> cur.execute("SET TIME ZONE 'Europe/Rome';") # UTC + 1 hour
|
|
|
|
>>> cur.execute("SELECT '2010-01-01 10:30:45'::timestamptz;")
|
|
|
|
>>> cur.fetchone()[0].tzinfo
|
|
|
|
psycopg2.tz.FixedOffsetTimezone(offset=60, name=None)
|
|
|
|
|
2011-10-15 02:59:49 +04:00
|
|
|
Note that only time zones with an integer number of minutes are supported:
|
2011-02-19 19:16:28 +03:00
|
|
|
this is a limitation of the Python `datetime` module. A few historical time
|
2010-05-20 05:07:50 +04:00
|
|
|
zones had seconds in the UTC offset: these time zones will have the offset
|
|
|
|
rounded to the nearest minute, with an error of up to 30 seconds.
|
|
|
|
|
|
|
|
>>> cur.execute("SET TIME ZONE 'Asia/Calcutta';") # offset was +5:53:20
|
|
|
|
>>> cur.execute("SELECT '1930-01-01 10:30:45'::timestamptz;")
|
|
|
|
>>> cur.fetchone()[0].tzinfo
|
|
|
|
psycopg2.tz.FixedOffsetTimezone(offset=353, name=None)
|
|
|
|
|
|
|
|
.. versionchanged:: 2.2.2
|
|
|
|
timezones with seconds are supported (with rounding). Previously such
|
|
|
|
timezones raised an error. In order to deal with them in previous
|
2011-02-19 19:16:28 +03:00
|
|
|
versions use `psycopg2.extras.register_tstz_w_secs()`.
|
2010-05-20 05:07:50 +04:00
|
|
|
|
2010-02-11 08:17:10 +03:00
|
|
|
|
2011-06-08 17:38:57 +04:00
|
|
|
.. index:: Transaction, Begin, Commit, Rollback, Autocommit, Read only
|
2010-02-14 22:42:15 +03:00
|
|
|
|
|
|
|
.. _transactions-control:
|
|
|
|
|
|
|
|
Transactions control
|
|
|
|
--------------------
|
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
In Psycopg transactions are handled by the `connection` class. By
|
2010-02-18 06:51:17 +03:00
|
|
|
default, the first time a command is sent to the database (using one of the
|
2010-02-26 03:17:52 +03:00
|
|
|
`cursor`\ s created by the connection), a new transaction is created.
|
2010-02-14 22:42:15 +03:00
|
|
|
The following database commands will be executed in the context of the same
|
|
|
|
transaction -- not only the commands issued by the first cursor, but the ones
|
|
|
|
issued by all the cursors created by the same connection. Should any command
|
|
|
|
fail, the transaction will be aborted and no further command will be executed
|
2011-06-08 17:38:57 +04:00
|
|
|
until a call to the `~connection.rollback()` method.
|
2010-02-14 22:42:15 +03:00
|
|
|
|
2012-08-15 15:25:35 +04:00
|
|
|
The connection is responsible for terminating its transaction, calling either
|
|
|
|
the `~connection.commit()` or `~connection.rollback()` method. Committed
|
2010-02-14 22:42:15 +03:00
|
|
|
changes are immediately made persistent into the database. Closing the
|
2010-02-26 03:17:52 +03:00
|
|
|
connection using the `~connection.close()` method or destroying the
|
2011-02-19 19:16:28 +03:00
|
|
|
connection object (using `!del` or letting it fall out of scope)
|
2012-08-15 15:28:13 +04:00
|
|
|
will result in an implicit rollback.
|
2010-02-14 22:42:15 +03:00
|
|
|
|
|
|
|
It is possible to set the connection in *autocommit* mode: this way all the
|
|
|
|
commands executed will be immediately committed and no rollback is possible. A
|
2010-02-18 06:51:17 +03:00
|
|
|
few commands (e.g. :sql:`CREATE DATABASE`, :sql:`VACUUM`...) require to be run
|
|
|
|
outside any transaction: in order to be able to run these commands from
|
2012-11-12 05:32:38 +04:00
|
|
|
Psycopg, the connection must be in autocommit mode: you can use the
|
2011-06-08 17:38:57 +04:00
|
|
|
`~connection.autocommit` property (`~connection.set_isolation_level()` in
|
|
|
|
older versions).
|
2010-02-14 22:42:15 +03:00
|
|
|
|
2011-06-08 17:38:57 +04:00
|
|
|
.. warning::
|
2011-06-03 04:46:56 +04:00
|
|
|
|
2011-06-08 17:38:57 +04:00
|
|
|
By default even a simple :sql:`SELECT` will start a transaction: in
|
|
|
|
long-running programs, if no further action is taken, the session will
|
|
|
|
remain "idle in transaction", a condition non desiderable for several
|
|
|
|
reasons (locks are held by the session, tables bloat...). For long lived
|
2011-11-01 11:09:51 +04:00
|
|
|
scripts, either make sure to terminate a transaction as soon as possible or
|
2011-06-08 17:38:57 +04:00
|
|
|
use an autocommit connection.
|
|
|
|
|
|
|
|
A few other transaction properties can be set session-wide by the
|
|
|
|
`!connection`: for instance it is possible to have read-only transactions or
|
|
|
|
change the isolation level. See the `~connection.set_session()` method for all
|
|
|
|
the details.
|
2010-02-14 22:42:15 +03:00
|
|
|
|
|
|
|
|
2012-12-03 07:18:51 +04:00
|
|
|
.. index::
|
|
|
|
single: with statement
|
|
|
|
|
|
|
|
``with`` statement
|
|
|
|
^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Starting from version 2.5, psycopg2's connections and cursors are *context
|
|
|
|
managers* and can be used with the ``with`` statement::
|
|
|
|
|
|
|
|
with psycopg2.connect(DSN) as conn:
|
|
|
|
with conn.cursor() as curs:
|
|
|
|
curs.execute(SQL)
|
|
|
|
|
|
|
|
When a connection exits the ``with`` block, if no exception has been raised by
|
|
|
|
the block, the transaction is committed. In case of exception the transaction
|
|
|
|
is rolled back. In no case the connection is closed: a connection can be used
|
|
|
|
in more than a ``with`` statement and each ``with`` block is effectively
|
|
|
|
wrapped in a transaction.
|
|
|
|
|
|
|
|
When a cursor exits the ``with`` block it is closed, releasing any resource
|
|
|
|
eventually associated with it. The state of the transaction is not affected.
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. index::
|
|
|
|
pair: Server side; Cursor
|
|
|
|
pair: Named; Cursor
|
|
|
|
pair: DECLARE; SQL command
|
|
|
|
pair: FETCH; SQL command
|
|
|
|
pair: MOVE; SQL command
|
|
|
|
|
|
|
|
.. _server-side-cursors:
|
|
|
|
|
|
|
|
Server side cursors
|
|
|
|
-------------------
|
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
When a database query is executed, the Psycopg `cursor` usually fetches
|
2010-02-11 06:15:14 +03:00
|
|
|
all the records returned by the backend, transferring them to the client
|
|
|
|
process. If the query returned an huge amount of data, a proportionally large
|
|
|
|
amount of memory will be allocated by the client.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-09 16:33:31 +03:00
|
|
|
If the dataset is too large to be practically handled on the client side, it is
|
2010-02-09 07:58:28 +03:00
|
|
|
possible to create a *server side* cursor. Using this kind of cursor it is
|
|
|
|
possible to transfer to the client only a controlled amount of data, so that a
|
|
|
|
large dataset can be examined without keeping it entirely in memory.
|
|
|
|
|
|
|
|
Server side cursor are created in PostgreSQL using the |DECLARE|_ command and
|
2010-02-11 06:15:14 +03:00
|
|
|
subsequently handled using :sql:`MOVE`, :sql:`FETCH` and :sql:`CLOSE` commands.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-11 08:17:10 +03:00
|
|
|
Psycopg wraps the database server side cursor in *named cursors*. A named
|
2010-02-26 03:17:52 +03:00
|
|
|
cursor is created using the `~connection.cursor()` method specifying the
|
2011-10-15 02:17:24 +04:00
|
|
|
*name* parameter. Such cursor will behave mostly like a regular cursor,
|
2010-02-26 03:17:52 +03:00
|
|
|
allowing the user to move in the dataset using the `~cursor.scroll()`
|
2010-03-03 20:43:24 +03:00
|
|
|
method and to read the data using `~cursor.fetchone()` and
|
2012-08-15 14:26:45 +04:00
|
|
|
`~cursor.fetchmany()` methods. Normally you can only scroll forward in a
|
|
|
|
cursor: if you need to scroll backwards you should declare your cursor
|
|
|
|
`~cursor.scrollable`.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2011-02-17 15:29:07 +03:00
|
|
|
Named cursors are also :ref:`iterable <cursor-iterable>` like regular cursors.
|
2011-10-15 02:59:49 +04:00
|
|
|
Note however that before Psycopg 2.4 iteration was performed fetching one
|
2011-02-17 15:29:07 +03:00
|
|
|
record at time from the backend, resulting in a large overhead. The attribute
|
2011-10-15 02:59:49 +04:00
|
|
|
`~cursor.itersize` now controls how many records are fetched at time
|
2011-02-17 15:29:07 +03:00
|
|
|
during the iteration: the default value of 2000 allows to fetch about 100KB
|
|
|
|
per roundtrip assuming records of 10-20 columns of mixed number and strings;
|
|
|
|
you may decrease this value if you are dealing with huge records.
|
|
|
|
|
2011-08-10 21:21:12 +04:00
|
|
|
Named cursors are usually created :sql:`WITHOUT HOLD`, meaning they live only
|
|
|
|
as long as the current transaction. Trying to fetch from a named cursor after
|
|
|
|
a `~connection.commit()` or to create a named cursor when the `connection`
|
|
|
|
transaction isolation level is set to `AUTOCOMMIT` will result in an exception.
|
|
|
|
It is possible to create a :sql:`WITH HOLD` cursor by specifying a `!True`
|
|
|
|
value for the `withhold` parameter to `~connection.cursor()` or by setting the
|
|
|
|
`~cursor.withhold` attribute to `!True` before calling `~cursor.execute()` on
|
|
|
|
the cursor. It is extremely important to always `~cursor.close()` such cursors,
|
|
|
|
otherwise they will continue to hold server-side resources until the connection
|
2011-10-15 02:59:49 +04:00
|
|
|
will be eventually closed. Also note that while :sql:`WITH HOLD` cursors
|
2011-08-10 21:21:12 +04:00
|
|
|
lifetime extends well after `~connection.commit()`, calling
|
|
|
|
`~connection.rollback()` will automatically close the cursor.
|
|
|
|
|
2011-10-15 02:17:24 +04:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
It is also possible to use a named cursor to consume a cursor created
|
|
|
|
in some other way than using the |DECLARE| executed by
|
|
|
|
`~cursor.execute()`. For example, you may have a PL/pgSQL function
|
|
|
|
returning a cursor::
|
|
|
|
|
|
|
|
CREATE FUNCTION reffunc(refcursor) RETURNS refcursor AS $$
|
|
|
|
BEGIN
|
|
|
|
OPEN $1 FOR SELECT col FROM test;
|
|
|
|
RETURN $1;
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
You can read the cursor content by calling the function with a regular,
|
|
|
|
non-named, Psycopg cursor:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
cur1 = conn.cursor()
|
|
|
|
cur1.callproc('reffunc', ['curname'])
|
|
|
|
|
|
|
|
and then use a named cursor in the same transaction to "steal the cursor":
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
cur2 = conn.cursor('curname')
|
|
|
|
for record in cur2: # or cur2.fetchone, fetchmany...
|
|
|
|
# do something with record
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
.. |DECLARE| replace:: :sql:`DECLARE`
|
2012-02-28 20:28:07 +04:00
|
|
|
.. _DECLARE: http://www.postgresql.org/docs/current/static/sql-declare.html
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-06 02:58:10 +03:00
|
|
|
.. index:: Thread safety, Multithread, Multiprocess
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
.. _thread-safety:
|
|
|
|
|
2010-11-06 02:58:10 +03:00
|
|
|
Thread and process safety
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
The Psycopg module and the `connection` objects are *thread-safe*: many
|
|
|
|
threads can access the same database either using separate sessions and
|
2011-11-01 11:09:51 +04:00
|
|
|
creating a `!connection` per thread or using the same
|
2010-11-06 02:58:10 +03:00
|
|
|
connection and creating separate `cursor`\ s. In |DBAPI|_ parlance, Psycopg is
|
|
|
|
*level 2 thread safe*.
|
|
|
|
|
|
|
|
The difference between the above two approaches is that, using different
|
|
|
|
connections, the commands will be executed in different sessions and will be
|
|
|
|
served by different server processes. On the other hand, using many cursors on
|
|
|
|
the same connection, all the commands will be executed in the same session
|
|
|
|
(and in the same transaction if the connection is not in :ref:`autocommit
|
|
|
|
<transactions-control>` mode), but they will be serialized.
|
|
|
|
|
|
|
|
The above observations are only valid for regular threads: they don't apply to
|
|
|
|
forked processes nor to green threads. `libpq` connections `shouldn't be used by a
|
2011-02-19 19:16:28 +03:00
|
|
|
forked processes`__, so when using a module such as `multiprocessing` or a
|
2011-11-01 11:09:51 +04:00
|
|
|
forking web deploy method such as FastCGI make sure to create the connections
|
2010-11-06 02:58:10 +03:00
|
|
|
*after* the fork.
|
|
|
|
|
2012-02-28 20:28:07 +04:00
|
|
|
.. __: http://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNECT
|
2010-11-06 02:58:10 +03:00
|
|
|
|
2011-06-05 19:22:54 +04:00
|
|
|
Connections shouldn't be shared either by different green threads: see
|
|
|
|
:ref:`green-support` for further details.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-02-10 07:03:30 +03:00
|
|
|
.. index::
|
2010-02-09 07:58:28 +03:00
|
|
|
pair: COPY; SQL command
|
|
|
|
|
|
|
|
.. _copy:
|
|
|
|
|
|
|
|
Using COPY TO and COPY FROM
|
|
|
|
---------------------------
|
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
Psycopg `cursor` objects provide an interface to the efficient
|
2010-02-09 19:30:52 +03:00
|
|
|
PostgreSQL |COPY|__ command to move data from files to tables and back.
|
|
|
|
The methods exposed are:
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
`~cursor.copy_from()`
|
2010-02-09 19:30:52 +03:00
|
|
|
Reads data *from* a file-like object appending them to a database table
|
2010-02-11 06:15:14 +03:00
|
|
|
(:sql:`COPY table FROM file` syntax). The source file must have both
|
2010-02-26 03:17:52 +03:00
|
|
|
`!read()` and `!readline()` method.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
`~cursor.copy_to()`
|
2010-02-11 06:15:14 +03:00
|
|
|
Writes the content of a table *to* a file-like object (:sql:`COPY table TO
|
2010-02-26 03:17:52 +03:00
|
|
|
file` syntax). The target file must have a `write()` method.
|
2010-02-10 03:10:51 +03:00
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
`~cursor.copy_expert()`
|
2010-02-11 06:15:14 +03:00
|
|
|
Allows to handle more specific cases and to use all the :sql:`COPY`
|
|
|
|
features available in PostgreSQL.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-09 19:30:52 +03:00
|
|
|
Please refer to the documentation of the single methods for details and
|
|
|
|
examples.
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
.. |COPY| replace:: :sql:`COPY`
|
2012-02-28 20:28:07 +04:00
|
|
|
.. __: http://www.postgresql.org/docs/current/static/sql-copy.html
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
|
2010-02-14 22:42:15 +03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: Large objects
|
|
|
|
|
|
|
|
.. _large-objects:
|
|
|
|
|
|
|
|
Access to PostgreSQL large objects
|
|
|
|
----------------------------------
|
|
|
|
|
2011-11-01 11:09:51 +04:00
|
|
|
PostgreSQL offers support for `large objects`__, which provide stream-style
|
2010-02-14 22:42:15 +03:00
|
|
|
access to user data that is stored in a special large-object structure. They
|
|
|
|
are useful with data values too large to be manipulated conveniently as a
|
|
|
|
whole.
|
|
|
|
|
2012-02-28 20:28:07 +04:00
|
|
|
.. __: http://www.postgresql.org/docs/current/static/largeobjects.html
|
2010-02-14 22:42:15 +03:00
|
|
|
|
|
|
|
Psycopg allows access to the large object using the
|
2010-02-26 03:17:52 +03:00
|
|
|
`~psycopg2.extensions.lobject` class. Objects are generated using the
|
2011-01-05 03:05:29 +03:00
|
|
|
`connection.lobject()` factory method. Data can be retrieved either as bytes
|
|
|
|
or as Unicode strings.
|
2010-02-14 22:42:15 +03:00
|
|
|
|
|
|
|
Psycopg large object support efficient import/export with file system files
|
|
|
|
using the |lo_import|_ and |lo_export|_ libpq functions.
|
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
.. |lo_import| replace:: `!lo_import()`
|
2012-02-28 20:28:07 +04:00
|
|
|
.. _lo_import: http://www.postgresql.org/docs/current/static/lo-interfaces.html#LO-IMPORT
|
2010-02-26 03:17:52 +03:00
|
|
|
.. |lo_export| replace:: `!lo_export()`
|
2012-02-28 20:28:07 +04:00
|
|
|
.. _lo_export: http://www.postgresql.org/docs/current/static/lo-interfaces.html#LO-EXPORT
|
2010-10-12 05:46:38 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
pair: Two-phase commit; Transaction
|
|
|
|
|
|
|
|
.. _tpc:
|
|
|
|
|
|
|
|
Two-Phase Commit protocol support
|
|
|
|
---------------------------------
|
|
|
|
|
2010-11-05 15:38:49 +03:00
|
|
|
.. versionadded:: 2.3
|
2010-10-12 05:46:38 +04:00
|
|
|
|
2010-10-16 05:31:51 +04:00
|
|
|
Psycopg exposes the two-phase commit features available since PostgreSQL 8.1
|
2010-10-12 05:46:38 +04:00
|
|
|
implementing the *two-phase commit extensions* proposed by the |DBAPI|.
|
|
|
|
|
2011-11-01 11:09:51 +04:00
|
|
|
The |DBAPI| model of two-phase commit is inspired by the `XA specification`__,
|
2010-10-12 05:46:38 +04:00
|
|
|
according to which transaction IDs are formed from three components:
|
|
|
|
|
|
|
|
- a format ID (non-negative 32 bit integer)
|
|
|
|
- a global transaction ID (string not longer than 64 bytes)
|
|
|
|
- a branch qualifier (string not longer than 64 bytes)
|
|
|
|
|
|
|
|
For a particular global transaction, the first two components will be the same
|
|
|
|
for all the resources. Every resource will be assigned a different branch
|
|
|
|
qualifier.
|
|
|
|
|
|
|
|
According to the |DBAPI| specification, a transaction ID is created using the
|
|
|
|
`connection.xid()` method. Once you have a transaction id, a distributed
|
|
|
|
transaction can be started with `connection.tpc_begin()`, prepared using
|
|
|
|
`~connection.tpc_prepare()` and completed using `~connection.tpc_commit()` or
|
|
|
|
`~connection.tpc_rollback()`. Transaction IDs can also be retrieved from the
|
|
|
|
database using `~connection.tpc_recover()` and completed using the above
|
|
|
|
`!tpc_commit()` and `!tpc_rollback()`.
|
|
|
|
|
|
|
|
PostgreSQL doesn't follow the XA standard though, and the ID for a PostgreSQL
|
2010-10-16 05:31:51 +04:00
|
|
|
prepared transaction can be any string up to 200 characters long.
|
|
|
|
Psycopg's `~psycopg2.extensions.Xid` objects can represent both XA-style
|
|
|
|
transactions IDs (such as the ones created by the `!xid()` method) and
|
|
|
|
PostgreSQL transaction IDs identified by an unparsed string.
|
2010-10-12 05:46:38 +04:00
|
|
|
|
|
|
|
The format in which the Xids are converted into strings passed to the
|
|
|
|
database is the same employed by the `PostgreSQL JDBC driver`__: this should
|
|
|
|
allow interoperation between tools written in Python and in Java. For example
|
|
|
|
a recovery tool written in Python would be able to recognize the components of
|
|
|
|
transactions produced by a Java program.
|
|
|
|
|
|
|
|
For further details see the documentation for the above methods.
|
|
|
|
|
|
|
|
.. __: http://www.opengroup.org/bookstore/catalog/c193.htm
|
|
|
|
.. __: http://jdbc.postgresql.org/
|
|
|
|
|