drziff
drziff

Reputation: 23

psycopg2 connect gives operational error unsupported frontend protocol

I'm trying to connect to a postgres 12.2 database from anaconda python 3.8.2 on Windows 10. I am using a conda environment with the following packages installed:

asgiref==3.2.7
certifi==2020.4.5.1
Django==3.0.4
django-crispy-forms==1.8.1
Jinja2==2.11.1
MarkupSafe==1.1.1
psycopg2==2.8.5
pytz==2019.3
sqlparse==0.3.1
wincertstore==0.2

I have created a database and I can connect using psql and pgAdmin. At the python prompt I can import psycopg2 but I get an error connecting to the database:

>>> import psycopg2
>>> c = psycopg2.connect("host='localhost' dbname='mydb' user='myuser' password='secret'")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "J:\Programs\Anaconda3\envs\myenv\lib\site-packages\psycopg2\__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL:  unsupported frontend protocol 1234.5679: server supports 2.0 to 3.0

Upvotes: 0

Views: 4452

Answers (2)

drziff
drziff

Reputation: 23

Following @jjanes suggestion and the related answer here I disabled ssl and gss:

>>> import psycopg2
>>> c = psycopg2.connect("host='localhost' dbname='mydb' user='myuser' password='secret' sslmode='disable' gssencmode='disable'")

This works, although note that you do not need to disable both sslmode and gssencmode, disabling either one individually also works.

For Django users, you just need to add the relevant option to settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'secret',
        'HOST': 'localhost',
        'PORT': '5432',
        'OPTIONS': {'sslmode':'disable'},
    }
}

Upvotes: 2

jjanes
jjanes

Reputation: 44137

This is a known bug where the client thinks it can use GSS encryption, but then discovers that it can't (either the server doesn't support it, or doesn't like the credentials) and so tries to renegotiate to ssl encryption.

You usually have to go out of your way to make the client think it can use GSS encryption (except on Mac, where some buggy mac libraries make it always think it can). Have you intentionally set up GSS/kerberos in your Anaconda environment?

Upvotes: 0

Related Questions