Empty OperationalError: when trying to connect to local PostgreSQL database [duplicate] - python

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 Windows10.
the following code is what I have for connection, the host is just the IP of the windows10
db= psycopg2.connect(database='dbname',user='username',password="secret",host="192.168.3.9",port="5432")
Errors:
File "path/to/psycopg2/__init__.py", line 126, 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
Is this because of system compatibility or something else? I've tried other Windows machine and I got no luck to make it work.
However, I was able to connect PostgreSQL on windows while I using Node.JS module pg

1234.5679 is the special code sent by the client to request SSL-encrypted database connections, and support for that has been in PostgreSQL since commit e0e7daef6da in 1999. But your PostgreSQL cannot be that old, because support for protocol version 3.0 was not added before 2003.
Actually, from studying src/backend/postmaster/postmaster.c and reading the mailing list, this is a bug on the PostgreSQL server:
The client must be configured to try GSS authentication, and when the server rejects, it wants to negotiate an SSL connections, but the server doesn't expect that at this point; hence the error.
See the discussion here. The bug has been fixed with release 12.3.
As a workaround, disable either GSS authentication or SSL negotiation on the client.
In psycopg2, disabling SSL is done by using sslmode="disable" in the connection string, and disabling GSS is done with gssencmode="disable". See the documentation for details.

Adding ?gssencmode=disable to the connection string worked for me:
import pyodbc
from sqlalchemy import create_engine
engine = create_engine(f'postgresql://{user}:{password}#localhost:5432/database_name?gssencmode=disable')

Getting a similar error working with Laravel and Postgres. Solved it by putting this in my .env file: PGGSSENCMODE=disable

If you installed this Psycopg2 module through conda command, then uninstall that module and install using pip command.
Command:
pip install Psycopg2
The above command may resolve your issue. I resolved through this step

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)

Using PostgresSQL 13.0 I had the same problem, displaying the error messages:
unsupported frontend protocol 255.255: server supports 2.0
unsupported frontend protocol 0.0: server supports 2.0 to
according to this site it postgresql.org deals with the SSL / GSS Protocol Negotiation Problem. Which should already be resolved in Postgres version 12.0
I was able to solve it following the guidelines contained in these sites:
highgo.ca
In the postgres terminal, I executed the command below setting the environment variable gssencmode = disable and the problem was solved:
psql -h localhost -U postgres -d "dbname=belez gssencmode=disable";

Related

Empty OperationalError psycopg2.connect [duplicate]

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 Windows10.
the following code is what I have for connection, the host is just the IP of the windows10
db= psycopg2.connect(database='dbname',user='username',password="secret",host="192.168.3.9",port="5432")
Errors:
File "path/to/psycopg2/__init__.py", line 126, 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
Is this because of system compatibility or something else? I've tried other Windows machine and I got no luck to make it work.
However, I was able to connect PostgreSQL on windows while I using Node.JS module pg
1234.5679 is the special code sent by the client to request SSL-encrypted database connections, and support for that has been in PostgreSQL since commit e0e7daef6da in 1999. But your PostgreSQL cannot be that old, because support for protocol version 3.0 was not added before 2003.
Actually, from studying src/backend/postmaster/postmaster.c and reading the mailing list, this is a bug on the PostgreSQL server:
The client must be configured to try GSS authentication, and when the server rejects, it wants to negotiate an SSL connections, but the server doesn't expect that at this point; hence the error.
See the discussion here. The bug has been fixed with release 12.3.
As a workaround, disable either GSS authentication or SSL negotiation on the client.
In psycopg2, disabling SSL is done by using sslmode="disable" in the connection string, and disabling GSS is done with gssencmode="disable". See the documentation for details.
Adding ?gssencmode=disable to the connection string worked for me:
import pyodbc
from sqlalchemy import create_engine
engine = create_engine(f'postgresql://{user}:{password}#localhost:5432/database_name?gssencmode=disable')
Getting a similar error working with Laravel and Postgres. Solved it by putting this in my .env file: PGGSSENCMODE=disable
If you installed this Psycopg2 module through conda command, then uninstall that module and install using pip command.
Command:
pip install Psycopg2
The above command may resolve your issue. I resolved through this step
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)
Using PostgresSQL 13.0 I had the same problem, displaying the error messages:
unsupported frontend protocol 255.255: server supports 2.0
unsupported frontend protocol 0.0: server supports 2.0 to
according to this site it postgresql.org deals with the SSL / GSS Protocol Negotiation Problem. Which should already be resolved in Postgres version 12.0
I was able to solve it following the guidelines contained in these sites:
highgo.ca
In the postgres terminal, I executed the command below setting the environment variable gssencmode = disable and the problem was solved:
psql -h localhost -U postgres -d "dbname=belez gssencmode=disable";

