How to Install PostgreSQL 9.6 on Ubuntu Linux

Posted on

Another article with the title of ‘How to Install PostgreSQL 9.6 on Ubuntu Linux’ is written. Based on the title of the article, it is clearly written to describe the steps or the processes taken to install PostgreSQL 9.6 on Ubuntu Linux. Below are steps taken for achieving the purpose of installing PostgreSQL9.6 as shown below :

1. First of all, check first whether package installation named postgresql-9.6 which represent the PostgreSQL 9.6 database package installer exist by using the Ubuntu-based package management tool named ‘apt’ as shown below :

apt search postgresql-9.6

Below is the output of the execution of the above command :

root@hostname:~# apt search postgresql-9.6
Sorting... Done
Full Text Search... Done
postgresql-9.6/xenial-pgdg,now 9.6.9-2.pgdg16.04+1 amd64 [installed]
  object-relational SQL database, version 9.6 server
...
root@hostname:~# apt search postgresql-9.6

The package name exist which is specified based on the result given upon executing the above command. The package name is ‘postgresql-9.6’. So, carry on to the next step.

2. Execute the command ‘apt-get install postgresql-9.6’ as shown below :

root@hostname:~# apt-get install postgresql-9.6
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  comerr-dev krb5-multidev libgssrpc4 libkadm5clnt-mit9 libkadm5srv-mit9 libkdb5-8 libssl-dev libssl-doc
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  libpq-dev libpq5 pgdg-keyring postgresql-client-9.6 postgresql-client-common postgresql-common postgresql-contrib-9.6
Suggested packages:
  postgresql-doc-10 locales-all postgresql-doc-9.6 libdbd-pg-perl
The following NEW packages will be installed:
  pgdg-keyring postgresql-9.6 postgresql-client-9.6 postgresql-client-common postgresql-common postgresql-contrib-9.6
The following packages will be upgraded:
  libpq-dev libpq5
2 upgraded, 6 newly installed, 0 to remove and 19 not upgraded.
Need to get 6.557 kB of archives.
After this operation, 29,2 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://apt.postgresql.org/pub/repos/apt xenial-pgdg/main amd64 libpq-dev amd64 10.4-2.pgdg16.04+1 [159 kB]
Get:2 http://apt.postgresql.org/pub/repos/apt xenial-pgdg/main amd64 libpq5 amd64 10.4-2.pgdg16.04+1 [163 kB]
Get:3 http://apt.postgresql.org/pub/repos/apt xenial-pgdg/main amd64 pgdg-keyring all 2017.3 [10,3 kB]
Get:4 http://apt.postgresql.org/pub/repos/apt xenial-pgdg/main amd64 postgresql-client-common all 191.pgdg16.04+1 [82,3 kB]
Get:5 http://apt.postgresql.org/pub/repos/apt xenial-pgdg/main amd64 postgresql-client-9.6 amd64 9.6.9-2.pgdg16.04+1 [1.241 kB]                                                
Get:6 http://apt.postgresql.org/pub/repos/apt xenial-pgdg/main amd64 postgresql-common all 191.pgdg16.04+1 [222 kB]                                                            
Get:7 http://apt.postgresql.org/pub/repos/apt xenial-pgdg/main amd64 postgresql-9.6 amd64 9.6.9-2.pgdg16.04+1 [4.181 kB]                                                       
Get:8 http://apt.postgresql.org/pub/repos/apt xenial-pgdg/main amd64 postgresql-contrib-9.6 amd64 9.6.9-2.pgdg16.04+1 [498 kB]                                                 
Fetched 6.557 kB in 2min 41s (40,5 kB/s)                                                                                                                                       
Preconfiguring packages ...
(Reading database ... 403627 files and directories currently installed.)
Preparing to unpack .../libpq-dev_10.4-2.pgdg16.04+1_amd64.deb ...
Unpacking libpq-dev (10.4-2.pgdg16.04+1) over (9.5.12-0ubuntu0.16.04) ...
Preparing to unpack .../libpq5_10.4-2.pgdg16.04+1_amd64.deb ...
Unpacking libpq5:amd64 (10.4-2.pgdg16.04+1) over (9.5.12-0ubuntu0.16.04) ...
Selecting previously unselected package pgdg-keyring.
Preparing to unpack .../pgdg-keyring_2017.3_all.deb ...
Unpacking pgdg-keyring (2017.3) ...
Selecting previously unselected package postgresql-client-common.
Preparing to unpack .../postgresql-client-common_191.pgdg16.04+1_all.deb ...
Unpacking postgresql-client-common (191.pgdg16.04+1) ...
Selecting previously unselected package postgresql-client-9.6.
Preparing to unpack .../postgresql-client-9.6_9.6.9-2.pgdg16.04+1_amd64.deb ...
Unpacking postgresql-client-9.6 (9.6.9-2.pgdg16.04+1) ...
Selecting previously unselected package postgresql-common.
Preparing to unpack .../postgresql-common_191.pgdg16.04+1_all.deb ...
Adding 'diversion of /usr/bin/pg_config to /usr/bin/pg_config.libpq-dev by postgresql-common'
Unpacking postgresql-common (191.pgdg16.04+1) ...
Selecting previously unselected package postgresql-9.6.
Preparing to unpack .../postgresql-9.6_9.6.9-2.pgdg16.04+1_amd64.deb ...
Unpacking postgresql-9.6 (9.6.9-2.pgdg16.04+1) ...
Selecting previously unselected package postgresql-contrib-9.6.
Preparing to unpack .../postgresql-contrib-9.6_9.6.9-2.pgdg16.04+1_amd64.deb ...
Unpacking postgresql-contrib-9.6 (9.6.9-2.pgdg16.04+1) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Processing triggers for systemd (229-4ubuntu21.2) ...
Processing triggers for ureadahead (0.100.0-19) ...
Setting up libpq5:amd64 (10.4-2.pgdg16.04+1) ...
Setting up libpq-dev (10.4-2.pgdg16.04+1) ...
Setting up pgdg-keyring (2017.3) ...
Removing apt.postgresql.org key from trusted.gpg: OK
Setting up postgresql-client-common (191.pgdg16.04+1) ...
Setting up postgresql-client-9.6 (9.6.9-2.pgdg16.04+1) ...
update-alternatives: using /usr/share/postgresql/9.6/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode
Setting up postgresql-common (191.pgdg16.04+1) ...

