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 '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 install libicu-devel readline-devel
# configure git
git config --global pager.branch false

Getting the source code

To download the source code we use git

su - user1
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 - user1
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 - user1
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

# run as root
(read -p 'Compile also additional modules and documentation (html and manpages) (y/n)? ' answ
su - user1 -c "cd postgresql ; ./configure"
su - user1 -c "cd postgresql ; if [ $answ == 'y' ] ; then make world ; else make ; fi"
cd ~user1/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. Here are some useful files:

# config file
/usr/local/pgsql/data/postgresql.conf
# postgres logfile
/home/postgres/logfile

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 - user1 -c 'cd postgresql && make clean'

Further info