Skip to content

Compile PostgreSQL from source and create your own package

Preamble

For all our database installations we strongly favor the usage of bintar packages to choose the installation directory. This installation directory is a dedicated mount point located on one of our storage array (Pure Storage, EMC, …).

Avoiding the /var, /etc, /usr and so on default directories of the RPM release of the different databases is even more important in case of cluster installations where you want to move the mount point from one server to another one (Veritas Cluster, Pacemaker, …). Managing a binary installation per server would be a non sense as well as sometimes a problem from licensing standpoint…

For all the other database engines I have tested on Linux (Oracle, MariaDB, SQL Server, MongoDB? …) I have been able to download a bintar version while for PostgreSQL this release is simply not available.

So I was asking myself what if you want to run PostgreSQL 17.7 and PostgreSQL 17.6 to validate an upgrade or you have a constraint for one of your application where the supplier has not yet certified 17.7 ?

On official PostgreSQL web site they simply do not provides this bintar version and if you want to choose your custom installation directory you have to compile PostgreSQL from source…

My test server is running Red Hat 8.140 and has 12 cores and 64GB of memory.

building_postgresql01
building_postgresql01

PostgreSQL Compilation and Installation

Download the source zip on PostgreSQL official web site and unzip it to a temporary folder:

[postgres@server01 ~]$ gtar xf postgresql-17.6.tar.gz
[postgres@server01 ~]$ cd postgresql-17.6

Configure the PostgreSQL installation directory and make/make install:

[postgres@server01 postgresql-17.6]$ ./configure --prefix=/postgres/pgsql-17.6 --with-openssl
[postgres@server01 postgresql-17.6]$ make world-bin
[postgres@server01 postgresql-17.6]$ make install-world-bin

Remark:

  • It is not mandatory but strongly suggested to compile with OpenSSL for secure connection. This is required for TimescaleDB extension.
  • You might have to resolve few missing dependencies like I had to do. Example: flex, bison, libicu-devel, openSSL… This is really linked to your server…
  • You can also simply do “make/make install” and build the contrib extension(s) you really need.

Example on how to build and install the pg_stat_statements extension:

[postgres@server01 postgresql-17.6]$ cd contrib/pg_stat_statements/
[postgres@server01 pg_stat_statements]$ make
[postgres@server01 pg_stat_statements]$ make install
[postgres@server01 ~]$ ll /postgres/pgsql-17.6/lib/pg_stat_statements.so
-rwxr-xr-x 1 postgres dba 49544 Oct 24 14:02 /postgres/pgsql-17.6/lib/pg_stat_statements.so

Building pgvector and TimescaleDB extensions

In previous blog posts linked to PostgreSQL I have used pgvector and TimescaleDB so I need them in my custom installation !

In below I have discovered that the chosen directory by “make install” is coming from pg_config binary. So change your PATH to have this tool available in good target directory:

[postgres@server01 pgvector-master]$ pg_config
BINDIR = /postgres/pgsql-17.6/bin
DOCDIR = /postgres/pgsql-17.6/share/doc
HTMLDIR = /postgres/pgsql-17.6/share/doc
INCLUDEDIR = /postgres/pgsql-17.6/include
PKGINCLUDEDIR = /postgres/pgsql-17.6/include
INCLUDEDIR-SERVER = /postgres/pgsql-17.6/include/server
LIBDIR = /postgres/pgsql-17.6/lib
PKGLIBDIR = /postgres/pgsql-17.6/lib
LOCALEDIR = /postgres/pgsql-17.6/share/locale
MANDIR = /postgres/pgsql-17.6/share/man
SHAREDIR = /postgres/pgsql-17.6/share
SYSCONFDIR = /postgres/pgsql-17.6/etc
PGXS = /postgres/pgsql-17.6/lib/pgxs/src/makefiles/pgxs.mk
CONFIGURE =  '--prefix=/postgres/pgsql-17.6'
CC = gcc
CPPFLAGS = -D_GNU_SOURCE
CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2
CFLAGS_SL = -fPIC
LDFLAGS = -Wl,--as-needed -Wl,-rpath,'/postgres/pgsql-17.6/lib',--enable-new-dtags
LDFLAGS_EX =
LDFLAGS_SL =
LIBS = -lpgcommon -lpgport -lz -lreadline -lpthread -lrt -ldl -lm
VERSION = PostgreSQL 17.6

