How to install PostgreSQL using Terminal on Ubuntu Linux

Nnamani Ezinne Martina
Dev Genius
Published in
3 min readAug 7, 2023

--

PostgreSQL icon
Credit: freepngimg.com

Introduction:

PostgreSQL, also known as Postgres, is a powerful and open-source object-relational database management system (ORDBMS). It offers advanced features, high scalability, and robust data integrity, making it a popular choice for various applications.

In this article, we will walk you through installing PostgreSQL on a Linux Ubuntu system. Whether you are a developer, a system administrator, or an enthusiast, this guide will help you get PostgreSQL up and running smoothly.

First, we make sure that the system is updated to the latest packages by running the following commands in the terminal:

sudo apt update
sudo apt upgrade

Step 1: Installing PostgreSQL:

To install PostgreSQL, we will use the apt package manager, which is the default package manager for Ubuntu. Open the terminal and execute the following command:


sudo apt install postgresql postgresql-contrib

This command will download and install the PostgreSQL packages along with the necessary dependencies.

It will also add a default PostgreSQL user — postgres.

Step 2: Verifying the Installation:

Once the installation is complete, PostgreSQL will automatically start running as a background service.
In the Ubuntu terminal, log in as a Postgres user with the following command:

sudo -i -u postgres

Running the command psql when logged in as a Postgres user will show the version of Postgres installed. This command will enable you to log into the PostgreSQL prompt, which allows you to interact with the database management system.

psql

To get more help from the command, you can use the --help command.


psql --help

Step 3: Creating a New PostgreSQL user role (Optional)

This is an optional step. The default Postgres user created when PostgreSQL was installed can be used without issues. You also have the flexibility of creating an additional user.

3a. In the terminal, log in as a Postgres user. Then run the command below:

//Login as a Postgres user

sudo -i -u postgres


//Create a new user role
createuser --interactive

Remember the name of the role added. We will need to add the database with the same name. Add the name of the role and accept the user to be a superuser. For the tutorial purpose, we will use the name o the user as poweruser.

We will create a database for it and also a new user using the same name — poweruser.

3b. Create a database for the user

createdb poweruser

3c. Create a new Linux user that matches that of the database created in 3b above.

sudo adduser poweruser

3d. Open a new terminal and log in using the newly created user

sudo -i -u poweruser

3e. Test the installation on the Postgres user. Type psql after logging in as a user to connect to the database.

psql

Remember that step 3 is optional. One can easily log in using the default Postgres user.

Congratulations! You have successfully installed PostgreSQL on your Ubuntu Linux system.

To expand your knowledge of commands for creating and modifying databases and tables, you can explore the article.

You can now utilize the power and flexibility of PostgreSQL to build robust and scalable database-driven applications.

Happy Coding =)

--

--