How to install and use PostgreSQL on Ubuntu 20.04

Sign up for a Bitcoin VPS today and get started with PostgreSQL

In today's guide we're going to focus on how to install PostgreSQL on Ubuntu 20.04 as quickly as possible. For those unfamiliar, PostgreSQL, or Postgres, is a free, open-source database management system with high extensibility and compliance.

We won't get too bogged down in the details, but you should know that you'll want an Ubuntu 20.04 server or local machine for this tutorial, with a non-root user that has sudo permissions. Other than that, we'll cover everything below:

How to install PostgreSQL on Ubuntu 20.04

To install PostgreSQL, you can simply use the package manager as you would any other app. However, it's worth noting that you may want to add the -contrib package to your install to gain some additional functionality. Run the following commands:

sudo apt update
sudo apt install postgresql postgresql-contrib

Wait for it to download and install and press y if prompted. It's really that simple folks.

How to use PostgreSQL

Using PostgreSQL is also fairly pain free, but it does differ slightly from, say, MySQL. Postgres uses a "role" system to define its authentication. It associates itself your system account so that a system user with the same name can sign in as the database role with their regular credentials.

When you install postgres, it creates a new system user called postgres which is essentially the default role. The easiest way to access postgres, then, is to log in to this account and start it:

sudo -u postgres psql

You can quit again with \qif you'd like.su

Of course, you'll probably want to create a new role at some point. Thankfully, this isn't too hard either. Simply type:

sudo -u postgres createuser --interactive

The output will ask you to choose a name and then decide whether it should be a superuser. Choose whatever suits your preferences.

Postgres will try to connect to a database with the same name as the role you just made, in our case bitlaunch. You can create a database to fit the role with:

sudo -u postgres createdb bitlaunch

To log in to postgres with the user, you'll want an Ubuntu account that matches the username. You can do so with:

sudo adduser bitlaunch

To login to posgres with that user, type:

sudo -u bitlaunch psql

If you'd like to connect to a database other than the role's default, you can specify it with:

psql -d postgres

Finally, to check which database you're connected to, run \conninfo.