Psycopg2 Python SSL Support is not compiled in - python

I am trying to connect to my postgres database using psycopg2 with sslmode='required' param; however, I get the following error
psycopg2.OperationalError: sslmode value "require" invalid when SSL support is not compiled in
Heres a couple details about my system
Mac OS X El Capitan
Python 2.7
Installed psycopg2 via pip
Installed python via homebrew
Here is what I tried to do to fix the problem
brew uninstall python
which python still shows python living in /usr/local/bin/python, tried to uninstall this but couldnt. And heard that this is the python that the OS uses and should not be uninstalled anyways
brew install python --with-brewed-openssl --build-from-source
pip uninstall psycopg2
pip install psycopg2
After doing all of this, the exception still happens. I am running this python script via #!/usr/bin/env python Not sure if it matters, but that is a different directory than the one that which python shows

Since you're installing via pip, you should be using the most recent version of psycopg2 (2.6.1).
After a little digging through the code, it seems that the exception is being thrown in connection_int.c, which directly calls the postgresql-c-libraries to set up the db-connection. The call happens like so:
self->pgconn = pgconn = PQconnectStart(self->dsn);
Dprintf("conn_connect: new postgresql connection at %p", pgconn);
if (pgconn == NULL)
{
Dprintf("conn_connect: PQconnectStart(%s) FAILED", self->dsn);
PyErr_SetString(OperationalError, "PQconnectStart() failed");
return -1;
}
else if (PQstatus(pgconn) == CONNECTION_BAD)
{
Dprintf("conn_connect: PQconnectdb(%s) returned BAD", self->dsn);
PyErr_SetString(OperationalError, PQerrorMessage(pgconn));
return -1;
}
The keywords which were specified in your connect statement to psycopg2.connect() are being handled to that function and errors are returned as OperationalError exception.
The error is actually being generated directly in the postgresql-lib - you may want to check which version you are using, how it was built and, if possible, upgrade it to a version with SSL support or rebuilt it from source with SSL enabled.
The postgresql-docs also state that missing SSL support will raise an error, if the sslmode is set to require, verify-ca or verify-full. See here under sslmode for reference.
The postgres-website lists several ways to install postgres from binary packages, so you might choose one which suits your needs. I'm not familiar with OSX, so I don't have a recommendation what's best.
This question may also be helpful.
You also need to reinstall the psycopg2-module, be sure to use the newly installed lib when rebuilding it. Refer to the linked question (in short, you will need to place the path to pg_config which is included in your new installation to $PATH when running pip install psycopg2).

I had this same error, which turned out to be because I was using the Anaconda version of psycopg2. To fix it, I had adapt VictorF's solution from here and run:
conda uninstall psycopg2
sudo ln -s /Users/YOURUSERNAME/anaconda/lib/libssl.1.0.0.dylib /usr/local/lib
sudo ln -s /Users/YOURUSERNAME/anaconda/lib/libcrypto.1.0.0.dylib /usr/local/lib
pip install psycopg2
Then when you run conda list you'll see psycopg2 installed with <pip> in the far right column. After that, I just restarted Python and everything worked.

The error you are receiving is caused by a problem with Postgres itself, and not psycopg2.
psycopg2.OperationalError: sslmode value "require" invalid when SSL support is not compiled in
The above indicates that the version of Postgres that psycopg2 is built against does not have SSL support compiled in. When you attempt to connect to the running Posgres server with ssl=require it throws this error.
You don't mention how you installed Postgres but since you are using Homebrew for other things, I recommend you also install Postgres the same way:
$ brew update
$ brew install postgresql
The formula for postgresql shows that it depends on openssl and compiles with the --with-openssl flag set. It will also install the neccessary libpq headers. You may need to reinstall psycopg2 after this step to ensure it picks up the correct libraries/version.
Interestingly, there is a bug listed against conda which lists the same error that you report occurring when the conda version of psycopg2 — linked on a system with Homebrew postgres — was installed on a system without, suggesting missing SSL libraries can also trigger this.
I would suggest uninstalling any existing Postgres versions (including any installed via Homebrew) before reinstalling to minimise the risk of the wrong one being used.

As others have said, the error message looks to be coming from Postgres. You can verify this by typing: psql sslmode=require if it gives you a pgsql terminal then ssl works with postgres, if it errors then it doesn't
Try and remove postgres and reinstall with openssl support:
brew uninstall postgres
brew update
brew install postgres --with-openssl
Alternatively, and this is the way I'd suggest, there is a one click installer at http://postgresapp.com that might also make it easier to get it installed. Follow the instructions on the site to get it installed correctly.
When I did it on Yosemite it installed at ~/Library/Application\ Support/Postgres93/var
You'll also want to create a certificate (this could also be where the error is coming from) either from a registrar if this is going to be public facing in the slightest or self signed if it's for a dev/test environment.
openssl req -new -text -out server.req
openssl rsa -in privkey.pem -out server.key
rm privkey.pem
openssl req -x509 -in server.req -text -key server.key -out server.crt
chmod og-rwx server.key
Navigate to your config directory, by default it is: ~/Library/Application\ Support/Postgres93/var
Enable ssl support:
vim postgresql.conf
# change this:
# ssl = on
# to this:
ssl = on
Restart the app and then check ssl with psql "sslmode=require"
If that works then try it through your Python code. If it works with the code above, but not Python then it's definitely a Python code problem that needs to be worked through.

