MilesWeb
India’s #1 Web Hosting Company Since 2012
Award-winning Web Hosting Company
Everything that you need to build, host and manage a website is available at just 60/mo

Discover the Steps to Install PostgreSQL on Ubuntu 18.04

Relational Databases are an important part of different applications and websites. These databases help to continue data and offer a logical structuring to it. The data can be queried and accessed at any time and this is one of the biggest advantages of it.

If you want to find an alternative for MySQL, you will find multiple relational databases and one of them is the PostgreSQL. It is very popular and used by several applications.

Being an open source, PostgreSQL is compatible with most OS, including Unix based systems.

This article offers the steps to install PostgreSQL on Ubuntu 18.04. You will be using Ubuntu’s default repository with PostgreSQL packages.

Installation of PostgreSQL on Ubuntu is simple. But, in the post-installation, you will need to create an appropriate database and set user roles for everything to work well.

Steps to Install PostgreSQL on Ubuntu

Prior to starting these steps you will need to access your VPS through SSH.

At first you will use apt to update the local package index:

Then, install PostgreSQL and the contrib package that offers additional features using the command below:

With this command the PostgreSQL will get installed. For using it, do some basic configuration.

Steps to Verify PostgreSQL on Ubuntu

After completing the installation of Postgres on Ubuntu, the services will start automatically. Now, verify the installation by connecting to the PostgreSQL database with psql.

The psql is a command line utility that enables you to interact with the PostgreSQL server. This will print the server version:

You can use the psql to log in to PostgreSQL as a postgre user. Use the below command for this:

Type the below command to use PostgreSQL:

With this you will get access to PostgreSQL instance. For exiting, you can use:

Role and User Creation

As done above for creating a role, log in to the postgres account. After being at the Postgres console, you can create a new role with the below command:

You will be asked to name the role and if it has superuser permission. Similarly, it is possible to create a new user with the below command:

Otherwise, from the normal command prompt you can use the below command:

Again you will be asked for a user name. You can use the createuser command with multiple options which can be checked by using the createuser.

In this way, you have created a new user. Then, you will learn to create a Database.

Database Creation

With PostgreSQL, it is assumed that the role name should be the same as the database name. That indicates if you created a user named “testPostgres” earlier, then the role will try to access a database named “testPostgres”.

You can create a database from the Postgres console with the below command:

If you are working without the Postgres SQL, you can use a command as displayed below:

Steps to Validate the New Role

First, you need to create a Linux user with the same names as the Postgres DB and the role. You can create it using a non-root account with sudo access. For creating the new user in Linux, use a command similar to the one shown below:

Then replace the name testPostgres with a proper name.

After the account is created, switch to it and connect to the database using the below command:

For switching to another database, mention the database name with the below command:

After logging in, check the current DB connection with the below command:

Postgres Basic DML Operation

The similar syntax is used by Postgres to other databases for functionalities such as creation, deletion and updating tables. A new table can be created and you can describe its definition using the below command:

To view the table without a sequence, use the below command:

Wrapping Up

So, you might have not got a basic understanding of Postgres. You have learned to install Postgres on Ubuntu 18.04 and understood the basic setup of it to create a user, roles, and database.

This tutorial will help you to get started with Postgres on Ubuntu.

Pallavi Godse

Pallavi is a Digital Marketing Executive at MilesWeb and has an experience of over 4 years in content development. She is interested in writing engaging content on business, technology, web hosting and other topics related to information technology.

Leave a Reply

Your email address will not be published. Required fields are marked *