The database man­age­ment system (DBMS) Post­greSQL can be installed on Ubuntu 20.04 with just a few commands. Use this step-by-step guide, which takes you from in­stal­la­tion all the way through to creating a database.

Re­quire­ments for an Ubuntu server with Post­greSQL

A simple database server with Post­greSQL doesn’t need a lot of computing power. Even a simple con­fig­u­ra­tion 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.

Tip

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

  • Cloud server: The best scal­a­bil­i­ty with minute by minute billing
  • Dedicated server: The best per­for­mance with hardware reserved just for you
  • vServer (VPS): Complete dig­i­tal­iza­tion for more freedom.

Install Post­greSQL on Ubuntu

You can install Post­greSQL on Ubuntu 20.04 with just a few commands. To do that, you can use the Linux dis­tri­b­u­tion 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 Post­greSQL

As always, before you install Linux software you need to update the package manager. This ensures that the latest in­stal­la­tion package is used.

sudo apt update
sudo apt upgrade

Enter this simple command to download and install the Post­greSQL package.

sudo apt install postgresql

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

Step 2: Activate Post­greSQL

Once you’ve installed the DBMS, you can start Post­greSQL:

sudo systemctl start postgresql

To find out whether Post­greSQL is working properly, you can check its status:

sudo systemctl status postgresql

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

Step 3: Use Post­greSQL

Once installed, you’ll find a generic account in Post­greSQL called “postgres”. You can activate it by using the following command:

sudo -i -u postgres

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

psql

This part requires you to work with Post­greSQL. To display which con­nec­tion is currently active, you can use the following command:

\conninfo

To leave the area you can use the following command:

\q

Step 4: Create databases

Alongside the role “postgres” the system has au­to­mat­i­cal­ly created a cor­re­spond­ing database. Your Post­greSQL 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;

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

The short form “\l” (small L) will also work.

Image: List of databases in PostgreSQL via Ubuntu 20.04
By using |list you can display all databases and will get more in­for­ma­tion 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 Post­greSQL console. To change the database, you can enter the following command and enter the name of the database you want to use:

\c example
Image: 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 Post­greSQL en­vi­ron­ment you will recognize 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;

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 Post­greSQL on Windows. If you install Post­greSQL on Windows Server 2016, you will be able to take ad­van­tages of the benefits of the DBMS. Users who un­der­stand the Microsoft operating system better than the Linux one, should consider this option.

Go to Main Menu