As I can not comment:
Adding to Brideau's answer that this only worked for me after changing my version of Postgres.
brew uninstall postgres
brew update
brew install postgres --with-openssl
Then run the code provided by Brideau and it should work.

If you're using v2.6.1 or v2.6.2 of psycopg2 (like me), the answer was a simple upgrade to v2.7. Reading the release notes for psycopg2, there was a minor fix for SSL albeit it doesn't look particularly relevant.
My setup was as follows:
Mac OS X El Capitan 10.11.6
psycopg2 2.6.2 installed via pip
PostgreSQL installed via Enterprise DB Installer
Running pip uninstall psycopg2 followed by pip install psycopg2 resolved matters.

Try to install psycopg2 from MacPorts
sudo port install py27-psycopg2

Related

Can't connect to HTTPS URL because the SSL module is not available on Mac/OS

Trying to build Python 2.7.17 and then install pip on Mac OS 10.14
Installed OpenSSL using brew
Python's build fails to build _ssl. When I try to build pip, it fails with error "Can't connect to HTTPS URL because the SSL module is not available
OpenSSL is correctly installed at /usr/local/Cellar/openssl#1.1/1.1.1d/
Python looks for ssl at /usr/local/ssl
To get it to build I did the following:
1. Set up a path to OpenSSL that didn't involve any weird characters:
cd /usr/local/Cellar/
ln -s openssl\#1.1 openssl
2: Ran ./configure
3: Edited Makefile replacing all "/usr/local/ssl" with "/usr/local/Cellar/openssl"
4: Run make / make test / make install
This created a Python that had _ssl, and thus pip would run

pymongo - "dnspython" module must be installed to use mongodb+srv:// URIs

I am trying to connect MongoDB from Atlas.
My mongo uri is: mongodb+srv://abc:123#something.something.com/admin?retryWrites=True
My pymongo version is 3.6.1
I have installed dnspython and done import dns
But i still get this error:
dnspython module must be installed to use mongodb+srv:// URI
In order to use mongo+srv protocol, you need to install pymongo-srv
Launch this command to do it with python 3:
pip3 install pymongo[srv]
or this one for other versions:
pip install pymongo[srv]
And as suggested by #lukrebs, add quotes for ZSH:
pip3 install 'pymongo[srv]'
I would like to answer my own questions here. As I mentioned in the comment, the kernel of the jupyter notebook has to be restarted in order for the pymongo to take effect of the loaded dnspython.
I solved this problem with:
$ python -m pip install pymongo[srv]
In requirements.txt, replace pymongo with pymongo[tls,srv], as mentioned here.
I got stuck with the same problem and tried
pip install dnspython==2.0.0
This is the latest version from https://pypi.org/project/dnspython/
It worked :D
you can use mongo:// instead of mongodb+srv://
May be the protocol, your URI should start with:
mongo+srv instead of mongo+src
If it still not working please put a pip list with the versions of PyMongo and dnspython (and version of python that you are using)
I had the same problem on Ubuntu 18 but since I am using Anaconda
I just tried
Conda install dns python
I had an IPython running, it did not work while the same instance was open but when I restarted that instance it worked.
On a different machine using
Conda install dns python
and it worked but I had to restart my machine altogether due to a different reason before testing it
I had the same issue and found the following line.
import dns.resolver
dns.resolver.default_resolver=dns.resolver.Resolver(configure=False)
dns.resolver.default_resolver.nameservers=['8.8.8.8']
It worked for me.
pip install dnspython
dnspython is a DNS toolkit for Python. It supports almost all record types. It can be used for queries, zone transfers, and dynamic updates. It supports TSIG authenticated messages and EDNS0.
None of the existing answers had worked for me. I had to do the following:
sudo apt-get install python3-dnspython

Error by create secure_channel in grpc of python