Creating config file /etc/postgresql-common/createcluster.conf with new version
Building PostgreSQL dictionaries from installed myspell/hunspell packages...
  en_au
  en_ca
  en_gb
  en_us
  en_za
  ko
Removing obsolete dictionary files:
Setting up postgresql-9.6 (9.6.9-2.pgdg16.04+1) ...
Creating new PostgreSQL cluster 9.6/main ...
/usr/lib/postgresql/9.6/bin/initdb -D /var/lib/postgresql/9.6/main --auth-local peer --auth-host md5
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locales
  COLLATE:  en_US.UTF-8
  CTYPE:    en_US.UTF-8
  MESSAGES: en_US.UTF-8
  MONETARY: id_ID.UTF-8
  NUMERIC:  id_ID.UTF-8
  TIME:     id_ID.UTF-8
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/9.6/main ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

Success. You can now start the database server using:

    /usr/lib/postgresql/9.6/bin/pg_ctl -D /var/lib/postgresql/9.6/main -l logfile start

Ver Cluster Port Status Owner    Data directory               Log file
9.6 main    5432 down   postgres /var/lib/postgresql/9.6/main /var/log/postgresql/postgresql-9.6-main.log
update-alternatives: using /usr/share/postgresql/9.6/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode
Setting up postgresql-contrib-9.6 (9.6.9-2.pgdg16.04+1) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Processing triggers for systemd (229-4ubuntu21.2) ...
Processing triggers for ureadahead (0.100.0-19) ...
root@hostname:/tmp# 

3. After successfully installed, start to run the server by executing the following command which has already been informed in the previous output :

root@hostname:/~# /usr/lib/postgresql/9.6/bin/pg_ctl -D /var/lib/postgresql/9.6/main -l logfile start
pg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.
root@hostname:/~#

4. To be noticed based on the above execution, just execute the command using ‘postgres’ account. So, the execution is shown below :

root@hostname:/tmp# su - postgres
postgres@hostname:~$ /usr/lib/postgresql/9.6/bin/pg_ctl -D /var/lib/postgresql/9.6/main -l logfile start
pg_ctl: another server might be running; trying to start server anyway
server starting
postgres@hostname:~$

5. Check whether the service of PostgreSQL has been started by executing the following command :

postgres@hostname:~$ ps aux | grep postgres
...
postgres 23033  0.0  0.3 308980 24756 ?        S    08:42   0:00 /usr/lib/postgresql/9.6/bin/postgres -D /var/lib/postgresql/9.6/main -c config_file=/etc/postgresql/9.6/main/postgresql.conf
postgres 23035  0.0  0.0 308980  4032 ?        Ss   08:42   0:00 postgres: 9.6/main: checkpointer process   
postgres 23036  0.0  0.0 308980  4032 ?        Ss   08:42   0:00 postgres: 9.6/main: writer process   
postgres 23037  0.0  0.0 308980  4032 ?        Ss   08:42   0:00 postgres: 9.6/main: wal writer process   
postgres 23038  0.0  0.0 309384  6940 ?        Ss   08:42   0:00 postgres: 9.6/main: autovacuum launcher process   
postgres 23039  0.0  0.0 163980  3356 ?        Ss   08:42   0:00 postgres: 9.6/main: stats collector process   
root     23374  0.0  0.0  58076  3620 pts/3    S    08:44   0:00 su - postgres
postgres 23375  0.1  0.0  26944  5712 pts/3    S    08:44   0:00 -su
postgres 23403  0.0  0.0  41064  3236 pts/3    R+   08:44   0:00 ps aux
postgres 23404  0.0  0.0  17924  1008 pts/3    S+   08:44   0:00 grep postgres
postgres@hostname:~$ exit

6. The above command is checking all running process with its associate user who created the process which in the context of PostgreSQL 9.6 database service is owned by a user named ‘postgres’.

7. Switch to user postgres and test to connect to PostgreSQL 9.6 database to prove the database has running and accepting incoming request :

postgres@hostname:~$

Test access
postgres@hostname:~$ psql -Upostgres
psql (9.6.9)
Type "help" for help.

postgres=# 

Leave a Reply