# Install PostgreSQL in Ubuntu 20.04

URL: https://mepritam.dev/references/install-postgresql-in-ubuntu-2004/

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

![PostgreSQL Database Symbol](/images/references/install-postgresql-in-ubuntu-2004-1.webp)

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.

```bash
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.

```bash
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.

```bash
sudo -i -u postgres psql
```

![Ubuntu Server Shell / PostgreSQL CLI](/images/references/install-postgresql-in-ubuntu-2004-2.webp)

### Exit the prompt

Exit the prompt.

```bash
 \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.

```bash
sudo -u postgres createuser --interactive
```

### Create a new database

Create a new database instance for your application or user.

```bash
sudo -u postgres createdb sammy
```

### Add the Unix user

Create a corresponding system user on your Ubuntu host.

```bash
sudo adduser sammy
```

### Change the user password

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

```bash
sudo -u postgres psql

ALTER USER samy PASSWORD 'myPassword';

\q
```
