PostgreSQL (also called postgres) is a very famous, powerful, open-source object-relational database management system that has been around for almost 30 years. PostgreSQL requires very minimum maintained efforts because of its stability. Therefore, if you develop applications based on PostgreSQL, the total cost of ownership is low in comparison with other database management systems.
Recommended Read: Simple guide to install POSTGRESQL on Ubuntu
Also Read: How to setup SSH login without password on Linux systems
In this tutorial, we will learn to install PostgreSQL from source. So let’s start the tutorial,
Pre-requisites
We need to install some packages on servers before we can install PostgreSQL from source,
CentOS/RHEL
# yum groupinstall “Development Tools” -y
# yum install gcc zlib-devel readline-devel systemd-devel -y
Ubuntu
# sudo apt-get install build-essential -y
# sudo apt install gcc zlib1g-dev libreadline6-dev libsystemd-dev -y
Now we can start the process to install PostgreSQL from source.
Install Postgres from source
The first step would be to download the source package for Postgres. Here we will be installing PostgreSQL 11.6,
# wget -O – https://ftp.postgresql.org/pub/source/v11.6/postgresql-11.6.tar.bz2 | tar jxf –
# cd postgresql-11.6
We will now configure postgresql,
# ./configure –with-systemd
Next, run the following commands,
# make
# make install
So these commands will install postgresql from the source. Next step would be to create a user for the postgres installation,
# useradd postgres
# passwd -d postgres
Next, we will also create a data directory for postgres,
# mkdir /usr/local/pgsql/data
# chown postgres /usr/local/pgsql/data
The next step would be to initialize the database. To do that, change the user to postgres & execute the following command,
# su – postgres
$ /usr/local/pgsql/bin/initdb -A trust -D /usr/local/pgsql/data
Once this is done, we can start the database but we need to create a service file to start the database. Revert back to superuser & create a service file,
# vi /etc/systemd/system/postgresql.service
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)
[Service]
Type=notify
User=postgres
ExecStart=/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
ExecReload=/bin/kill -HUP \\\$MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0
[Install]
WantedBy=multi-user.target
EOF
Save the file & exit. Then run the following command to start the database service,
# systemctl daemon-reload
# systemctl enable postgresql.service
# systemctl start postgresql.service
That’s it, the database is now ready to use.
Note: The Same method can be used to install other postgresql versions as well, the only change would download link for the version you wish to install.
We now end this tutorial on how to install postgresql from source, in our future tutorial we will discuss some other important Postgresql commands. Please feel free to send in any questions or queries using the comment box below.