Preface

There are several ways of building PostgreSQL under Windows. Official manual stands using Visual Studio is the simplest one, wiki describes how to use mingw and mingw-w64.

As for me, using new MSYS2 building platform for Windows is a bless. With its help not only PostgreSQL sources are built smoothly, but even extensions are not a problem anymore. And if you were playing with extensions under Windows, you know what I’m talking about.

At its core MSYS2 is an independent rewrite of MSYS, based on modern Cygwin (POSIX compatibility layer) and MinGW-w64 with the aim of better interoperability with native Windows software. It provides a bash shell, Autotools, revision control systems and the like for building native Windows applications using MinGW-w64 toolchains.

Installation

It is pretty straightforward. On the official page you have the step-by-step guide, but in case of troubles, you may check detailed install guide.

After installation and upgrading run in cmd console

C:\msys64\msys2_shell.cmd -mingw64

or simply just click on the Start menu “MSYS2 MinGW 64-bit” shortcut:
MSYS2 MinGW 64-bit shortcut

Then update packages:

pacman -Syu

Install only needed packages:

pacman --needed -S git mingw-w64-x86_64-gcc base-devel

Let’s build it!

Get the PostgreSQL sources. Here you have two options:
1. Download them from the official site.
2. Use git repository:

git clone git://git.postgresql.org/git/postgresql.git

Enter to the source folder:

cd postgresql

And run configure followed by make and make install:

./configure --host=x86_64-w64-mingw32 --prefix=/c/pgsql/ && make && make install

Here we tell our target machine will be 64-bit and we want our binaries to be copied to C:\pgsql\.

Conclusion

As you may see building PostgreSQL with MSYS2 for Windows is simple enough. But the main profit of such toolchain is building extensions for PostgreSQL. And there it shines in all its glory. Stay tuned.

6 thoughts on “Building PostgreSQL with MSYS2 and MinGW under Windows

  1. Great post!
    I tried building the latest postgresql 11.1, but the make failed.
    In order to be sure that I did not missed something in my environment setup, I built postgresql 10.4 and it worked fine.
    Is this issue related to MSYS2 or postgresql?
    Have any idea how to fix it?
    Thanks

    The make failed as follow:

    x86_64-w64-mingw32-gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 -I../../src/include -I./src/include/port/win32 -DEXEC_BACKEND “-I../../src/include/port/win32″ -DBUILDING_DLL -c -o zic.o zic.c
    sed -e ‘s;FILEDESC;”zic – time zone compiler”;’ -e ‘s;VFT_APP;VFT_APP;’ -e ‘s;_ICO_;IDI_ICON ICON “../../src/port/win32.ico”;’ -e ‘s;(VERSION.*),0 *$;1,’`date ‘+%y%j’ | sed ‘s/^0*//’`’;’ ../../src/port/win32ver.rc >win32ver.rc
    windres -i win32ver.rc -o win32ver.o –include-dir=../../src/include –include-dir=.
    x86_64-w64-mingw32-gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 zic.o win32ver.o -L../../src/port -L../../src/common -Wl,–allow-multiple-definition -Wl,–disable-auto-import -Wl,–as-needed -lpgcommon -lpgport -lz -lws2_32 -lm -lws2_32 -o zic.exe
    C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.2.1/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible ../../src/common/libpgcommon.a when searching for -lpgcommon
    C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.2.1/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible ../../src/commonlibpgcommon.a when searching for -lpgcommon
    C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.2.1/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible ../../src/common/libpgcommon.a when searching for -lpgcommon
    C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.2.1/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lpgcommon
    collect2.exe: error: ld returned 1 exit status
    make[2]: *** [Makefile:51: zic] Error 1
    make[2]: Leaving directory ‘/c/source/postgresql-11.1/src/timezone’
    make[1]: *** [Makefile:42: all-timezone-recurse] Error 2
    make[1]: Leaving directory ‘/c/source/postgresql-11.1/src’
    make: *** [GNUmakefile:11: all-src-recurse] Error 2

    1. Hello. We’re really sorry you get into this issue. I checked one more time. All is clear on my side. Perhaps you should `make clean` first and start from the beginning. Sounds like you already have some prebuilt binaries, maybe wrong chain, e..g mingw32, VC++ etc. Let me know if you need further assistance.

      1. I made a make clean and it worked. I cannot explain what wrong I did. Sorry, issue was between the keyboard and chair. Thanks again for your post, I loved it. Well explain, the best I have found.

        1. Ah, never mind. Shit happens. Glad you made it. If you have any ideas what other posts you are interesting in, please, do not hesitate to share your thoughts.

          Regards

  2. Can you update your post with the following, maybe useful for others.
    I had issue with openssl.
    1. Currently, mysy2 install openssl-1.0.2.p-2-x86_64 while PostgreSQL 11 is compatible with Openssl 1.1.0 or later.
    https://github.com/Alexpux/MINGW-packages/issues/4642
    The Openssl should be updated as follow. If not the configure fails on ssl.
    pacman -S mingw64/mingw-w64-x86_64-openssl
    This will install openssl-1.1.1a

    run the configure & make & make install
    ./configure –host=x86_64-w64-mingw32 –prefix=/c/postgresql/pg11/ –with-openssl

    2. Then you need openssl on the system that run the postgres server
    Only libcrypto-1_1-x64.dll and libssl-1_1-x64.dll are required (copy them into C:WindowsSystem32)
    You have two choices:
    – Copy the Win64OpenSSL-1_1_0j.exe dll from https://slproweb.com/products/Win32OpenSSL.html
    – Copy the Win64OpenSSL-1_1_1a.exe dll from https://slproweb.com/products/Win32OpenSSL.html
    If you are using git, download the Win64OpenSSL-1_1_0j.exe (1_1_1a. is not compatible with git)

    3. Openssl 1.1.1.a requires MSVCP runtime dist 2017
    install vc_redist.x64.exe
    MSVCP runtime dist 2017 is also necessary for oracle_fdw of PG11

Comments are closed.