Compiling From Source

By Silviu, on 2017-05-02

Basic steps to compile PostgreSQL from source.

Compiling on ARM Processors

On my ARM-based Odroid XU4, I installed Postgres using pacman on my ArchLinux partition, but I opted to compile it from source on my Ubuntu partition. Here are the steps I took to compile version 9.6:

#!/usr/bin/env bash

# The postgresql source folder. Change as needed
PG_SOURCE_FOLDER="/home/${USER}/tools/postgresql/src/postgresql-9.6.2"

# The target where the libraries and binaries will be installed
TARGET_PG_FOLDER="/usr/local/pgsql"

# Target data folder
PGDATA="/usr/local/pgsql/data"

echo ""
echo "Postgresql source folder located at: ${PG_SOURCE_FOLDER}"
echo "Target installation folder: ${TARGET_PG_FOLDER}"
echo "Target data folder (PGDATA): ${PGDATA}"
echo "Waiting 10 seconds so you can change your mind..."
echo ""
sleep 10s # Waits 10 seconds

# Install prerequisite libraries

# readline
sudo apt-get install libreadline6 libreadline6-dev

# zlib
sudo apt-get install zlib1g-dev  

# xml
sudo apt-get install libxml2-dev libxslt1-dev

# systemd support
sudo apt-get install libsystemd-dev

# Instruct configure to build the configuration file with the following options
# systemd support
# xml and xslt support
cd ${PG_SOURCE_FOLDER}
./configure --with-systemd --with-libxml --with-libxslt

# builds everything (including docs and additional contrib modules)
# use 8 cpu cores when compiling
make world -j 8
sudo make install world

echo "************************************************************************"
echo "After a successful build, postgres should reside at ${TARGET_PG_FOLDER}"
echo "Please add the binaries to path: PATH=/usr/local/pgsql/bin:\$PATH"
echo ""
echo "Before PostgreSQL can function correctly, the following steps are required:"
echo "1.Create a postgres user account: sudo useradd postgres"
echo "2.Create the data folder at ${PGDATA}"
echo "3.Change ownership of the data folder to postgres"
echo "4.Switch user to postgres: sudo su postgres"
echo "5.As postgres, initialize database cluster:  initdb -D ${PGDATA}"
echo ""
echo "The server can be started with: /usr/local/pgsql/bin/pg_ctl -D ${PGDATA} -l logfile start"
echo ""
echo "To set up systemd scripts, see here:"
echo "http://unix.stackexchange.com/questions/220362/systemd-postgresql-start-script"