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 http://apt.postgresql.org/pub/repos/apt/ zesty-pgdg main

--------------- On Ubuntu 16.04 ---------------
deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main

--------------- On Ubuntu 14.04 ---------------
deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main

On Debian Systems

--------------- On Stretch 9.x ---------------
deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main

--------------- On Jessie 8.x ---------------
deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main

--------------- On Wheezy 7.x ---------------
deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main

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

$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | 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: https://www.postgresql.org/

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 Tecmint.com for interesting Linux stuff.

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

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

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.