All tech notes

Install PostgreSQL in Ubuntu 20.04

Step-by-step developer guide to installing PostgreSQL on Ubuntu 20.04 LTS, setting up a database user, and configuring secure remote access connections.

Install PostgreSQL in Ubuntu 20.04

Published June 10, 2021

Install PostgreSQL in Ubuntu 20.04

PostgreSQL Database Symbol

This guide shows you how to install and configure PostgreSQL on Ubuntu 20.04. Let's get started.

Update the system

Updating your system is essential. First, fetch the latest package lists from the repositories so that the package manager knows about every new patch and security release.

sudo apt-get update

Install PostgreSQL

Run the install command. This installs PostgreSQL along with the postgresql-contrib package, which adds extra tools and features.

sudo apt install postgresql postgresql-contrib

Open PostgreSQL prompt

Let's test it. Switch to the default postgres user and start the interactive command-line interface, psql, to verify that the installation succeeded.

sudo -i -u postgres psql

Ubuntu Server Shell / PostgreSQL CLI

Exit the prompt

Exit the prompt.

 \q

Create a new user

You can add database roles. Run the interactive utility, specify the role name, and decide if it needs superuser status.

sudo -u postgres createuser --interactive

Create a new database

Create a new database instance for your application or user.

sudo -u postgres createdb sammy

Add the Unix user

Create a corresponding system user on your Ubuntu host.

sudo adduser sammy

Change the user password

Log back into PostgreSQL and change the password for your database user.

sudo -u postgres psql
 
ALTER USER samy PASSWORD 'myPassword';
 
\q

Related work

More tech notes