grpc was installed using pip. I tried to use it and I got an error. I looked for errors, but I could not find a solution.
The environment through uname is as follows.
env
uname -s -> Linux
uname -r -> 3.10.65
uname -m -> aarch64
code
import grpc
creds = grpc.ssl_channel_credentials(open('roots.pem').read())
channel = grpc.secure_channel('myservice.example.com:443', creds)
error log
24061 ssl_transport_security.c:655] Could not set ephemeral ECDH key.
24061 security_connector.c:774] Handshaker factory creation failed with TSI_INTERNAL_ERROR.
(I love that xkcd comic.. I feel that way all the time)
I solved this on arm64, here's how:
Under-the-hood grpcio uses boringssl if an environment variable is not applied to use the system's ssl. I believe the issue seen is with boringssl actually. Assuming you have openssl, libssl1.0.0 and libssl-dev installed, and you have already installed grpcio via pip or pip3.. then, you would do the following:
First download the grpcio source from PyPi.
pip3 uninstall grpcio
tar -xvf grpcio-<version>.tar.gz
cd grpcio-<version>/
GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=True python3 setup.py install
I'm sure setting GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=True before your pip3 install would work also, but I haven't tested that. The above solution fixed my issue.

Error when trying to uninstall Macports

I'm using this link to uninstall macports and instead install homebrew. However, I run into an error on the first step. I run sudo port -f uninstall installed my terminal (I'm on a mac btw) and it spits this right back at me :
Warning: port definitions are more than two weeks old, consider using selfupdate
Warning: configured user/group macports does not exist, will build as root
---> Uninstalling python27 #2.7.1_3
Error: Target org.macports.uninstall returned: error deleting "/opt/local/var/macports/software/python27/2.7.1_3": directory not empty
Log for python27 is at: /opt/local/var/macports/logs/_opt_local_var_macports_registry_portfiles_python27_2.7.1_3/python27/main.log
Warning: Failed to execute portfile from registry for python27 #2.7.1_3
---> Uninstalling python27 #2.7.1_3
Error: port uninstall failed: error deleting "/opt/local/var/macports/software/python27/2.7.1_3": directory not empty
I'm not really sure what this stuff means and am not very familiar with macports. I don't even think I installed python with macports...
If you are going to remove macports I would then just do the deletetion
sudo rm -rf \
/opt/local \
/Applications/DarwinPorts \
/Applications/MacPorts \
/Library/LaunchDaemons/org.macports.* \
/Library/Receipts/DarwinPorts*.pkg \
/Library/Receipts/MacPorts*.pkg \
/Library/StartupItems/DarwinPortsStartup \
/Library/Tcl/darwinports1.0 \
/Library/Tcl/macports1.0 \
~/.macports
The first warnings you are getting are due to not having the latest version of macports so run the selfupdate. For the error you need to look at the log suggested /opt/local/var/macports/logs/_opt_local_var_macports_registry_portfiles_python27_2.7.1_3/python27/main.log
The reason macports installed python without you asking is probably because you asked for a port that needed to use python. Macports will install its own python so that it knows exactly which version and compile options were used for python, as the developers from long experience with unix vendpors have found that the vendors (e.g. Apple) can change things and cause issues (see latest updates to Xcode for a good example) or not provide later bug fixes which are needed. Homebrew trusts Apple and so would use Apple's versions

Homebrew failing to install postgresql; python 64-bit errors

I'm getting errors when running
$ brew install postgresql
==> Downloading http://ftp.postgresql.org/pub/source/v9.1.2/postgresql-9.1.2.tar.bz2
File already downloaded in /Users/neil/Library/Caches/Homebrew
Warning: Detected a framework Python that does not have 64-bit support in:
/Library/Frameworks/Python.framework/Versions/Current/Python
e configure script seems to prefer this version of Python over any others,
you may experience linker problems as described in:
http://osdir.com/ml/pgsql-general/2009-09/msg00160.html
fix this issue, you may need to either delete the version of Python
own above, or move it out of the way before brewing PostgreSQL.
te that a framework Python in /Library/Frameworks/Python.framework is
e "MacPython" version, and not the system-provided version which is in:
/System/Library/Frameworks/Python.framework
==> ./configure --disable-debug --prefix=/usr/local/Cellar/postgresql/9.1.2 --datadir=/usr/local/Cellar/postgresql/9.1.2/shar
^C
Here's where python is located.
$ which python
/usr/local/bin/python
I modified my ~/.zshrc PATH from
export PATH=/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin
to
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin
And although I'm getting python 64-bit errors, my version of python is 64-bit according to this SO post:
$ python -c 'import struct;print( 8 * struct.calcsize("P"))'
64
The problem pointed out in the referenced mailing list post is that the configure step isn't impacted by the PATH here. There's a whole other mechanism used to find things to link against; see Where do I set DYLD_LIBRARY_PATH on Mac OS X for a quick intro. You could try the suggested workaround given by the brew script--rename /Library/Frameworks/Python.framework/Versions/Current/Python to something else to get it out of the linker's search path, repeat the brew install, then put it back.
If you don't need Python bindings in your PostgreSQL, you can also just install it without Python bindings using brew install postgresql --no-python.
This command is installing the server, not the python bindings. Is that what you want? There is a installer for osx that will install the server for you.
Once you have done that, you can install the psycopg2 bindings directly from source.

Categories