How to Install PostgreSQL 9.6 on Debian and Ubuntu

PostgreSQL is a powerful, highly scalable, open source and cross-platform object-relational database system that runs on Unix-like operating systems including Linux and Windows OS. It is an enterprise level database system which is highly reliable and offers data integrity and correctness to users.

In our earlier article, we’ve explained PostgreSQL 10 installation on CentOS/RHEL and Fedora. In this article, we will explain how to install PostgreSQL 9.6 on Debian, Ubuntu and its derivatives using official PostgreSQL APT repository.

Add PostgreSQL APT Repository

This official PostgreSQL APT repository will combine with your Linux system and offers automatic updates for all supported versions of PostgreSQL on Debian and Ubuntu distributions.

To add the apt repository, first create the file /etc/apt/sources.list.d/pgdg.list, and add a line for the repository as per your distribution.

On Ubuntu Systems

--------------- On Ubuntu 17.04 ---------------
deb zesty-pgdg main

--------------- On Ubuntu 16.04 ---------------
deb xenial-pgdg main

--------------- On Ubuntu 14.04 ---------------
deb trusty-pgdg main

On Debian Systems

--------------- On Stretch 9.x ---------------
deb stretch-pgdg main

--------------- On Jessie 8.x ---------------
deb jessie-pgdg main

--------------- On Wheezy 7.x ---------------
deb wheezy-pgdg main

Then import the repository signing key, and update the system package lists like this.

$ wget --quiet -O - | sudo apt-key add -
$ sudo apt update 
Add PostgreSQL Key

Add PostgreSQL Key

Install PostgreSQL Server

Once you have added the PostgreSQL apt repository in your respective Linux distribution, now install PostgreSQL server and client packages as follows:

$ sudo apt install postgresql-9.6-server postgresql-9.6  
Install Postgresql Database Server

Install Postgresql Database Server

Important: Unlike in RHEL/CentOS/Fedora where you have to manually initialize the database system, in Ubuntu/Debian, it is initialized automatically. Therefore simply proceed to start database server as described in the next section.

The PostgreSQL data directory /var/lib/postgresql/9.6/main contains all of the data files for the database.

Start and Enable PostgreSQL Server

With the database server initialized, start PostgreSQL service and enable PostgreSQL service to auto start on system boot like this.

--------------- On SystemD --------------- 
$ sudo systemctl start postgresql.service
$ sudo systemctl enable postgresql.service 
$ sudo systemctl status postgresql.service 

--------------- On SysVinit --------------- 
$ sudo service postgresql-9.6 start
$ sudo chkconfig postgresql on
$ sudo service postgresql-9.6 status
Start and Enable PostgreSQL Database

Start and Enable PostgreSQL Database

Verify PostgreSQL Installation

After installing PostgreSQL database system on your server, verify its installation by connecting to postgres database server. The PostgreSQL administrator user is named as postgres, type this command to access the user system account.

$ sudo su postgres
# cd
# psql
Verify PostgreSQL Database

Verify PostgreSQL Database

To set a password for the postgre database administrator user, use this command:

postgres=# \password postgres
Set Password for PostgreSQL Admin

Set Password for PostgreSQL Admin

To secure the postgre user system account, use the password command below.

$ sudo passwd postgres 

Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

$su - postgre
$ ls
$ psql
Set Password for Postgre User

Set Password for Postgre User

For more information, go to the PostgreSQL Homepage:

Lastly, also read through these articles about popular database management systems:

  1. Installing MariaDB 10.1 in Debian Jessie and Running Various MariaDB Queries
  2. How to Change a Default MySQL/MariaDB Data Directory in Linux
  3. How to Install and Secure MariaDB 10 in CentOS 7
  4. How to Install and Secure MariaDB 10 in CentOS 6
  5. Install MongoDB Community Edition 3.2 on Linux Systems

That’s all for now! To share any thoughts with us, make use of the feedback form below. Remember to always stay connected to for interesting Linux stuff.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.