Introduction
PostgreSQL is an open-source, object-relational database system with a strong reputation for feature robustness, extensibility, and technical standards compliance.
The latest version of this database system is PostgreSQL 12.1, while versions 11.6, 10.11, 9.6.16, 9.5.20, and 9.4.25 still get regular support updates.
In this tutorial, you will learn about two different ways of installing the newest PostgreSQL on Ubuntu 18.04.
Prerequisites
- An Ubuntu 18.04 Linux system
- Access to a command-line/terminal window (Ctrl+Alt+T)
- A user account with sudo privileges
Install PostgreSQL from PostgreSQL Apt Repository
PostgreSQL is available in all Ubuntu versions by default, but it doesn’t guarantee automatic updates when new releases come out. The local repository only has "snapshots" of a specific version. The best practice is to install the software from the PostgreSQL Apt Repository.
The PostgreSQL Apt Repository provides the latest PostgreSQL version, as well as all previous server packages, extensions, and modules.
Step 1: Add PostgreSQL Repository
To install from the official repository, you first need to add it to your system.
Import the GPG repository key with the commands:
sudo apt-get install wget ca-certificates
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
Then, add the PostgreSQL repository by typing:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
Step 2: Update the Package List
After adding the official PostgreSQL repository, make sure to update the package list. Doing this ensures you install the latest PostgreSQL package.
sudo apt-get update
Step 3: Install PostgreSQL
To install PostgreSQL and the PostgreSQL contrib package (which provides additional features), use the following command:
sudo apt-get install postgresql postgresql-contrib
By default, the software creates a postgres user, once you successfully install the database system. This user account has the default ‘postgres’ role.
Install PostgreSQL from Local Ubuntu Repository
If you prefer installing PostgreSQL from the local Ubuntu repository, bear in mind that it is most likely not going to be the latest version of the package.
Step 1: Check Available PostgreSQL Version
Before you decide whether you want to set up PostgreSQL from the Ubuntu repository, verify which versions are available. Update the repository and then run the command:
apt show postgresql
The output provides all the necessary information about the package, including the release number and size.
Step 2: Install PostgreSQL Package
If you are happy with the PostgreSQL version accessible from the local repository, use the following command to install the package:
sudo apt install postgresql postgresql-contrib
With these simple steps, you have successfully installed PostgreSQL on Ubuntu 18.04.
Connect to PostgreSQL
To establish a connection with the newly set-up database, log into the postgres account with:
sudo su - postgres
Now open a postgress prompt using the command:
psql
Check Connection Information
If you are connected to PostgreSQL and want to see details of the connection, use the command:
\conninfo
The output displays information on the database name, the account you are logged in, the socket path, and port number.
Conclusion
This article should help you set up PostgreSQL. Whether you decide to install from the PostgreSQL repository or the local Ubuntu repository, both installations are simple and easy to do.
To connect to PostgreSQL and manage SQL queries check out our article on setting up and connecting PostgreSQL with SQL Workbench.
To learn more about PostgreSQL installation, make sure to read our article How to Download and Install PostgreSQL on Windows.