PostgreSQL Connection Error

I have not been able to connect Prodigy to my PostgreSQL database. Here is the error I’m getting.

>>> from prodigy.components.db import connect
>>> db = connect()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/prodigy/components/db.py", line 58, in connect
    _DB = Database(db, db_id, db_name)
  File "/usr/local/lib/python3.6/site-packages/prodigy/components/db.py", line 123, in __init__
    DB_PROXY.initialize(db)
  File "/usr/local/lib/python3.6/site-packages/peewee.py", line 431, in initialize
    callback(obj)
  File "/usr/local/lib/python3.6/site-packages/peewee.py", line 1197, in _set_constructor
    self._constructor = database.get_binary_type()
  File "/usr/local/lib/python3.6/site-packages/peewee.py", line 4281, in get_binary_type
    return psycopg2.Binary
AttributeError: 'NoneType' object has no attribute 'Binary'

PostgreSQL Version 9.4

Hi! This looks like it could potentially be related to the driver not being available. Can you check that psycopg2 is installed in the same environment you’re using for Prodigy and that you can interact with it from Python?

If just had a look for the error online, specifically relating to peewee, and found this thread, which describes a similar issue: if the driver is not installed or importable, it defaults to None, causing an error like the one above.

Looks like you’re right - I didn’t think I needed to install it manually but that make sense (I’m not much of a python guy).

1 Like

No worries and sorry if this was unclear! :+1:

(Prodigy doesn’t auto-install any database drivers – there are different options and we generally want to leave this up to the user and keep the default installation slim and simple.)

Hi all – I’m getting the same error, but installing psycopg2 isn’t working. I’m using PyCharm. Prodigy is installed and available in the virtual environment I’m using to run the test database script. psycopg2 is also installed and available. (I double-checked that they’re both installed and available by re-installing them, both from within PyCharm and from the command-line). However, when the script tries to execute ‘db.connect()’ I get the same error: “‘NoneType’ object has no attribute ‘Binary’”. Any ideas? I’m attaching a screen shot of the package listing for the virtual env, and the stack trace from the error.

@jsidell Yes, this is definitely strange – so you’ve also tested adding import psycopg2 to your file and that succeeded without an import error? And can you connect to your DB manually, e.g. like this?

HI @ines – yes, adding ‘import psycopg2’ to my file doesn’t fix the error (and PyCharm highlights it as an unused import); I can connect to Postgres through psql, using the database, UID and PWD in the prodigy.json config file…

Ah, I wasn’t suggesting this would fix the problem. But if for whatever reason psycopg2 wasn’t available in your environment, trying to import it in your file would raise an ImportError. Ultimately, all peewee does is this – it tries to import it and if that fails, it sets it to None.

I had another closer look at your paths, though, and I think that’s where the problem is: If you compare the path to the Python environment in your first screenshot to the path it’s using in your second screenshot, you see that those paths don’t match. The peewee that gets executed here is in a different Python environment and not in the Python 3.7 venv you’re showing in the first screenshot.

True, the paths are different, but the Python interpreter referenced in the first screen shot includes the path referenced in the second:

Hi Jeff,

I really think this is a general installation configuration issue, between your editor and your Python environment. As I said in my email to you, I’m afraid this stretches beyond the support we usually offer. We really can’t offer end-to-end support that extends past our software to general things like Python, psycopg2, etc.

We do actually try to help a little bit more with general machine learning advice, even if it’s not specific to our software, because of course we want to help people succeed in their projects. But we have to have some sort of scope limitations. If nobody on your team can help you, perhaps JetBrains support might be a better place to ask?

Understood – I think the issue is a misconfigured install of psycopg2 – so not on the Prodigy side, but on the Postgres side. The reason psycopg2 = None is due to a failed import statement; The failure is caused by psycopg2 not finding or being able to load the Postgres dynamically-loaded library libpq.5.dylib. This error was masked by the import being contained by a try/except block.