Unable to build SQL connection using Python server on Unix

I'm trying to connect to SQL Server through Python. I've installed pyodbc library at my user location using the following command:
! pip install pyodbc --user
I'm running python on a unix server and trying to build the connection using the following string :
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};'
'Server=server_name;'
'Database=db;',user = 'user',password ='pwd')
I'm able to connect to SQL Server while running python on windows on my local machine.
I believe the error is due to the missing driver, I downloaded some jar files, though I'm not sure how to set it up and setting the path, etc.
go to the path
/etc/odbc.ini
and create your data source name with your credentials
Driver = your_installed_driver
Server = my_machine\SQLEXPRESS
User = my_domain\my_user
Password = my_password
Database = your_database

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

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 Windows10.
the following code is what I have for connection, the host is just the IP of the windows10
db= psycopg2.connect(database='dbname',user='username',password="secret",host="192.168.3.9",port="5432")
Errors:
File "path/to/psycopg2/__init__.py", line 126, 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
Is this because of system compatibility or something else? I've tried other Windows machine and I got no luck to make it work.
However, I was able to connect PostgreSQL on windows while I using Node.JS module pg
1234.5679 is the special code sent by the client to request SSL-encrypted database connections, and support for that has been in PostgreSQL since commit e0e7daef6da in 1999. But your PostgreSQL cannot be that old, because support for protocol version 3.0 was not added before 2003.
Actually, from studying src/backend/postmaster/postmaster.c and reading the mailing list, this is a bug on the PostgreSQL server:
The client must be configured to try GSS authentication, and when the server rejects, it wants to negotiate an SSL connections, but the server doesn't expect that at this point; hence the error.
See the discussion here. The bug has been fixed with release 12.3.
As a workaround, disable either GSS authentication or SSL negotiation on the client.
In psycopg2, disabling SSL is done by using sslmode="disable" in the connection string, and disabling GSS is done with gssencmode="disable". See the documentation for details.
Adding ?gssencmode=disable to the connection string worked for me:
import pyodbc
from sqlalchemy import create_engine
engine = create_engine(f'postgresql://{user}:{password}#localhost:5432/database_name?gssencmode=disable')
Getting a similar error working with Laravel and Postgres. Solved it by putting this in my .env file: PGGSSENCMODE=disable
If you installed this Psycopg2 module through conda command, then uninstall that module and install using pip command.
Command:
pip install Psycopg2
The above command may resolve your issue. I resolved through this step
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)
Using PostgresSQL 13.0 I had the same problem, displaying the error messages:
unsupported frontend protocol 255.255: server supports 2.0
unsupported frontend protocol 0.0: server supports 2.0 to
according to this site it postgresql.org deals with the SSL / GSS Protocol Negotiation Problem. Which should already be resolved in Postgres version 12.0
I was able to solve it following the guidelines contained in these sites:
highgo.ca
In the postgres terminal, I executed the command below setting the environment variable gssencmode = disable and the problem was solved:
psql -h localhost -U postgres -d "dbname=belez gssencmode=disable";

Conecting to MySQL in a remote server from python

