Ubuntu

Simplified Postgres Installation on Ubuntu

PostgreSQL is pre-installed in every version of Ubuntu; it does not ensure automated upgrades when fresh versions are released. The most recent PostgreSQL release and all prior server bundles, extensions, and plugins are available via the PostgreSQL Apt Repository. Installing Postgres on a server that runs in Ubuntu 20.04 is demonstrated in this tutorial. Additionally, it offers some guidelines for the basic management of databases.

Update and Upgrade the System Repositories

Letā€™s get started with the update of a system before the installation of Postgres on the Ubuntu system. This is required to refresh the system repositories via the ā€œupdateā€ instruction and the ā€œaptā€ package with the sudo rights.

$ sudo apt update

After successfully updating the Ubuntu system, itā€™s necessary to upgrade the system repositories and modules to avoid any inconvenience in the future. In the very same way, the apt repository is used in the ā€œupgradeā€ command as follows:

$ sudo apt upgrade

Install the Prerequisites

Now that we will install PostgreSQL from its official repository, we need to ensure that the prerequisites are already installed. These prerequisites include the ā€œwgetā€ utility to add the Postgres key in Ubuntu and ā€œca-certificatesā€ to enable the installation by enabling the certificates for secure installation through the official repositories. As depicted in the following image, the ā€œapt-getā€ installation instruction is used for this purpose.

$ sudo apt-get install wget ca-certificates

Add the PostgreSQL GPG Key to Ubuntu

After installing the ā€œwgetā€ Ubuntu utility and the ca-certificates package, we move towards adding the Postgres GPG key to the Ubuntu system. As mentioned, we will utilize the ā€œwgetā€ utility with the ā€œā€“quietā€ and ā€œ-Oā€ options followed by the URL of the official Postgres repository site to add its GPG key to the Ubuntu system. The main part of this instruction is using the ā€œapt-keyā€ keyword followed by the ā€œaddā€ keyword. The output of this command displays ā€œOKā€ in return. The Postgres GPG key is successfully added to the system, and you can finally install it.

$ wget ā€“quiet -O ā€“ https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

After adding the Postgres GPG key to the Ubuntu system, it’s necessary to update the system packages and repositories once again so that the system can take the effect of the GPG key perfectly. Therefore, we execute the ā€œupdateā€ instruction again.

$ sudo apt-get update

Install PostgreSQL on Ubuntu

After perfectly meeting the pre-requirements of Ubuntu for the installation of Postgres, our system is now ready to install PostgreSQL. To install PostgreSQL on the Ubuntu system, we will utilize the everlasting ā€œapt-getā€ package within the ā€œinstallā€ command. The command uses the ā€œpostgresqlā€ package name followed by the additional package which is ā€œpostgresql-contribā€ which contributes an extra functionality to the PostgreSQL database.

$ sudo apt-get install postgresql postgresql-contrib

The installation process continues until it prompts for the affirmation. To proceed with the remaining installation process without interruption, tap on ā€œyā€ when prompted at the command shell. To top the execution right at the moment, tap on ā€œyā€.

The process of PostgreSQL installation creates a system link between PostgreSQL and system files. It also displays the username for a new database user that is generated automatically right after the installation of PostgreSQL. After displaying a certain information regarding PostgreSQL, it displays ā€œOKā€ and quits. The process displays that Postgres version 12 is successfully installed through the developer site.

If you donā€™t want to use the version of Postgres that is available at the developer site and you want to go with the available local version, you can also look for that. The ā€œapt showā€ instruction displays the latest version of Postgres, i.e. ā€œVersion 12ā€. You can install it via the previous installation command that you utilized to get it from the official site.

$ apt show postgresql

Now that PostgreSQL is successfully installed in the Ubuntu system, we start its service via the ā€œsystemctlā€ utility and look for its current status, i.e. running or not. The output of the status instruction displays that the PostgreSQL has been active.

$ sudo systemctl start postgresql.service

$ sudo systemctl status postgresql

Connect to PostgreSQL

Now, itā€™s time to use PostgreSQL and connect with its databases. The ā€œsuā€ instruction is used with the ā€œpostgresā€ keyword to open PostgreSQL. After successfully launching PostgreSQL, we need to launch the PostgreSQL command utility. For this, use the ā€œpsqlā€ instruction, and the command prompt will be launched.

$ sudo su ā€“ postgres
$ psql

We are currently logged in with the ā€œpostgresā€ database using the ā€œpostgresā€ user as per the ā€œ\conninfoā€ command that is used in the Postgres command prompt.

$ \conninfo

Create a Database

Letā€™s make a new database in the PostgreSQL database. For this, make sure to open Postgres via the ā€œsuā€ instruction and utilize the ā€œcreatedbā€ instruction with the database name to be created.

$ createdb test

You can log in from a specific database and quit it via the ā€œ\qā€ command. After returning to the PostgreSQL main menu, you can exit it via the ā€œexitā€ instruction.

$ \q

$ exit

Let’s say we added a new user, ā€œtestā€, in our Ubuntu system via the ā€œadduserā€ instruction and didnā€™t assign it with any sudo or administrative rights.

$ sudo adduser test

Switch the Database

Now, to open Postgres with the newly created ā€œtestā€ user, we need to switch the user in our command shell first using the ā€œ-Iā€ and ā€œ-uā€ option. Upon opening PostgreSQL with the ā€œtestā€ user, it doesnā€™t allow us because the ā€œtestā€ user is not a sudo user.

$ sudo -I -u test

$ sudo su ā€“ postgres

On the other hand, you can successfully login from PostgreSQL via any other user with the ā€œsudoā€ rights, i.e. xyz. Now, you can switch the database as well. Thus, we utilize the ā€œpsqlā€ instruction to switch to the newly created ā€œtestā€ database.

$ sudo -I -u xyz

$ sudo su ā€“ postgres

$ psql -d test

The following instruction displays the connection information:

$ \conninfo

The SQL command is the same as any conventional database, i.e. create table.

CREATE TABLE EMP ( Name VARCHAR (40) NOT NULL, Age VARCHAR (20) NOT NULL );

To display the list of tables in a PostgreSQL database, run the ā€œ\dā€ instruction as follows:

# \d

Conclusion

This guide demonstrates the importance and compatibility issues of Postgres during installation on Ubuntu. After this, we discussed a simple and brief way to install Postgres on Ubuntu like installing the tools from the PostgreSQL Apt Repository. Ultimately, we elaborated the simplest method to use PostgreSQL and the switch databases.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.