How to build and debug PostgreSQL using MinGW

1. Install PostgreSQL using MSYS2 and MinGW

(1) Download MSYS2 installer from the following link and install along with the installer
http://www.msys2.org/
(2) Add path to D:\msys2-64\mingw64\bin.

(3) Start MSYS2 by running the following command in command prompt

D:\msys2-64\msys2_shell.cmd -mingw64

(4) Install build tools and debug tool by running the following commands in MSYS2

pacman -Syu
pacman –needed -S mingw-w64-x86_64-gcc base-devel
pacman –needed -S mingw-w64-x86_64-gdb

(5) Create makefile by running the following comannds in MSYS2

wget https://ftp.postgresql.org/pub/source/v13.0/postgresql-13.0.tar.gz
tar -x -zf ./postgresql-13.0.tar.gz
cd ./postgresql-13.0
./configure –host=x86_64-w64-mingw32 –prefix=/d/pgsql-13/ –enable-debug

(5) Remove a flag which means code optimization from “src/Makefile.global”

before:
CFLAGS = -Wall (…) -fexcess-precision=standard -g -O2
CXXFLAGS = -Wall (…) -g -O2
after:
CFLAGS = -Wall (…) -fexcess-precision=standard -g
CXXFLAGS = -Wall (…) -g

(6) Execute build and install by running the following comannds in MSYS2

make
make install

2. Setup PostgreSQL

(1) Add path to d:\pgsql-13\bin

(2) Init database cluster by running the following command in comannd prompt

initdb.exe -D D:\pgsql-13\data

(3) Register PostgreSQL service by running the following command in comannd prompt as the Administrator

pg_ctl.exe register -N “postgresql-x64-13” -U “NT AUTHORITY\NetworkService” -D “D:/pgsql-13/data” -w -S demand

(4) Start servie “postgresql-x64-13”

(5) Create postgres user by running the following command in command prompt. In the following commands, <login_user> is the name of login user and <password> is a password of postges.

psql -U <login_user> -d postgres -c “create user postgres with superuser ‘<password>’ ”

(6) Create sample table and insert sample data into the table in psql terminal.

create table t1(id integer, name text);
insert into t1 values(1, ‘hoge1’);
insert into t1 values(2, ‘hoge2’);

3. Debugging PostgreSQL

(1) Show the pid of PostgreSQL server in psql terminal

select pg_backend_pid();

(2) Run gdb in command prompt as the Administrator and run the following commands. In the following command, <pid> is the output of (1).

attach <pid>
b planner

(3) Start sample query, for example, “select * from t1 where id = 1”. If you can see PostgreSQL server stopping at planner() in planner.c, debugging success..

4. References

[1] BUILDING POSTGRESQL WITH MSYS2 AND MINGW UNDER WINDOWS
https://www.cybertec-postgresql.com/en/building-postgresql-with-msys2-and-mingw-under-windows/

Published by ktke109

I love open souce database management systems.