I'm using Python 3.5, pymysql 0.7.6 on MacOS X 10.12.
I'm trying to use python to access a MySQL database in a remote server. I have no problems to access from the command line using:
ssh root#XXX.XXX.XXX.XXX
root#XXX.XXX.XXX.XXX's password: my_server_password
and then in the server:
mysql my_database -p
Enter password: my_database_password
And it works and I can do all sort of things with my database. Now, I try to do the same within python, following the documentation or the numerous examples I've found in other posts here:
import pymysql
cnx = pymysql.connect(host='XXX.XXX.XXX.XXX', port='3306', user='root', password='my_server_password', db='my_database')
And it does not work, getting as error:
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'XXX.XXX.XXX.XXX' ([Errno 61] Connection refused)")
The credentials are the correct credentials and I've checked that the port is the correct port, as it is suggested in other posts. I suspect it might be related with the database having also a password, not just the server, but I haven't found any way of including both passwords. Indeed, I'm not sure which password should be included in the connect command, if the server password or the database password. It does not work with neither of them.
So, do you have any suggestion about what might be the issue here or if I'm missing an important bit?
When you run the mysql command, you are doing this in an SSH shell. That is you are connecting to the server running on the remote machine via a localhost connection. That remote server doesn't seem to be set up to allow remote connections to it, only connections from the machine itself.
You'll need to have your python script connect to the MySQL server the same way you are, via SSH. You can open an SSH tunnel to port 3306 on the remote server.
The module I like to use for this purpose is: https://pypi.python.org/pypi/sshtunnel
from sshtunnel import SSHTunnelForwarder
import pymysql
server = SSHTunnelForwarder(
'XXX.XXX.XXX.XXX',
ssh_username='root',
ssh_password='my_server_password',
remote_bind_address=('127.0.0.1', 3306)
)
server.start()
cnx = pymysql.connect(
host='127.0.0.1',
port=server.local_bind_port,
user='root',
password='my_database_password',
db='my_database'
)
# Make sure to call server.stop() when you want to disconnect
# after calling cnx.close()
Based on the information given, I would suggest you try the following:
Ensure the MySQL bind address is "0.0.0.0". You can do this by editing /etc/mysql/my.cnf and checking the bind-address option is set to "0.0.0.0"
Make sure the port is open from your remote machine. Install "nmap" then use nmap -sS -O -p3306 <public-server-ip>. If see something like 3306/mysql open all good. If not, you need to ensure your firewall isn't blocking that port.
As Rocket Hazmat suggests you probably will want to use an SSH tunnel to reduce the risks of exposing MySQL port directly as data is probably sent in the clear. If you just need to test your code locally before deploying to the server you can use the following command before you run your Python code: ssh -f user#serverip -L 3306:127.0.0.1:3306 -N. In this case you don't need to complete step one and two.
working code
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
import pymysql
import sshtunnel
from sshtunnel import SSHTunnelForwarder
server = SSHTunnelForwarder(
('xx.xx.xx.xx', 22),
ssh_username='deploy',
ssh_pkey='~/.ssh/id_rsa',
remote_bind_address=('127.0.0.1', 3306)
)
server.start()
con = pymysql.connect(host='127.0.0.1', user='root', passwd='mysql', db='backoffice_demo', port=server.local_bind_port)
with con:
cur = con.cursor()
cur.execute("SELECT VERSION()")
version = cur.fetchone()
print("Database version: {}".format(version[0]))

Can't connect to Cloud SQL using PyMySQL

I'm trying to connect to Cloud SQL from a Python application (using PyMySQL 0.7.9) running on top of Google App Engine.
My connection string looks like this (credentials are fake of course):
pymysql.connect(unix_socket='/cloudsql/gae_project_name:cloudsql_instance_name',
user='user', password='', db='database_name')
The error message I receive is:
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 97] Address family not supported by protocol)")
It's like PyMySQL doesn't recognize that I'm trying to connect through a Unix socket and tries the default value for the host argument instead (which I presume is localhost)
I am able to connect with MySQLdb with the same connection string.
Why don't use MySQLdb instead then ?
I just had the same problem deploying a Flask application with PyMySQL, I tried a lot of fixes without success. My workaround was to use MySQLDb instead aha..!
Apparently, PyMySQL is not currently supported on the Google App Engine Standard environment, which only runs Python 2.7 (as of June 2018). This is from the maintainers of the GCP python project:
I can confirm that pymysql is not supported in the python27 runtime. However, for most use cases, it's possible to use pymysql locally and mysqldb in production by using a try: / except ImportError: to import one or the other conditionally. As they share the same interface, you can use import as to make the two different libraries share the same name for ease of use in your code.
See this Github thread for details
I got it to connect using a Python3 Flex App Engine and PyMySQL.
Here's what my app.yaml looks like:
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT api:app --timeout 180
runtime_config:
python_version: 3
env_variables:
SQLALCHEMY_DATABASE_URI: >-
mysql+pymysql://user:password#/database?unix_socket=/cloudsql/project-name:us-central1:instance-name
beta_settings:
cloud_sql_instances: us-central1:instance-name
# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
Make sure to replace the user, password, database, and instance connection in both the env_variables and the beta_settings.
Here's my python:
import pymysql.cursors
import pymysql
connection = pymysql.connect(unix_socket='/cloudsql/project-name:us-central1:instance-name',
user='user',
password='password',
db='database',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)

Categories

Resources