Compiling PostgreSQL

Compiling PostgreSQL shows you how to build PostgreSQL from the official git repository on RedHat Enterprise Linux 8.10.

Prerequisites

We start by setting up a basic RedHat Enterprise Linux 8.10 virtual machine (Oracle VirtualBox) as described here. Next we create the postgres user and install required OS packages.

# create postgres user
useradd -G vboxsf postgres
echo 'postgres:changeme'|chpasswd
echo 'MANPATH=/usr/local/pgsql/share/man:$MANPATH' >> ~/.bash_profile
echo 'export MANPATH' >> ~/.bash_profile
echo 'MANPATH=/usr/local/pgsql/share/man:$MANPATH' >> ~postgres/.bash_profile
echo 'export MANPATH' >> ~postgres/.bash_profile
echo 'PATH=/usr/local/pgsql/bin:$PATH' >> ~postgres/.bash_profile
echo 'export PATH' >> ~postgres/.bash_profile
mkdir -p /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data
# install OS packages
dnf -y groupinstall 'Development Tools'
dnf -y install libicu-devel readline-devel
# configure git (disable pager)
git config --global pager.branch false

Getting the source code

To download the source code from postgresql.org we use git

su - postgres
git clone https://git.postgresql.org/git/postgresql.git
exit

If there a new commits in the repository after some time we can update our local repository by running the following commands. But for now we are good since we just cloned the repository.

su - postgres
cd progresql
git fetch

The default branch (master) of this repository is the development branch of the upcoming release. If we want we can switch to other branches with these commands:

su - postgres
cd postgresql
# show all remote branches
git branch -r
# checkout branch
git checkout REL_17_STABLE
# show current branch (marked with a *)
git branch

Compiling PostgreSQL

The script below will ask what type of compilation you want to perform. Choose according your needs.

# run as root
(read -p 'Compile also additional modules and documentation (html and manpages) (y/n)? ' answ
read -p 'Create a development build? (y/n)? ' devbuild
su - postgres -c "cd postgresql ; if [ $devbuild == 'y' ] ; then ./configure --enable-debug --enable-cassert CFLAGS='-O0 -g3' ; else ./configure ; fi"
su - postgres -c "cd postgresql ; if [ $answ == 'y' ] ; then make -j \$(nproc) world ; else make -j \$(nproc) ; fi"
cd ~postgres/postgresql
if [ $answ == 'y' ] ; then  make install-world ; else make install ; fi)

Starting the database server

su - postgres
initdb -D /usr/local/pgsql/data
pg_ctl -D /usr/local/pgsql/data -l logfile start
createdb test
psql -c '\l' test
exit

The database server is now started and a test database has been created.

Cleanup for a new build

su - postgres -c 'pg_ctl stop'
rm -rf /usr/local/pgsql/data/* /home/postgres/logfile /usr/local/pgsql/*
mkdir -p /usr/local/pgsql/data && chown postgres /usr/local/pgsql/data
su - postgres -c 'cd postgresql && make clean'

Important files and useful commands

FileDescription
/usr/local/pgsql/data/postgresql.confconfig file
/home/postgres/logfilepostgres log file
/home/postgres/postgresqlpostgres source directory (git)
/usr/local/pgsql/postgres install directory
Commandrun as userDescription
pg_ctl -D /usr/local/pgsql/data -l logfile startpostgresstart the PostgreSQL server
pg_ctl -D /usr/local/pgsql/data stoppostgresstop the PostgreSQL server
pg_ctl -D /usr/local/pgsql/data statuspostgresget the status of the PostgreSQL server

Further info

0