Compiling PostgreSQL
Compiling PostgreSQL shows you how to build PostgreSQL from the official git repository on RedHat Enterprise Linux 8.10. This guide also works for RedHat Enterprise Linux 10.
Prerequisites
We start by setting up a basic RedHat Enterprise Linux 8.10 virtual machine (with Oracle VirtualBox) as described here. Next we create the postgres user and install required OS packages.
# run as root
# 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
# enable codeready-builder repo (required for perl-IPC-Run)
dnf config-manager --set-enabled $(dnf repolist --all | grep -E "^codeready-builder.*rpms " | grep -v -E "debug|eus|source" | head -1 | awk '{print $1}')
# install OS packages
dnf -y groupinstall 'Development Tools'
dnf -y install libicu-devel readline-devel perl-IPC-Run
# 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 -c "git clone https://git.postgresql.org/git/postgresql.git"
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 -c "cd postgresql; 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
# get the latest data
git fetch
# show all remote branches
git branch -r
# checkout another 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' --enable-tap-tests; else ./configure ; fi"
su - postgres -c "cd postgresql ; if [ $answ == 'y' ] ; then make -s -j \$(nproc) world ; else make -s -j \$(nproc) ; fi"
cd ~postgres/postgresql
if [ $answ == 'y' ] ; then make -s install-world ; else make -s install ; fi)
Initialize and start the database server
su - postgres -c "initdb -D /usr/local/pgsql/data
pg_ctl -D /usr/local/pgsql/data -l logfile start
createdb mydb
psql -c '\l' mydb"
Configure the database server
We will now perform some configuration steps. Run the following as root:
# Configure the database server to allow connections from other hosts
# in postgresql.conf whe change the listening endpoint from localhost to all interfaces (*)
sed -i s/^"#listen_addresses = 'localhost'"/"listen_addresses = '*'"/g /usr/local/pgsql/data/postgresql.conf
su - postgres -c "pg_ctl -D /usr/local/pgsql/data -l logfile restart"
# allow connections from other hosts
cat << EOF >> /usr/local/pgsql/data/pg_hba.conf
# Allow connections from all other IPv4 hosts
host all all 0.0.0.0/0 scram-sha-256
EOF
su - postgres -c "pg_ctl -D /usr/local/pgsql/data -l logfile reload"
# give the postgres database user a password (changeme)
su - postgres -c "psql -c \"alter user postgres password 'changeme'\";"
The database server is now started and a test database (mydb) 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 -s clean'
Important files and useful commands
| File | Description |
|---|---|
/usr/local/pgsql/data/postgresql.conf | postgres config file |
/usr/local/pgsql/data/pg_hba.conf | postgres pg_hba.conf file |
/home/postgres/logfile | postgres log file |
/home/postgres/postgresql | postgres source directory (git) |
/usr/local/pgsql/ | postgres install directory |
| Command | run as user | Description |
|---|---|---|
pg_ctl -D /usr/local/pgsql/data -l logfile start | postgres | start the PostgreSQL server |
pg_ctl -D /usr/local/pgsql/data stop | postgres | stop the PostgreSQL server |
pg_ctl -D /usr/local/pgsql/data status | postgres | get the status of the PostgreSQL server |
Applying a patch to the source code
If we want to apply a .patch to the current source code we can do that with:
# run as the postgres user
cd ~/postgresql
# dryrun
patch -p1 --dry-run < /tmp/repack.patch
# apply patch
patch -p1 < /tmp/repack.patch
# now rebuild
or with git am:
# run as the postgres user
cd ~/postgresql
# apply patch
git am /tmp/repack.patch
Creating a .patch file
If you want to contrib to the development of postgres you can create a .patch file as follows:
- Make your changes to the source files and create a commit:
git add doc/src/sgml/maintenance.sgmlgit commit -m "Doc: Fix small typo (vacuuming)"
- Create the .patch file:
git format-patch -v 1 origin/master
- Send a mail with the patch to the postgres mailing list
- (Optional) remove the local commit
git fetch origingit reset --hard origin/master

Leave a Reply