How to install PostgreSQL in Linux in 2 easy steps

PostgreSQL can be installed by means of two ways

  1. Installing from source
  2. Installing binary packages

NOTE: PostgreSQL 13 Installation on RedHat 7 and everything about PostgreSQL that root can do is found here

In this tutorial, you will learn how to install PostgreSQL in Linux using source code.

Step by Step PostgreSQL installation using binary packages is found here

There are two steps involved in creating a PostgreSQL server.

  1. Preparing the setup with prerequisites.
  2. How to install PostgreSQL server.

Preparing the setup with prerequisites

Before we get into actual installation of PostgreSQL server we shall have to check the pre-requisites that are defined for PostgreSQL installation.

The machine I am using for the entire tutorial is el6-64 Bit.

[root@postgreshelp ~]# uname -a
Linux postgreshelp 2.6.39-400.17.1.el6uek.x86_64 #1 SMP Fri Feb 22 18:16:18 PST 2013 x86_64 x86_64 x86_64 GNU/Linux
[root@postgreshelp ~]#

Pre-requisite check to install PostgreSQL

A basic installation of Linux would sufficiently enough to install PostgreSQL on Linux, however, we check the basic requirements.

  1. Make sure the GNU make version 3.80.
[root@postgreshelp ~]# make --version
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for x86_64-unknown-linux-gnu
[root@postgreshelp ~]#

2. A GNU C compiler. Recent versions of GCC are recommended.

[root@postgreshelp ~]# rpm -qa gcc*
gcc-4.4.7-18.el6.x86_64
gcc-c++-4.4.7-18.el6.x86_64
[root@postgreshelp ~]#

       3. tar is required to unpack the source distribution, in addition to either gzip or bzip2.

Apart from these, there are two additional pre-requisites libraries which are recommended to have.

They are.

The GNU Readline library

It allows psql (the PostgreSQL command line SQL interpreter) to remember each command you type, and allows you to use arrow keys to recall and edit previous commands. This is very helpful and is strongly recommended.

If you don’t want to use it then you must specify the --without-readline option to configure

[root@postgreshelp ~]# rpm -qa readline
readline-6.0-4.el6.x86_64
[root@postgreshelp ~]#

The zlib compression library

zlib is a software library used for data compression

If you don’t want to use it then you must specify the --without-zlib option to configure.

[root@postgreshelp ~]# rpm -qa zlib
zlib-1.2.3-29.el6.x86_64
[root@postgreshelp ~]#

How to install a PostgreSQL database server – step by step:

There are few advantages of installing PostgreSQL server using binaries. The main advantages include

  1. Split the binary and library directories and make sure that PostgreSQL does not integrate tightly into the existing OS and OS directories.
  2. Custom configure options, or some other options, to enable dtrace.
  3. Highly customizable.

How to install PostgreSQL server

Note: This below mentioned steps are applicable for any version you download from PostgreSQL site including PostgreSQL Version 13, Read more about PostgreSQL v13 Master Guide here.

Step 1: Download the source code.

Download PostgreSQL-9.6.8 binaries from the official PostgreSQL site or Click Here to download the source code.

You should get a file named postgresql-9.6.8.tar.gz

Step 2: Extract the tar archive

gunzip postgresql-9.6.8.tar.gz
tar xvf postgresql-9.6.8.tar

This will create a directory called postgresql-9.6.8 containing the PostgreSQL source code.

Go to the directory and open the file INSTALL which gives you insights on how to install postgresql in Linux.

Following the steps mentioned in the INSTALL file will install the postgresql server, let’s have a look at the steps:

Short Version

./configure
make
su
make install
adduser postgres
mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data
su - postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >logfile 2>&1 &
/usr/local/pgsql/bin/createdb test
/usr/local/pgsql/bin/psql test

Now, we will try to understand each step in the document while doing the installation.

Step 3: configure

[root@postgreshelp postgresql-9.6.8]# ./configure

configure check your system to see if all libraries you need are present. It generates vital parts of the build infrastructure.

The default configuration will build the server and utilities, as well as all client applications and interfaces that require only a C compiler.

All files will be installed under /usr/local/pgsql by default.

You can customize the build and installation process by supplying one or more commands mentioned here.

[root@postgreshelp postgresql-9.6.8]# ./configure
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether to build with 64-bit integer date/time support... yes
checking whether NLS is wanted... no
checking for default port number... 5432
checking for block size... 8kB
checking for segment size... 1GB
checking for WAL block size... 8kB
..
..
..
config.status: linking src/backend/port/dynloader/linux.h to src/include/dynloader.h
config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port
[root@postgreshelp postgresql-9.6.8]#
[root@postgreshelp postgresql-9.6.8]#

The successful configure should end as described above.

