PostgreSQL database access simplified.
Queries is an opinionated wrapper for interfacing with PostgreSQL that offers caching of connections and support for PyPy via psycopg2ct. Queries supports Python versions 2.6+ and 3.2+.
The core queries.Queries class will automatically register support for UUIDs, Unicode and Unicode arrays.
Without requiring any additional code, queries offers connection pooling that allows for multiple modules in the same interpreter to use the same PostgreSQL connection.
Documentation is available at https://queries.readthedocs.org
queries is available via pypi and can be installed with easy_install or pip:
pip install queries
- psycopg2 (for cpython support)
- psycopg2ct (for PyPy support)
Executing a query and fetching data, connecting by default to localhost as the current user with a database matching the username:
>>> import pprint
>>> import queries
>>>
>>> for row in queries.query('SELECT * FROM names'):
... pprint.pprint(row)
...
{'id': 1, 'name': u'Jacob'}
{'id': 2, 'name': u'Mason'}
{'id': 3, 'name': u'Ethan'}
Iterate over the results from calling a stored procedure:
>>> import pprint
>>> import queries
>>>
>>> for row in queries.callproc('now'):
... pprint.pprint(row)
...
{'now': datetime.datetime(2014, 4, 25, 12, 52, 0, 133279,
tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=-240, name=None))}
Using the Session object as a context manager:
>>> import pprint
>>> import queries
>>>
>>> with queries.Session() as s:
... for row in s.query('SELECT * FROM names'):
... pprint.pprint(row)
...
{'id': 1, 'name': u'Jacob'}
{'id': 2, 'name': u'Mason'}
{'id': 3, 'name': u'Ethan'}
Queries is inspired by Kenneth Reitz's awesome work on requests.
Queries is a fork and enhancement of pgsql_wrapper, which can be found in the main GitHub repository of Queries as tags prior to version 1.2.0.