How to Install PostgreSQL 9.5 on Ubuntu (12.04 - 15.10)

PostgreSQL is a relational database management system, it is a fully ACID compliant enterprise-class database system with support for stored procedures in many programming languages such as Python, Java, C/C++, Ruby, Perl, or even with its own language PL/pgSQL. The language PL/pgSQL is very similar to PL/SQL in Oracle. Postgres supports several operating systems like Linux, UNIX (AIX, BSD, HP-UX, SGI IRIX, Mac OS X, Solaris, Tru64), and Windows.

This tutorial shows the installation of the latest PostgreSQL 9.5 version on Ubuntu. We will use the official repository from, so you can update your server easily in future and get direct updates and patches from the database vendor. The steps have been tested with Ubuntu versions from 12.04 until 15.10.


Add the PostgreSQL repository

An additional Ubuntu repository can be added by appending a new line to the file /etc/apt/sources.list or by adding a new file in the directory /etc/apt/sources.list.d/. We will use the second option here as it is the cleaner solution. The PostgreSQL version provided in the Ubuntu base repository is a bit older, so we will not use that here. The following commandwill add the new repository configuration file.

sudo sh -c 'echo "deb `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

Then we will import the GPG key of the repository so apt can check the validity of the packages.

wget -q -O - | sudo apt-key add -

 Sample output:

Add the postgresql repository.

Update the package list

After adding the repository, we will have to update the package list. Run the apt-get update command to load the list of available packages from

sudo apt-get update 

Sample output:

Update the package list.

PostgreSQL Installation

The following command installs the latest PostgreSQL version on Ubuntu:

 sudo apt-get install postgresql postgresql-contrib

 Sample output:

Install PostgreSQL.

Output when installing Postgres.

Connecting to the Postgres Server

The successful installation of PostgreSQL will create a user and role named 'postgres' in the database and a Linux system user with the same name. This system user has administrative privileges in the database. Hence, in order to connect to the PostgreSQL server, you should log in as "postgres" and then proceed to connect the appropriate database.

Become the "postgres" Linux user:

sudo su - postgres

Start the psql shell to login to the database server.


 Sample output:

Connect to postgres.

Once you are logged into the PostgreSQL database server, check the connection and PostgreSQL version with the following command from your database prompt.

postgres-# \conninfo

 Sample output:

Check the postgresql connection info.

Disconnect from Postgres

The command to disconnect from PostgreSQL database is "\q", the char q stands for quit here. 

postgres-# \q

Now you are back on the Linux shell, but you are still the user "postgres", to become your "normal" Linux user again, enter the word "exit".


Sample output:

Disconnect from PostgreSQL.

Share this page:

4 Comment(s)

Add comment


From: Karen at: 2016-02-29 13:55:33

When including the ppa in the ppa list (1st code block in your step-by-step description), the file to update is in reality:


A typo in yr how-to made it "pgdg.list" instead.

From: Slava Fomin at: 2016-04-27 17:46:50

Thanks! Works like a charm on 15.10 ; )

From: Tim at: 2016-05-05 07:46:33

I wish all installations were this painless, thanks for putting this together!

From: Tiru at: 2016-07-06 17:45:59

I followed  the  same procedure , i am able to login to postgres server ,but after that it wont work for me. while typing psql , it is showing below error,


[email protected]:~$ sudo su - [email protected]:~$ psqlThe program 'psql' can be found in the following packages: * postgresql-client-common * postgres-xc-clientAsk your administrator to install one of [email protected]:


while trying to install above two packages it is asking for password,


Ask your administrator to install one of [email protected]:~$ sudo apt-get install postgres-xc-client[sudo] password for postgres:


what would be the password for this ? please help me it is urgent