In some cases, you might find that our operating system lacks libraries needed to compile PostgreSQL properly. Some of the most common candidates are libreadline-dev and zlib-dev (of course there are some more).

In that case, the configure will show error as shown below.

configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.
[root@postgreshelp postgresql-9.6.8]#

[OR]

configure: error: zlib library not found
If you have zlib already installed, see config.log for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-zlib to disable zlib support.
[root@postgreshelp postgresql-9.6.8]#

Step 4: Build a PostgreSQL.

We can move forward and actually compile PostgreSQL, using the following commands one by one with root user.

4.1 make

[root@postgreshelp postgresql-9.6.8]# su
[root@postgreshelp postgresql-9.6.8]# make
make -C src all
make[1]: Entering directory `/root/postgresql-9.6.8/src'
make -C common all
..
..
make[1]: Leaving directory `/root/postgresql-9.6.8/config'
All of PostgreSQL successfully made. Ready to install.

4.2 make install

[root@postgreshelp postgresql-9.6.8]# make install
make -C src install
make[1]: Entering directory `/root/postgresql-9.6.8/src'
..
..
make[1]: Leaving directory `/root/postgresql-9.6.8/config'
PostgreSQL installation complete.
[root@postgreshelp postgresql-9.6.8]

This completes the PostgreSQL server installation.

The build will take a few minutes depending on your hardware.

If you want to scale out the build process to many CPU cores, you can use –j, shown as follows:

make -j 8

The -j 8 command will tell make to do up to 8 things in parallel, if possible. Adding parallelism to the build process will definitely speed up the process.

Now we will move on to creating and configuring PostgreSQL database instance.

Step 1: Add postgres user at OS level.

[root@postgreshelp postgresql-9.6.8]# adduser postgres
[root@postgreshelp postgresql-9.6.8]#

Step 2: Create required directory for database files and assign required permissions.

mkdir /u02/pgsql/data

chown postgres /u02/pgsql/data

[root@postgreshelp data]# mkdir /u02/pgsql/data
[root@postgreshelp data]# chown postgres /u02/pgsql/data

Step 3: initialize the database cluster

create a new PostgreSQL database cluster using initdb command.

syntax: initdb [option…] [--pgdata | -Ddirectory

[postgres@postgreshelp ~]$ /usr/local/pgsql/bin/initdb -D /u02/pgsql/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /u02/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/local/pgsql/bin/pg_ctl -D /u02/pgsql/data -l logfile start

[postgres@postgreshelp ~]$

The above command initializes the postgres database cluster and installs reqired files under the data directory specified.

Step 4: Start the database cluster:

As given in the above step, we can now start the database cluster using the below command.

[postgres@postgreshelp ~]$ /usr/local/pgsql/bin/pg_ctl -D /u02/pgsql/data -l logfile start
server starting
[postgres@postgreshelp ~]$

This completes the PostgreSQL server installation along with PostgreSQL database cluster initialization.

On the successful start of the database cluster, the following processes will run by default.

[postgres@postgreshelp ~]$ ps -ef | grep postgres
root      20082   4435  0 20:28 pts/2    00:00:00 su - postgres
postgres  20083  20082  0 20:28 pts/2    00:00:00 -bash
postgres  20127      1  0 20:28 pts/2    00:00:00 /usr/local/pgsql/bin/postgres -D /u02/pgsql/data
postgres  20129  20127  0 20:28 ?        00:00:00 postgres: checkpointer process
postgres  20130  20127  0 20:28 ?        00:00:00 postgres: writer process
postgres  20131  20127  0 20:28 ?        00:00:00 postgres: wal writer process
postgres  20132  20127  0 20:28 ?        00:00:00 postgres: autovacuum launcher process
postgres  20133  20127  0 20:28 ?        00:00:00 postgres: stats collector process
postgres  20137  20083  0 20:29 pts/2    00:00:00 ps -ef
postgres  20138  20083  0 20:29 pts/2    00:00:00 grep postgres
[postgres@postgreshelp ~]$

You will learn more about the processes here.

We can then login to the default database called postgres with a default database user postgres using the following command

$ psql

[postgres@postgreshelp ~]$  /usr/local/pgsql/bin/psql
psql (9.6.8)
Type "help" for help.

postgres=#
postgres=#

Further Reading:

What actually happens when you do configure, make and make install?

The entire process of behind the scenes is described here.

Bonus: PostgreSQL 13 New Features Practical Master Guide is here.

 

Words from postgreshelp

Thank you for giving your valuable time to read the above information. I hope the content served your purpose in reaching out the blog.
Suggestions for improvement of the blog are highly appreciable. Please contact us for any information/suggestion/feedback.

If you want to be updated with all our articles

please follow us on Facebook Twitter
Please subscribe to our newsletter.

 

2 thoughts on “How to install PostgreSQL in Linux in 2 easy steps”

Leave a Comment