In this tutorial, we will show you how to install PostgreSQL by using source code on your local system for learning and practicing PostgreSQL.
PostgreSQL was developed for UNIX-like platforms, however, it was designed to be portable. It means that PostgreSQL can also run on other platforms such as Mac OS X, Solaris, and Windows.
Since version 8.0, PostgreSQL offers an installer for Windows systems that makes the installation process easier and faster. For development purpose, we will install PostgreSQL version 11.3 on Linux
There are three steps to complete the PostgreSQL installation:
- Download the PostgreSQL source file .
- Configure the PostgreSQL source code .
- Verify the postgres configuration .
1. First install required prerequisites such as gcc, readline-devel and zlib-devel using package manager as shown.
# yum install gcc zlib-devel readline-devel [On RHEL/CentOS] # apt install gcc zlib1g-dev libreadline6-dev [On Debian/Ubuntu] # Zypper in gcc zlib1g-dev libreadline6-dev [On SUSE Linux ]
2. Download the source code tar file from the official postgres website using the following wget command directly on system.
# wget https://ftp.postgresql.org/pub/source/v11.3/postgresql-11.3.tar.gz
3. Use tar command to extract the downloaded tarball file. New directory named postgresql-11.3 will be created.
# tar -xvzf postgresql-11.3.tar.gz # ll Sample Output total 26328 drwxr-xr-x 2 nijam nijam 4096 Sep 21 2014 bin -rwxr-xr-x 1 nijam nijam 347 Nov 7 2017 init-user-db.sh drwxr-xr-x 6 nijam nijam 4096 May 7 04:59 postgresql-11.3 -rw-r--r-- 1 nijam nijam 25868246 Jul 8 12:04 postgresql-11.3.tar.gz drwxr-xr-x 2 nijam nijam 4096 May 17 2017 public_html
4. Next step for installation procedure is to configure the downloaded source code by choosing the options according to your needs.
# cd postgresql-11.3 # ls -l total 764 -rw-r--r-- 1 nijam nijam 730 May 7 04:46 .dir-locals.el -rw-r--r-- 1 nijam nijam 1622 May 7 04:46 .gitattributes -rw-r--r-- 1 nijam nijam 504 May 7 04:46 .gitignore -rw-r--r-- 1 nijam nijam 1192 May 7 04:46 COPYRIGHT -rw-r--r-- 1 nijam nijam 3848 May 7 04:46 GNUmakefile.in -rw-r--r-- 1 nijam nijam 284 May 7 04:46 HISTORY -rw-r--r-- 1 nijam nijam 74257 May 7 04:59 INSTALL -rw-r--r-- 1 nijam nijam 1682 May 7 04:46 Makefile -rw-r--r-- 1 nijam nijam 1212 May 7 04:46 README -rw-r--r-- 1 nijam nijam 522 May 7 04:46 aclocal.m4 drwxr-xr-x 2 nijam nijam 4096 May 7 04:58 config -rwxr-xr-x 1 nijam nijam 561752 May 7 04:46 configure -rw-r--r-- 1 nijam nijam 84451 May 7 04:46 configure.in drwxr-xr-x 56 nijam nijam 4096 May 7 04:58 contrib drwxr-xr-x 3 nijam nijam 4096 May 7 04:58 doc drwxr-xr-x 16 nijam nijam 4096 May 7 04:59 src Note : use ./configure --help to get help about various options.
5. Now create a directory where you want to install postgres files and use prefix option with configure.
# mkdir /opt/11.3/ # ./configure --prefix=/opt/11.3/ (or) ./configure --prefix=/opt/11.3/ --without-readline # ./configure --prefix=/opt/11.3/ --without-readline --without-zlib
6. After configuring, next we will start to build postgreSQL using following make command.
# make (or) # make world (additional modules (contrib), type instead PostgreSQL, contrib, and documentation # make install (or) # make install-world (if you want contribution extension)
7. Postgresql 11 has been installed in “/opt/11.3/” directory. now create a postgres user and directory to be used as data directory for initializing database cluster. Owner of this data directory should be postgres user and permissions should be 700 and also set path for postgresql binaries for our ease.
# useradd nijam # passwd nijam # mkdir -p /data_11.3/ # chown -R nijam.nijam /data_11.3/
8. Now initialize database using the following command as postgres user before using any postgres commands.
# su nijam $ /opt/11.3/bin/initdb -D /data_11.3/ -U nijam
Where -D is location for this database cluster or we can say it is data directory where we want to initialize database cluster, -U for database superuser name.
For more info and options we can refer initdb –help.
9. After initializing database, start the database cluster or if you need to change port or listen address for server, edit the postgresql.conf file in data directory of database server.
$ /opt/11.3/bin/pg_ctl -D /data_11.3/ -l logfile start
10. After starting database, verify the status of postgres server process by using following commands.
$ ps -ef |grep -i postgres $ netstat -apn |grep -i 5420
We can see that database cluster is running fine, and startup logs can be found at location specified with -l option while starting database cluster.
11. Now connect to database cluster and create database by using following commands.
$ psql -p 5420 postgres=# \l --- to list all databases in cluster postgres=# \q --- to quit form postgres console
12. Create the environment variable file then only you can able to access the postgres utility directly on home path without going to bin path.
$ cat /home/nijam/.profile export PATH=/opt/11.3/bin:$PATH export PGHOME=/opt/11.3/ export PGDATA=/data_11.3/ export LD_LIBRARY_PATH=/opt/11.3/lib export PGDATABASE=nijam export PGUSER=nijam export PGPORT=5420 export PGLOCALEDIR=/opt/11.3/share/locale export MANPATH=$MANPATH:/opt/11.3/share/man