How to install PostgreSQL on Ubuntu

The database management system (DBMS) PostgreSQL can be installed on Ubuntu 20.04 with just a few commands. Use this step-by-step guide, which takes you from installation all the way through to creating a database.

Requirements for an Ubuntu server with PostgreSQL

A simple database server with PostgreSQL doesn’t need a lot of computing power. Even a simple configuration will work well:

  • 1 GHz CPU
  • 2 GB
  • 2 GB memory

Depending on the type of data you want to save, you may need to expand your hard drive space.

If you want to create a PostgreSQL server with IONOS, you can use the cloud server RAM M to start. If you opt for this, you will have the option to use Ubuntu as your operating system for your server. The advantage of a cloud server is that if you end up needing more space, you can add more resources without it affecting operation.

Tip

Before you choose a server, you should take a look at what other options you have. IONOS offers three different server types:

Install PostgreSQL on Ubuntu

You can install PostgreSQL on Ubuntu 20.04 with just a few commands. To do that, you can use the Linux distribution terminal. You can open the console either from the program overview or the search field. Or you can simply use the keyboard shortcut [Ctrl] + [Alt] + [T].

Tip

You will need to learn some basic commands in order to use the terminal to its full potential. Find the most important in our article on Linux commands.

Step 1: Download and install PostgreSQL

As always, before you install Linux software you need to update the package manager. This ensures that the latest installation package is used.

sudo apt update
sudo apt upgrade
bash

Enter this simple command to download and install the PostgreSQL package.

sudo apt install postgresql
bash

You now just need to confirm it. This will then install the DBMS.

Step 2: Activate PostgreSQL

Once you’ve installed the DBMS, you can start PostgreSQL:

sudo systemctl start postgresql
bash

To find out whether PostgreSQL is working properly, you can check its status:

sudo systemctl status postgresql
bash

You should see that that service is ‘active’. To quit the status display, press the [Q] key.

Step 3: Use PostgreSQL

Once installed, you’ll find a generic account in PostgreSQL called ‘postgres’. You can activate it by using the following command:

sudo -i -u postgres
bash

Ubuntu will now act as if you were connected to the PostgreSQL account. From here you will be able to access the DBMS’ input prompt.

psql
bash

This part requires you to work with PostgreSQL. To display which connection is currently active, you can use the following command:

\conninfo
bash

To leave the area you can use the following command:

\q
bash

Step 4: Create databases

Alongside the role ‘postgres’ the system has automatically created a corresponding database. Your PostgreSQL server can, however, work with multiple databases at the same time. You can create a new database by using the following command, you can choose another name instead of ‘example’:

CREATE DATABASE example;
bash

Make sure you don’t forget the semicolon at the end when using this command.

To display which database you’ve created, you can use the following command:

\list
bash

The short form ‘\l’ (small L) will also work.

List of databases in PostgreSQL via Ubuntu 20.04
By using |list you can display all databases and will get more information on them.

If you want to work directly in a database, you can simply select it. The active database will be displayed in the input row of the PostgreSQL console. To change the database, you can enter the following command and enter the name of the database you want to use:

\c example
bash
Entries to create a new PostgreSQL database and switch to it with the given command in the Ubuntu 20.04 terminal
At the beginning of the row in the PostgreSQL environment you will recognise which databae you are currently working in – in this case: ‘postgres’ and ‘example’.

To delete a database, you can use the following code:

DROP DATABASE example;
bash

But be careful because you can’t go back if you change your mind. Use this command only when you have to and carefully consider if you really want to delete the database.

Tip

You can also use PostgreSQL on Windows. If you install PostgreSQL on Windows Server 2016, you will be able to take advantages of the benefits of the DBMS. Users who understand the Microsoft operating system better than the Linux one, should consider this option.

Was this article helpful?
Page top