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.

If you read this far, tweet to the author to show them you care. Tweet a thanks
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.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.