Either you git clone or you download a zip from pgvector web site and this is as simple as:

[postgres@server01 ~]$ unzip pgvector-master.zip
[postgres@server01 ~]$ cd pgvector-master
[postgres@server01 pgvector-master]$ make
[postgres@server01 pgvector-master]$ make install

Same for TimescaleDB, download source from TimescaleDB web site and simply do:

[postgres@server01 timescaledb-2.22.1]$ ./bootstrap
[postgres@server01 timescaledb-2.22.1]$ cd build
[postgres@server01 build]$ make
[postgres@server01 build]$ make install
[postgres@server01 build]$ ll /postgres/pgsql-17.6/lib/timescaledb*
-rwxr-xr-x 1 postgres dba 919160 Oct 24 14:48 /postgres/pgsql-17.6/lib/timescaledb-2.22.1.so
-rwxr-xr-x 1 postgres dba  20240 Oct 24 14:49 /postgres/pgsql-17.6/lib/timescaledb-invalidations-2.22.1.so
-rwxr-xr-x 1 postgres dba  57896 Oct 24 14:48 /postgres/pgsql-17.6/lib/timescaledb.so
-rwxr-xr-x 1 postgres dba 981120 Oct 24 14:49 /postgres/pgsql-17.6/lib/timescaledb-tsl-2.22.1.so

Remark:
If you did not compile PostgreSQL with openSSL support use ./bootstrap -DUSE_OPENSSL=0 to avoid below error but definitively it is better to compile PostgreSQL with OpenSSL support:

CMake Error at CMakeLists.txt:605 (message):
PostgreSQL was built without OpenSSL support, which TimescaleDB needs for
full compatibility.  Please rebuild PostgreSQL using `--with-openssl` or if
you want to continue without OpenSSL, re-run bootstrap with
`-DUSE_OPENSSL=0`

Deployment on other servers

To go further you will most probably not rebuild PostgreSQL from source on all your database servers. The obvious option is to build a package from this compilation that ideally occur on a server dedicated to this task. Your database production server does probably not requires all those development packages. It will at same time reduce the attack surface from treats by having less packages…

Two options here:

  • Build a bintar pacakge from the installation directory
  • Build a RPM from the installation directory

Bintar

Easiest method:

[postgres@server01 ~]$ cd /postgres/
[postgres@server01 postgres]$ ll
total 2
drwx------ 3 postgres dba   96 Nov 27  2024 17
drwxr-xr-x 4 postgres dba 1024 Jul 20  2021 backup
-rw------- 1 postgres dba  191 Nov 17 15:43 logfile
drwxr-xr-x 2 postgres dba   96 Feb 22  2022 lost+found
drwxr-x--- 6 postgres dba   96 Nov 17 15:05 pgsql-17.7
drwxr-x--- 6 postgres dba   96 Nov 17 18:02 pgsql-18.1
[postgres@server01 postgres]$ tar czf pgsql-17.7.tar.gz pgsql-17.7
[postgres@server01 postgres]$ ll
total 23286
drwx------ 3 postgres dba       96 Nov 27  2024 17
drwxr-xr-x 4 postgres dba     1024 Jul 20  2021 backup
-rw------- 1 postgres dba      191 Nov 17 15:43 logfile
drwxr-xr-x 2 postgres dba       96 Feb 22  2022 lost+found
drwxr-x--- 6 postgres dba       96 Nov 17 15:05 pgsql-17.7
-rw-r----- 1 postgres dba 14717598 Nov 18 11:19 pgsql-17.7.tar.gz
drwxr-x--- 6 postgres dba       96 Nov 17 18:02 pgsql-18.1

