
Do not be tempted to pass a query with Python-formatted values, as this is error prone and a safety issue ("SQL injection"), even if it looks simpler: con = pg. Note that we always passed the parameters separately. The PyGreSQL DB API 2 module (pgdb) also uses Python formatting and works similarly: con = nnect(cs)Ĭur.execute('select sum(column1) from table_name where column2 = any(%s)', ) 'select sum(column1) from table_name where column2 = any(%s)', ) 'select sum(column1) from table_name where column2 in (%s)' % p_list, tup_ids)īy using ANY instead of IN in your SQL statement, you can avoid creating a parameter list, and pass the values as a single list: db = pg.DB(cs) Setting up the Python virtual environment and installing a PostgreSQL.
Pygresql how to#
This means that if you switch from one module to another, you can reuse almost all of your existing code (the code sample below demonstrates how to do this). Both of these packages support Python's portable SQL database API. This method uses Python formatting, so it must be used like this: db = pg.DB(cs) PyGreSQL: This package contains the pgdb module. It is based on the PyGres95 code written. PyGreSQL should run on most platforms where PostgreSQL and Python is running. It wraps the lower level C API library libpq to allow easy use of the powerful PostgreSQL features from Python. PyGreSQL is a Python module that interfaces to a PostgreSQL database.

Q = con.query('select sum(column1) from table_name where column2 in (%s)' % p_list, tup_ids)Īlternatively, you can use the query_formatted method available in the DB wrapper class of PyGreSQL classic (it is recommended to use that wrapper instead of raw connections anyway, because it adds a lot of convenience methods). PyGreSQL - Python interface for PostgreSQL. gpdb/gpload. An open-source massively parallel data platform for analytics, machine learning and AI. P_list = ','.join('$%d' % n for n in range(1, len(tup_ids) + 1)) Greenplum Database - Massively Parallel PostgreSQL for Analytics. This is very simple, though: con = pg.connect(cs) Unfortunately, these accept only individual values, so you must construct a parameter list with as many values as your tuple first. PyGreSQL "classic" (the pg module) supports PostgreSQL native parameters in its query method, labelled $1, $2.
