How to Install PostgreSQL and pgAdmin4 on Ubuntu 20.04

If you’re looking for a quick and easy way to install PostgreSQL and pgAdmin4 in Ubuntu 20.04, this post is for you! We’ll walk you through the whole process step-by-step, so it’s easy for anyone to do.

Postgres is an open-source, powerful relational database system that lets users create, edit, share, and store data quickly and flexibly. It supports all of SQL:2003 standards, which makes it great for storing any type of data. pgAdmin4 is a graphical user interface tool that allows users to manage their PostgreSQL databases from one place without having to use complicated command-line instructions or terminal commands like many other similar tools on the market offer.

Prerequisite

We’ll need to make sure the following prerequisites are installed before we can install PostgreSQL and pgAdmin4:

  • Ubuntu 20.04 is installed on the server.
  • Root privileges are configured on the server.
  • Basic understanding of command-line tools.

Updating the Server

To make sure that we have all the latest updates installed on the server, run the following wget command in the terminal:

sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt install wget curl ca-certificates gnupg2

This command will update our system packages, and upgrade any software that needs to be updated on the server. The third command installs some required packages like the command line HTTP clients curl and wget.

Installing PostgreSQL

Now that we have Ubuntu 20.04 server updated with the latest updates, let’s install PostgreSQL by running the following command in the terminal:

sudo apt install postgresql postgresql-contrib -y

This command will download and deploy PostgreSQL along with other useful packages that will get installed on the server.

Once the installation finishes. you can check the status of the installation by running the following command.

sudo systemctl status postgresql

Or

sudo systemctl is-active postgresql

As you can see in the output below, the PostgreSQL server daemon is up and running on the server.

Install PostgreSQL on Ubuntu

Creating a New Role in PostgreSQL

PostgreSQL provides a default account called postgre. We will use this account to connect to PostgreSQL.

sudo su - postgres

You will be logged into the PostgreSQL terminal and provided access to all PostgreSQL commands. This is where you can type all your queries.

Use the createuser statement to create a new user that will be used to access the PostgreSQL server. Let’s create a new account called linuxways. All of the following steps must be performed while we’re still logged into PostgreSQL as a postgres user.

createuser --interactive

Create postgres user

Creating a New Database

A role in PostgreSQL can be associated with one or more databases. You can create a new database by using the createdb command. Let’s create a new database called linuxways for our role linuxways.

createdb linuxways

Opening a Postgres Prompt with the New Role and the New Database

Now that we have a new role and a new database, let’s connect to them. It’s required to create a Linux user with the same name as your Postgres role and database.

sudo adduser linuxways

Once this new user is created, you can use it to connect to the PostgreSQL server by running the following command.

sudo -i -u linuxways
psql

This command will open a new psql prompt that is connected with our role linuxways and associated database called linuxways. You can issue any PostgreSQL command to the server from here. Let’s check your current connection.

\conninfo

This command will show all the connections your role, and user linuxways is currently connected with. As you can see in the screenshot below, we’re connected to the PostgreSQL server with our role linuxways and its associated database called linuxways.

pgsql

Installing pgAdmin4

pgAdmin4 is not available in Ubuntu 20.04 default repository. So we’re going to add the pgAdmin4 repository in order to install it with the apt-get command.

First, download and add the GPG key for the pgAdmin4 repository, which we’re going to add:

curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | apt-key add

Download pgadmin4

Next, add the pgAdmin4 repository to your system:

sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'

Add pgadmin4 repository

Finally, update the package list and install pgAdmin4.

sudo apt-get update
sudo apt install pgadmin4

The command above will install a large number of packages that are needed by pgAdmin4.

Install pgadmin4

After installing pgAdmin4, you have to run the web setup script, called web_setup.sh, which is shipped with the pgAdmin4 binary package.

You need to ensure that Apache2 is set up for it and that the pgAdmin application is mounted as the WSGI module. This setup will help you see your website and also let you do things like: run queries, create and delete databases and tables. You will be asked to create a login email and password during the process.

sudo /usr/pgadmin4/bin/setup-web.sh

Setup pgadmin4

Accessing pgAdmin4 Web UI

Now that pgAdmin4 is installed, you can access pgAdmin4 with a browser, by going to the following address: http://server_ip/pgadmin4, where server_ip is your server’s IP address or domain name.

You will see the postgreSQL pgAdmin4 login prompt, which requires an administrator username and password to be entered. Type in your login email and password created during the installation process, click on the Login button.

pgAdmin 4 web UI

Once you successfully log in, you will see the pgAdmin4 home page. From there, you can create and manage databases, users, roles, tables, etc. The interface is self-explanatory, so we won’t go over it here.

pgAdmin 4 Dashboard

And there you have it! You have successfully installed pgAdmin4 on Ubuntu 20.04 LTS server. For more information on how to install and use pgAdmin4, you can refer to the official documentation: https://www.postgresql.org/docs/

Conclusion

We have seen how to install pgAdmin4 on Ubuntu 20.04 LTS server. We hope you find this tutorial useful. If you have any questions or feedback, feel free to leave a comment below.