psycopg2.OperationalError: FATAL: unsupported frontend protocol 1234.5679: server supports 2.0 to 3.0

后端 未结 5 1803
有刺的猬
有刺的猬 2020-11-27 22:54

I\'m using Macbook

Psycopg2 works well when connecting the localhost db (PostgreSQL on Mac). The error was raised when I tried to connect PostgreSQL db on a Windows1

5条回答
  •  不知归路
    2020-11-27 23:38

    In order to have a SSL connection working and using certificates, you can disable GSS connection mode to avoid the client to attempt to connect with GSS and directly try a SSL connection (Postgres version 12 < 12.3, I had no such issues with a test on version 11).

    Below an example with the verify-ca option, providing filenames for certificates and key (example names from GCP Cloud SQL, running Postgres 12.1 when posted).

       from sqlalchemy import create_engine
    
       db_user = 'user'
       db_pwd = 'secret'
       db_host = 'hostname'
       db_port = '5432'
       db_name = 'test'
    
       cnn = f'postgresql://{db_user}:{db_pwd}@{db_host}:{db_port}/{db_name}'
       ssl_args = {
           'gssencmode': 'disable',
           'sslmode': 'verify-ca',
           'sslrootcert': 'server-ca.pem',
           'sslcert': 'client-cert.pem',
           'sslkey': 'client-key.pem',
       }
       
       engine = create_engine(cnn, connect_args=ssl_args)
    

    This engine can then be used with pandas for example:

       df.to_sql('my_table', con=engine)
    

提交回复
热议问题