Howtodojo logo
  • Home 
  • About 
  • Certifications 
  • Sample Database 
  • Cheatsheet 
  • Glossary 
  • Blog 
  • Tags 
  1.   Blog
  1. Home
  2. Blog
  3. How To Install PostgreSQL 12 on Ubuntu 20.04

How To Install PostgreSQL 12 on Ubuntu 20.04

Share via
Howtodojo
Link copied to clipboard

Learn how to install PostgreSQL 12 on Ubuntu 20.04. PostgreSQL is an open-source object-relational database management system (ORDBMS)

On this page
Introduction   Prerequisites   Step 1 — Update Base Systems   Step 3 — Add PostgreSQL 12 repository   Step 4 — Install PostgreSQL 12   Verify PostgreSQL 12 Installation on Ubuntu 20.04   Connect to PostgreSQL using psql   PostgreSQL 12 References   Related Tutorials   Conclusion  
How To Install PostgreSQL 12 on Ubuntu 20.04

Introduction  

In this tutorial, we learn how to install PostgreSQL 12 on Ubuntu 20.04 (Focal Fossa).

PostgreSQL, or usually called Postgres, is an open-source object-relational database management system (ORDBMS) with an emphasis on extensibility and standards compliance.

PostgreSQL is ACID-compliant and transactional. It is developed by PostgreSQL Global Development Group (PGDG) that consists of many companies and individual contributors. PostgreSQL released under the terms of PostgreSQL license.

PostgreSQL 12 was released on 18 October 2018. Major enhancements in PostgreSQL 12 includes:

  • Increased Robustness and Performance for Partitioning
  • Transactions Supported in Stored Procedures
  • Enhanced Capabilities for Query Parallelism
  • Just-in-Time (JIT) Compilation for Expressions
  • General User Experience Improvements

Prerequisites  

This tutorial assume you already satisfy the following requirements:

  • A machine with fresh install of Ubuntu Server 20.04. It can be A virtual machine locally using Virtualbox, VMWare workstation or VMWare Fusion, KVM, Xen, or a virtual machine in the cloud like DigitalOcean or Linode.
  • A non-root user with sudo privileges or a root user access. If you’re usig root user remove sudo from each command on this tutorial.

Step 1 — Update Base Systems  

Since this is a fresh install of Ubuntu Server 20.04, before we install PostgreSQL 12, let’s update our system to the latest update.

sudo apt-get update
sudo apt-get -y upgrade
```bash

After we upgrade our base system, now it's time to install PostgreSQL 12.

Since Ubuntu 20.04 doesn't have PostgreSQL 12 in [its repository][3], we need to add oficial PostgreSQL repository so we can install PostgreSQL 12.

## Step 2 — Import Repository Signing Key {#h-step-2-import-repository-signing-key}

The PostgreSQL team is using GPG to sign downloadable packages from the PostgreSQL website.

We add PostgreSQL public GPG key so apt can verify that packages downloaded from PostgreSQL repository are not tampered or corrupt.

Add PostgreSQL GPG public key using the command below.

```bash
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

You should get OK output for command above.

If you got the different output, you need to fix the error first before continue to the next step.

Step 3 — Add PostgreSQL 12 repository  

After adding PostgreSQL release keys, we create a new repository configuration for PostgreSQL using the command below.

echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql-pgdg.list > /dev/null
```bash

Refresh apt metadata using the command below.

```bash

sudo apt-get update

Step 4 — Install PostgreSQL 12  

We’re ready to install PostgreSQL 12. Use the command below to install PostgreSQL 12 on Ubuntu 20.04.


sudo apt-get install postgresql-12

```bash

Alternative method to install PostgreSQL 12 on Ubuntu 20.04 is using apt command

```bash

sudo apt install postgresql-12

Only 4 steps needed to install PostgreSQL 12 on Ubuntu 20.04. We learn how to check and verify and managing PostgreSQL 12 installation in the next few sections.

Verify PostgreSQL 12 Installation on Ubuntu 20.04  

We can check PostgreSQL 12 packages that already installed using the command below.


dpkg -l | grep postgresql

```bash

## Check PostgreSQL 12 Listening Ports {#h-check-postgresql-12-listening-ports}

By default, PostgreSQL listens on port 5432. We can use netstat to check whether there is a process listening on port 5432.

```bash

sudo netstat -naptu | grep 5432

Another method is to check whether there is a process named postgres listening on a port.


sudo netstat -naptu | grep postgres

```bash

Alternative to `netstat`, We can use `ss` command below to check whether there is a process listen on PostgreSQL default port 5432.

```bash

sudo ss -atnp | grep 5432

We can also use ss to check whether there is a process named postgres listening on a port.


sudo ss -atp | grep postgres

```bash

The last output above shows there is also a connection to ephemeral UDP port from local. According to [this thread on PostgreSQL mailing lists][4], this is PostgreSQL stats collector that use UDP to send and receive stats data locally.

## Checking PostgreSQL Process {#h-checking-postgresql-process}

We can ue `ps` command to check what processes currently running with name contain postgres.

```bash

