How to install PostgreSQL on Ubuntu

PostgreSQL is a feature-rich, general-purpose database management system. Following are the objectives of PostgreSQL:

  • Robust, high-quality software with maintainable, well-commented code.
  • Low maintenance administration of both embedded and enterprise use.
  • Standards-complaint SQL, interoperability, and compatibility.
  • Performance, security and high availability.
  • Its one of top 5 most used database engine.

Prerequisites to install PostgreSQL on Ubuntu:
We need one Ubuntu 18.04 server that has been configured

PostgreSQL Install on Ubuntu steps:

Ubuntu’s default repositories contain PostgreSQL packages. So, we can install these by using apt package management system.

Step1: Enable PostgreSQL Apt Repository

Start with the import of the GPG key for PostgreSQL packages.

sudo apt-get install wget ca-certificates wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Now add the PostgreSQL repository to our system.

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

Step2: Now update the Repository.

sudo apt-get update

Step3: Install PostgreSQL on Ubuntu

Now, install the Postgres package along with a -contrib package that adds some additional utilities and functionality.
Following installation command installs latest PostgreSQL server.

sudo apt-get install postgresql postgresql-contrib

After few seconds, it asks “Do you want to continue? [Y/n]”, then type ‘y’ and press enter.

Installation is successful.

Lets verify the PostgreSQL Server status by running any of following commands.

systemctl status postgresql service postgresql status ps -eaf | grep status pg_ctlcluster 12 main status

● postgresql.service - PostgreSQL RDBMS Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled) Active: active (exited) since Mon 2019-12-23 03:30:19 PST; 7min ago Main PID: 5520 (code=exited, status=0/SUCCESS) Tasks: 0 (limit: 2301) Memory: 0B CGroup: /system.slice/postgresql.service Dec 23 03:30:19 mongodb2 systemd[1]: Starting PostgreSQL RDBMS... Dec 23 03:30:19 mongodb2 systemd[1]: Started PostgreSQL RDBMS.

Step4: Connect to PostgreSQL on Ubuntu

sudo su - postgres psql

So, in this article we covered how to install and connect to PostgreSQL in Ubuntu.