This pgsql-17.7.tar.gz can be deployed anywhere and you install PostgreSQL with:

[postgres@server01 ~]$ tar xzvf pgsql-17.7.tar.gz -C /postgres

RPM

dnf install rpm-build rpmdevtools
[postgres@server01 ~]$ rpmdev-setuptree
[postgres@server01 ~]$ cd rpmbuild/
[postgres@server01 rpmbuild]$ ll
total 0
drwxr-x--- 2 postgres dba 6 Nov 18 11:24 BUILD
drwxr-x--- 2 postgres dba 6 Nov 18 11:24 RPMS
drwxr-x--- 2 postgres dba 6 Nov 18 11:24 SOURCES
drwxr-x--- 2 postgres dba 6 Nov 18 11:24 SPECS
drwxr-x--- 2 postgres dba 6 Nov 18 11:24 SRPMS
[postgres@server01 rpmbuild]$ cp /postgres/pgsql-17.7.tar.gz SOURCES

Create a spec file with:

[postgres@server01 rpmbuild]$ rpmdev-newspec ~/rpmbuild/SPECS/postgresql-17.7.spec
/home/postgres/rpmbuild/SPECS/postgresql-17.7.spec created; type minimal, rpm version >= 4.14.

I have this postgresql-17.7.spec file:

Name:           postgresql
Version:        17.7
Release:        1%{?dist}
Summary:        PostgreSQL 17.7 built from source
BuildArch:      x86_64

License:        PostgreSQL License, a liberal Open Source license, similar to the BSD or MIT licenses.
URL:            https://www.postgresql.org/
Source0:        pgsql-17.7.tar.gz

Requires:       openssl, libgcc, libstdc++

%description
PostgreSQL 17.7 built from source that will install in /postgres mount point

%prep
# Nothing to do

%build
# Nothing to do

%install
rm -rf %{buildroot}
mkdir -p %{buildroot}/postgres/pgsql-%{version}
tar -xzf %{SOURCE0} -C %{buildroot}/postgres/pgsql-17.7 --strip-components=1

%clean
rm -rf %{buildroot}/postgres/pgsql-17.7

%files
%defattr(755, postgres, dba, 750)
/postgres/pgsql-17.7

%changelog
* Tue Nov 18 2025 PostgreSQL 17.7
- Initial custom build from source

Build the RPM with (add -vv for verbose output in case of issue):

[postgres@server01 rpmbuild]$ rpmbuild -bb ~/rpmbuild/SPECS/postgresql-17.7.spec
.
.
[postgres@server01 rpmbuild]$ ll RPMS/x86_64/
total 8916
-rw-r----- 1 postgres dba 9129024 Nov 18 14:43 postgresql-17.7-1.el8.x86_64.rpm
[postgres@server01 rpmbuild]$ rpm -qi RPMS/x86_64/postgresql-17.7-1.el8.x86_64.rpm
Name        : postgresql
Version     : 17.7
Release     : 1.el8
Architecture: x86_64
Install Date: (not installed)
Group       : Unspecified
Size        : 49866188
License     : PostgreSQL License, a liberal Open Source license, similar to the BSD or MIT licenses.
Signature   : (none)
Source RPM  : postgresql-17.7-1.el8.src.rpm
Build Date  : Tue 18 Nov 2025 05:38:37 PM CET
Build Host  : server01.domain.com
Relocations : (not relocatable)
URL         : https://www.postgresql.org/
Summary     : PostgreSQL 17.7 built from source
Description :
PostgreSQL 17.7 built from source that will install in /postgres mount point
[postgres@server01 rpmbuild]$ rpm -q RPMS/x86_64/postgresql-17.7-1.el8.x86_64.rpm --changelog
* Tue Nov 18 2025 PostgreSQL 17.7
- Initial custom build from source

This RPM must be installed by root with dnf and once installed you see it with rpm and permission of files and folders in /postgres are good (postgres:dba) !

References

About Post Author

Share the knowledge!

Leave a Reply

Your email address will not be published. Required fields are marked *