## Building Psycopg on Windows using MinGW

Posted by on June 5, 2011
Tagged as build, windows

My goal was to install Psycopg on Windows using MinGW and the PostgreSQL binary package.

I have used the MinGW GCC binaries packaged by Giovanni Bajo. The package takes care of a lot of details, for instance registering MinGW as default compiler for Python, plus some magic I don't even want to know, and makes the entire process simple enough.

The first step is to ensure that setup.py can find pg_config. There is a bug causing it not to be found if it is in the path: it will be fixed in Psycopg 2.4.2. On a few older versions you will have to specify the pg_config path in the setup.cfg or by the --pg-config option, e.g.

python setup.py build_ext --pg-config=C:\path\to\pg_config.exe build


The library built depends on libpq.dll, so at runtime this library should be available, e.g. on the path or it may be copied in the psycopg2 directory. The libpq in turn depends on a few other dlls, all found in the PostgreSQL bin directory: libeay32.dll, ssleay32.dll and libintl-8.dll: they should be made available to the client as well. Unfortunately if any of these libraries is missing you will only get an "ImportError: dll load failed". The problem is very easy to debug using a dependency walker.

Another problem you may find is building for Python 2.6 and newer: some MinGW versions ship with a broken msvcr90 lib, and the result is again a rather unhelpful ImportError. the dependency walker is useful in this case too, showing the missing localtime function in the library. The bug was reported in the issue 3308: my solution was to download a newer MinGW and use the libmsvcr90.a from there.

So, overall, it can be done, but the result still depends on many dlls. I'm torn if the solution could be to have all the dependencies copied in the package directory. Of course you can still use Jason's binary package: he builds both the libpq and the openssl as static libs and creates a self-contained psycopg, which is probably the handiest result, but can't be obtained only using the PostgreSQL binaries.

Comment #1

June 22, 2011, 1:10 p.m.

Hej, thank you for the instructions. Following them building psycopg2-2.4.2 with MinGW worked like a charm - at least for Win32-version of Postgresql. Tried the 64bit-version of libqp.dll but was rejected with a "file not recognized/format not recognized" error. Do you have any clue whether this is related to a non-64bit-capable MinGW installation of mine? Or something else? Thanks in advance. Regards, Markus

Comment #2

June 22, 2011, 4:13 p.m.

Hi, Markus,

sorry but I don't have a Windows 64 bit handy. However googling around looks like there are a lot of informations about the issue. This is specific for the libpq but other hits may be handy as well.

Comment #3

July 7, 2012, 9:30 a.m.

when i install the psycopg, at the end of this,an error give that 'unable to find vcvarsall.bat',please give a solution ,thanks with all love!

Comment #4

July 7, 2012, 10:28 a.m.

Cannot check now, but there's plenty of info about this issue to be found on Google. Sorry for not being more specific now.

Comment #5

Nov. 28, 2012, 8:09 p.m.

I had the same problem. It was solved by uninstalling the 64bit version and installing the 32bit version of Python. Problem solved.

Comment #6

Sept. 1, 2013, 2:40 p.m.

Can you please let me know how to make this dll's libeay32.dll, ssleay32.dll and libintl-8.dll available to client?

I am getting following error when trying to connect-

File "C:\Python33\psycopg2\__init__.py", line 50, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: DLL load failed: The specified module could not be found.


Feel free to add a new comment. You can use reST syntax.