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.

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.

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

       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

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.

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

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:

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

Step 3: 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.

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.

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

4.2 make install

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.

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

mkdir /u02/pgsql/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

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.

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.

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

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.

 

This Post Has 2 Comments

  1. ATUL KUMAR

    Thank you so much for the crystal clear steps.

Leave a Reply