How To Set up PostgreSQL on Ubuntu 20.04

In this article, we want to teach you How To Set up PostgreSQL on Ubuntu 20.04.

PostgreSQL is an advanced, enterprise-class, and open-source relational database system. It supports both SQL (relational) and JSON (non-relational) querying.

Set up PostgreSQL on Ubuntu 20.04

Before you start to install PostgreSQL on Ubuntu 20.04, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article the Initial Server Setup with Ubuntu 20.04.

Now follow the steps below to set up PostgreSQL on your server.

Install PostgreSQL on Ubuntu 20.04

First, you need to update your local package index with the following command:

sudo apt update

Then, install PostgreSQL and its dependencies with the following command:

sudo apt install postgresql postgresql-contrib

When your installation is completed, let’s see how to use PostgreSQL.

How to Use PostgreSQL on Ubuntu 20.04

PostgreSQL uses a concept called roles to handle authentication and authorization.

During the PostgreSQL installation, Postgres is set up to use ident authentication, meaning that it associates Postgres roles with a matching Unix/Linux system account.

If a role exists within Postgres, a Unix/Linux username with the same name is able to sign in as that role.

One way is to switch over to the Postgres account on your server by running the following command:

sudo -i -u postgres

You can access the Postgres shell on Ubuntu 20.04 by running the following command:

psql

With this command, you will see that your shell prompt changes to:

postgres=#

To exit from the PostgreSQL shell, run the following command:

postgres=# \q

With this command, you will be back to the Postgres Linux command prompt.

[email protected]:~$

You can type exit to return to the regular system user.

[email protected]:~$ exit

Also, you can connect to your Postgres shell on Ubuntu 20.04 in another way by typing the following command:

sudo -u postgres psql

Create a new PostgreSQL Role

At this point, you can create a new Postgres role in two ways.

First, if you are logged in as a Postgres account, you can use the following command:

[email protected]:~$ createuser --interactive

Second, you can use sudo for each command without switching from your normal account on Ubuntu 20.04:

sudo -u postgres createuser --interactive

Both ways will be asked to enter your role name and the new role be a superuser or not.

Output
Enter name of role to add: orca
Shall the new role be a superuser? (y/n) y

Create a new PostgreSQL Database

The Postgres authentication system for any role used to log in, the role should have a database with the same name which it can access.

If you logged in as the Postgres account, run the following command to create the database with the name of the role you have created in the previous step:

[email protected]:~$ createdb orca

Or you can use sudo to create the database on Ubuntu 20.04:

sudo -u postgres createdb orca

Open the Postgres Shell with the new Role

At this point, you need to create a Linux user with the same name as your Postgres role and database. To do this, run the following command:

sudo adduser orca

Now you can connect to the Postgres database with the following commands:

sudo -i -u orca
psql

Or you can use the following command instead:

sudo -u orca psql

When you have logged in to your database, you can check your current connection information by running the following command:

orca=# \conninfo

In your output you will see:

Output
You are connected to database "orca" as user "orca" via socket in "/var/run/postgresql" at port "5432".

Conclusion

At this point, you learn to install some basic usage of PostgreSQL on Ubuntu 20.04.

Hope you enjoy it.

You may be interested in these articles:

Reset MySQL or MariaDB Root Password on Ubuntu 20.04.

How To Install NumPy on Ubuntu 20.04.

Install and Secure phpMyAdmin on Ubuntu 20.04.

How To Install Slack on Ubuntu 20.04.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular