Siva Krishna Reddy
PostgreSQL 9.6 Server Installation
File System Structure
/opt -----> For Software Installation
/pgdata ------> For Data Storage (like Tablespaces, Tables, Indexes etc…)
1. Install the Postgres RPM using below commands
Install the repository RPM:
# yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-
x86_64/pgdg-redhat-repo-latest.noarch.rpm
Install PostgreSQL:
# yum install postgresql96
Siva Krishna Reddy
# yum install postgresql96-server
# yum install postgresql96-contrib
Siva Krishna Reddy
2. Add Postgres9.6 Service to systemctl, it will create the postgres service file under
(lib/systemd/system)
# systemctl enable postgresql-9.6
# ls -lrth /usr/lib/systemd/system/postgresql-9.6.service
# ls -lrth /lib/systemd/system/postgresql-9.6.service
3. Create Necessary Directory Structure and change the ownership to postgres
# mkdir -p /opt/postgresql/9.6
# chown postgres:postgres /opt/postgresql/9.6
4. Update the postgres service file with the required PGDATA location
# vi /lib/systemd/system/postgresql-9.6.service
update PGDATA location as follows:
# Location of database directory
#Environment=PGDATA=/var/lib/pgsql/12/data/
Environment=PGDATA=/opt/postgresql/9.6/data/
Siva Krishna Reddy
5. Verify the postgres port is enable (5432)
firewall-cmd --list-ports
firewall-cmd --permanent --add-port=5432/tcp
firewall-cmd --reload
firewall-cmd --list-ports
6. Switch to postgres user and perform initdb (it creates the postgres cluster)
Creating a Database Cluster
Before you can do anything, you must initialize a database storage area on disk. We call this a database
cluster. (The SQL standard uses the term catalog cluster.) A database cluster is a collection of databases
that is managed by a single instance of a running database server.
In file system terms, a database cluster is a single directory under which all data will be stored. We call
this the data directory or data area. It is completely up to you where you choose to store your data.
There is no default, although locations such as /usr/local/pgsql/data or /var/lib/pgsql/data are
Siva Krishna Reddy
popular. To initialize a database cluster, use the command initdb, which is installed with PostgreSQL.
The desired file system location of your database cluster is indicated by the -D option, for example:
$ /usr/pgsql-9.6/bin/initdb -D /opt/postgresql/9.6/data
Tip: As an alternative to the -D option, you can set the environment variable PGDATA.
Alternatively, you can run initdb via the pg_ctl program like so:
$ pg_ctl -D /opt/postgresql/9.6/data initdb
initdb will refuse to run if the data directory exists and already contains files; this is to prevent
accidentally overwriting an existing installation.
Because the data directory contains all the data stored in the database, it is essential that it be secured
from unauthorized access. initdb therefore revokes access permissions from everyone but the
PostgreSQL user.
# su - postgres
$ export PGDATA=/opt/postgresql/9.6/data
$ echo $PGDATA
$ /usr/pgsql-9.6/bin/initdb -D /opt/postgresql/9.6/data
Siva Krishna Reddy
7. You can now start the database server using:
$ /usr/pgsql-9.6/bin/pg_ctl -D /opt/postgresql/9.6/data -l logfile start
Now we can see the server process and utility process as shown below
8. You can stop the database server using:
$ /usr/pgsql-9.6/bin/pg_ctl stop -D /opt/postgresql/9.6/data
Note: - Installation procedure same for all versions
Siva Krishna Reddy