Search code examples
postgresqlpygresql

PyGreSQL - Unable to Insert Data


I am using CentOS-6 with Python 3.8, PostgreSQL 12 and PyGreSQL 5.2.2. This is the error I get trying to insert data into my database.

>>> db.insert('settings', None, **data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/site-packages/pg.py", line 2322, in insert
    adapt = params.add
  File "/usr/local/lib/python3.8/site-packages/pg.py", line 1667, in __getattr__
    if self.db:
AttributeError: 'pg.Connection' object has no attribute 'escape_identifier'

Can anyone point me towards what is wrong?


Solution

  • The method escape_identifier on the connection corresponds to the function PQescapeIdentifier in libpq. It exists since PostgreSQL 9 which is required by PyGreSQL 5.2. PyGreSQL does not compile support for these functions if it is installed with the no-escaping-funcs option or if it thinks PostgreSQL is older than version 9.

    Since you say that you have PostgreSQL 12 installed, a possible explanation could be that you still have the client library (libpq) of an old PostgreSQL version installed as well, and the pg_config tool reports that old version. You should check this with pg_config --version. The pg_config tool is in libpq-devel, so make sure you have libpq and libpq-devel installed for PostgreSQL 12.

    You can also try to explicitly enable the escaping functions by installing PyGreSQL with python setup.py build_ext --escaping-funcs install, but you will still need the latest libpq, and there will be other functions disabled as well when PyGreSQL thinks that libpq is too old. So the proper solution is to make sure libpq is up to date and pg_config --version reports the corresponding PostgreSQL version.