Install PostgreSQL from source code

In this article we are going to install PostgreSQL from source code. I will show the installation process on Ubuntu as the most popular distribution. To understand the lesson, you must be able to use the command line.

Bonus: video for this lesson

Step One - Install dependencies

sudo apt-get install build-essential libreadline-dev zlib1g-dev flex bison libxml2-dev libxslt-dev libssl-dev

Also, for download postgres source code, you need to install git :

sudo apt-get install git

Step Two - Install postgresql

First, you need to download source code from repository. You can do this from git.postgresql.org or from it’s github mirror github.com/postgres/postgres :

git clone git://git.postgresql.org/git/postgresql.git cd postgresql/

You can choose which version to install by swinthing to the right branch.

postgresql $ git branch -a * master remotes/origin/HEAD -> origin/master remotes/origin/REL2_0B remotes/origin/REL6_4 remotes/origin/REL6_5_PATCHES remotes/origin/REL7_0_PATCHES remotes/origin/REL7_1_STABLE remotes/origin/REL7_2_STABLE remotes/origin/REL7_3_STABLE remotes/origin/REL7_4_STABLE remotes/origin/REL8_0_STABLE remotes/origin/REL8_1_STABLE remotes/origin/REL8_2_STABLE remotes/origin/REL8_3_STABLE remotes/origin/REL8_4_STABLE remotes/origin/REL8_5_ALPHA1_BRANCH remotes/origin/REL8_5_ALPHA2_BRANCH remotes/origin/REL8_5_ALPHA3_BRANCH remotes/origin/REL9_0_ALPHA4_BRANCH remotes/origin/REL9_0_ALPHA5_BRANCH remotes/origin/REL9_0_STABLE remotes/origin/REL9_1_STABLE remotes/origin/REL9_2_STABLE remotes/origin/REL9_3_STABLE remotes/origin/REL9_4_STABLE remotes/origin/REL9_5_STABLE remotes/origin/REL9_6_STABLE remotes/origin/REL_10_STABLE remotes/origin/REL_11_STABLE remotes/origin/REL_12_STABLE remotes/origin/Release_1_0_3 remotes/origin/WIN32_DEV remotes/origin/ecpg_big_bison remotes/origin/master

As you can see above, this is list of stable versions from 6 to 12. And master branch is current development version. In this lesson, we will will choose version 12:

git checkout REL_12_STABLE

Second, configure and install postgresql. You can choose a path where postgres will be installed. For example, it will be /usr/local/pgsql :

./configure --prefix = /usr/local/pgsql

After the configuration, we can start the installation. You can install only postgres, only some extensions or postgres with extensions.

Install only PostgreSQL: make sudo make install

Install PostgreSQL, extensions and docs: make world sudo make install-world

Install extensions: cd contrib make sudo make install

Step three - after installation

You need to create postgres user:

sudo adduser postgres sudo su - postgres echo 'PATH=/usr/local/pgsql/bin:$PATH' >> .bashrc source .bashrc

After that create pgdata dir and change it’s owner:

sudo mkdir /usr/local/pgsql/data sudo chown postgres /usr/local/pgsql/data

Initialize postgres database cluster:

sudo su - postgres initdb -k -D /usr/local/pgsql/data # "-k" enabled data page checksums

Check our postgres

Start server:

sudo su - postgres pg_ctl -w -l /home/postgres/logfile -D /usr/local/pgsql/data start

Check our postgres is working:

$ psql -U postgres -c 'select now();' now ------------------------------ 2020-02-25 18:06:59.97087+03 ( 1 row )

After that you can stop server. To do this, use the command pg_ctl stop -m fast|smart|immediate . You can specify one of the three stop modes in the -m key:

fast - forcefully stops sessions and writes changes from RAM to disk;

smart - waits for all sessions to end and writes changes to disk;

immediate - forcefully stops sessions and will need to be restored at startup.

By default, the mode is fast .