ps -aux | grep postgres

Check PostgreSQL 12 resource usage like CPU and memory usingtop command.


top -u postgres

```bash

## Managing PostgreSQL 12 Services on Ubuntu 20.04 {#h-managing-postgresql-12-services-on-ubuntu-20-04}

Check `postgres` service status using command below.

```bash

sudo service postgresql status

We can also use systemctl command to check PostgreSQL service status.


sudo systemctl status postgresql

```bash

Using both commands above you see `active (exited)` on the `Active:` line. This happens because systemd doesn't know the status of each PostgreSQL cluster running on the instance. One instance / installation of PostgreSQL can run mutliple clusters od PostgreSQL.

We can check, list of PostgreSQL clusters running on our machine using the command below.

```bash

pg_lsclusters

There is one cluster running on our machine. The cluster named main running PostgreSQL 12. To check this cluster service status, we can use the command below.


sudo service postgresql@12-main status

```bash

As an alternative to service, we can use systemctl to check this cluster status.

```bash

sudo systemctl status postgresql@12-main

Stop PostgreSQL service using the command below.


sudo systemctl stop postgresql

```bash

Start PostgreSQL service using the command below.

```bash

sudo systemctl start postgresql

Restart PostgreSQL service using the command below.


sudo systemctl restart postgresql

```bash

Reload PostgreSQL service using the command below.

```bash

sudo systemctl reload postgresql

Connect to PostgreSQL using psql  

Now let’s connect to the PostgreSQL 12 server using psql client application.

By default, postgres create system user named postgres. There is also role in postgresql database named postgres wth superuser permission.

Switch to postgresql user.


sudo su - postgres

```bash

After switching to postgres user, we can connect using `psql` command.

To list all databases in PostgreSQL we can use `\l` command.

To list all users in PostgreSQL we can use `\du` command.

To exit from `psql` we can use `\q` command.

To exit from `postgres` user type `exit` or `CTRL+D`

## Change postgres User Password {#h-change-postgres-user-password}

Now we're on our own linux user. Let's try to login to `localhost` or `127.0.0.1`.

```bash

$ psql -U postgres -W -h 127.0.0.1
Password for user postgres: 

It prompts for a password. Since we haven’t set any password, let’s leave it empty. We get the error message below.


psql: fe_sendauth: no password supplied

```bash

Since we cannot login without providing password, we set new password for `postgres` user. Change user to `postgres` user like shown above and use psql to connect to PostreSQL.

After logging in, type `\password`. Enter new password and confirmation password for user `postgres`.

When we login again to `localhost` or `127.0.0.1` and provide a password, we can log in successfully.

## Uninstall PostgreSQL 12 from Ubuntu 20.04 {#h-uninstall-postgresql-12-from-ubuntu-20-04}

In this section, we learn how to uninstall PostgreSQL 12 and its dependencies on Ubuntu 20.04 completely.

Please be careful when running steps in this section since this will stop your PostgreSQL database server.

Use the command below to uninstall all PostgreSQL 12 packages, both server, and client.

```bash

sudo apt-get remove postgresql-12 \
    postgresql-client-12 \
    postgresql-client-common \
    postgresql-common

Remove the PostgreSQL data directory using the command below.


rm -rf /var/lib/postgresql/

```bash

Remove PostgreSQL 12 configuration directories using the command below.

```bash

rm -rf /etc/postgresql
rm -rf /etc/postgresql-common

PostgreSQL 12 References  

You can follow the links below to learn more about PostgreSQL 12 from PostgreSQL official documentation.

  • PostgreSQL 12 release notes
  • PostgreSQL 12 release announcement
  • PostgreSQL 12 documentation

Related Tutorials  

  • Install PostgreSQL 13 on Ubuntu 20.04
  • Install PostgreSQL 11 on Ubuntu 20.04
  • Install PostgreSQL 10 on Ubuntu 20.04
  • Install PostgreSQL 9.6 on Ubuntu 20.04

Conclusion  

In this tutorial, we learn how to install PostgreSQL 12 on ubuntu 20.04. We also learn managing PostgreSQL services, using tools like netstat, ss, ps, and top to check PostgreSQL service.

After that, we learn how to connect to PostgreSQL 12 server using psql client, changing postgres superuser password, and in the end, we learn how to uninstall PostgreSQL 12. I hope this is useful for initiating your journey to PostgreSQL 12.

 How To Install PostgreSQL 13 on Ubuntu 20.04
How To Install PostgreSQL 11 on Ubuntu 20.04 
On this page:
Introduction   Prerequisites   Step 1 — Update Base Systems   Step 3 — Add PostgreSQL 12 repository   Step 4 — Install PostgreSQL 12   Verify PostgreSQL 12 Installation on Ubuntu 20.04   Connect to PostgreSQL using psql   PostgreSQL 12 References   Related Tutorials   Conclusion  
Follow me

We publish tutorials, tips and tricks about Linux, open source, cloud computing, and infrastructure

     
Copyright © 2012 - 2025 howtodojo.com. |
Howtodojo
Code copied to clipboard