PostgreSQL Installation types
In this tutorial, we will show you how to install PostgreSQL by using source code/RPM 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
Postgres database installation 3 types, they are
1. GRAPHICAL Installation (./ postgresql.run).
2. BINAY INSTALLATION (RPM).
3. SOURCE CODE INSTALLATION (./configure)
2. POSTGRES BINARY INSTALLATION :
============================
Step 1. As root user install the below packages
postgresql-infrastructure | Basic Clients and Utilities for PostgreSQL |
postgresql113 | PostgreSQL client programs and libraries |
postgresql113-contrib | Contributed source and binaries distributed with PostgreSQL |
postgresql113-devel | PostgreSQL development header files and libraries |
postgresql113-docs | Extra documentation for PostgreSQL |
postgresql113-libs | The shared libraries required for any PostgreSQL clients |
postgresql113-plperl | The Perl procedural language for PostgreSQL |
postgresql113-plpython | The Python procedural language for PostgreSQL |
postgresql113-pltcl | The Tcl procedural language for PostgreSQL |
postgresql113-server | The programs needed to create and run a PostgreSQL server |
postgresql113-test | The test suite distributed with PostgreSQL |
rpm -ivh postgresql-infrastructure-1-6.noarch.rpm
rpm -ivh postgresql113-libs-11.3-1.x86_64.rpm
rpm -ivh postgresql113-server-11.3-1.x86_64.rpm
rpm -ivh postgresql113-contrib-11.3-1.x86_64.rp
rpm -ivh postgresql113-devel-11.3-1.x86_64.rpm
rpm -ivh postgresql113-docs-11.3-1.x86_64.rpm
rpm -ivh postgresql113-plperl-11.3-1.x86_64.rpm
rpm -ivh postgresql113-plpython-11.3-1.x86_64.rpm
rpm -ivh postgresql113-pltcl-11.3-1.x86_64.rpm
rpm -ivh postgresql113-server-11.3-1.x86_64.rpm
rpm -ivh postgresql113-test-11.3-1.x86_64.rpm
Step 2. Now create a linux user to access the postgres database and create 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. /data_11.3/
Step 3. Initialize the postgres cluster as user 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.
Step 4. put the Web/Apps details on pg_ha.conf file.
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication nijam trust
#host replication nijam 127.0.0.1/32 trust
#host replication nijam ::1/128 trust
host all all 127.0.0.1/32 trust
host all all 54.99.18.56/32 trust
host all all 54.99.17.86/32 trust
Step 5 . Allocate the memory and connection as per the CPU and RAM.
For Example if you have 100 GB RAM.
1. Max_connection =1000×16mb(work_mem)=16 GB
2. Effective_cache_size=40gb optimized execution path.the query planner also requires some space
3. Shared_buffer=25GB,
4.Wall_buffer=3% of shared buffer,if lot of insert, update, delete set maximum 8mb
5.Maintenance_work_mem=4GB to 6GB(RAM/8) for vacuum,create,alter, reindex, backup/restoring dump.
6.Temp_buffer=8mb default session-local buffers used only for access to temporary tables.
Including this change the below parameter :
$ cat postgresql.conf
listen_addresses = '*'
port = 5420
logging_collector=on
log_filename = 'postgresql-%a.log'
log_directory = 'log'
log_rotation_age = 1d
archive_mode=on
archive_command = 'gzip < %p > /srv/postgresql/var/inst1/log/archive_logs/%f'
Step 6. Startup the new cluster as user nijam .
/opt/11.3/bin/pg_ctl -D /data_11.3/ restart
Step 7. Set the bash profile to access the postgres utility on user nijam home 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
Step 8. Run the bash profile and try to connect the database
$ ..profile
$ psql -p 5420
postgres=# \l --- to list all databases in cluster
postgres=# \q --- to quit form postgres console
3. SOURCE CODE INSTALLATION :
=======================
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