---
title: "Install PostgreSQL on a Ubuntu for Security Configurations"
description: "Learn the different ways to install PostgreSQL on a Ubuntu and perform initial configuration to setup a working RDBMS."
canonical: "https://adamtheautomator.com/install-postgresql-on-a-ubuntu/"
---

# Install PostgreSQL on a Ubuntu for Security Configurations

> Learn the different ways to install PostgreSQL on a Ubuntu and perform initial configuration to setup a working RDBMS.

Source: https://adamtheautomator.com/install-postgresql-on-a-ubuntu/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Install PostgreSQL on a Ubuntu Server for Security Configurations](https://adamtheautomator.com/wp-content/uploads/2022/03/Learn-How-to-Install-PostgreSQL-on-a-Ubuntu-Linux-Server.jpg)

# Install PostgreSQL on a Ubuntu Server for Security Configurations

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)24 March 20225 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Command Line](/tag/command-line/)[Databases](/tag/databases/)[Linux](/tag/linux/)[PostgreSQL](/tag/postgresql/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Install PostgreSQL on a Ubuntu](#install-postgresql-on-a-ubuntu)
*   [Method 1: Install PostgreSQL on a Ubuntu (from Ubuntu Repository)](#method-1-install-postgresql-on-a-ubuntu-from-ubuntu-repository)
*   [Method 2: Install PostgreSQL on Ubuntu (from the Official Repository)](#method-2-install-postgresql-on-ubuntu-from-the-official-repository)
*   [Starting and Verifying the PostgreSQL Service](#starting-and-verifying-the-postgresql-service)
*   [Securing the Default PostgreSQL User and Enabling Password Encryption](#securing-the-default-postgresql-user-and-enabling-password-encryption)
*   [Creating and Deleting a PostgreSQL Database](#creating-and-deleting-a-postgresql-database)
*   [Conclusion](#conclusion)

Are you planning to install PostgreSQL on a Ubuntu server and are not sure where to start? Look no further as you’re in luck. In this tutorial, you’ll learn the different ways to install a PostgreSQL Ubuntu server and perform basic security configurations to help you get started.

Keep reading, and you’ll be running a PostgreSQL server in no time!

## **Prerequisites**

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following.

*   An Ubuntu server. This tutorial uses [Ubuntu 20.04](https://adamtheautomator.com/install-ubuntu/).

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   A non-root user account with **[sudo privileges](https://www.liquidweb.com/kb/how-to-set-up-and-manage-sudo-permissions/).**

## Install PostgreSQL on a Ubuntu

Starting on a fresh Ubuntu server installation, you can install PostgreSQL in two ways. Which way you choose depends on whether you favor convenience (fewer steps) or making sure you have the latest version.

### Method 1: Install PostgreSQL on a Ubuntu (from Ubuntu Repository)

By default, Ubuntu’s repositories contain Postgres packages, which means you can install PostgreSQL Ubuntu packages using the [**Advanced Package Tool (APT)**](https://ubuntu.com/server/docs/package-management).

> _The Postgres packages in the Ubuntu repositories might not always be the latest versions. If you prefer to install the latest version instead, skip to Method 2._

1\. First, open a terminal window on your computer and SSH into your Ubuntu server.

Related:[Getting Started using SSH with PowerShell](https://adamtheautomator.com/powershell-ssh/)

2\. Next, run the below command to update the package list on your Ubuntu server.

```bash
sudo apt update -y
```

3\. Install the PostgreSQL packages by executing the below command. This command will also install the `postgresql-contrib` (additional Postgres features) and `postgresql-client` (Postgres CLI).

```bash
sudo apt install -y postgresql postgresql-contrib postgresql-client
```

4\. After the installation, verify the Postgres installation status by running the below command.

```bash
sudo dpkg --status postgresql
```

As you can see below, the latest version installed from the Ubuntu repository as of this writing is version 12.

![Verifying the PostgreSQL installation](https://adamtheautomator.com/wp-content/uploads/2022/03/image-339.png)

Verifying the PostgreSQL installation

Related:[Learning Ubuntu Apt Get Through Examples](https://adamtheautomator.com/ubuntu-apt-get/)

### Method 2: Install PostgreSQL on Ubuntu (from the Official Repository)

Installing PostgreSQL from the official repository ensures that you’re getting the latest version. But, this installation method requires more steps when compared to installing with APT.

1\. First, open a terminal window on your computer and SSH into your Ubuntu server.

2\. Run the following command to create a file named _`/etc/apt/sources.list.d/pgdg.list`_. This file contains PostgreSQL’s official repository’s address specific to your Ubuntu server version.

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

3\. Next, run the below command add the PostgreSQL [GPG key](https://en.wikipedia.org/wiki/GNU_Privacy_Guard) to your package manager. APT will use this key to verify the authenticity of the packages in the repository.

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

You will see an OK response if adding the key was successful.

![Adding the official repository GPG key](https://adamtheautomator.com/wp-content/uploads/2022/03/image-340.png)

Adding the official repository GPG key

4\. Now, update the package lists, so that `apt` knows where to find the official PostgreSQL packages.

```bash
sudo apt update -y
```

As you can see below, the command now includes PostgreSQL’s official repository’s address when checking for packages.

![Updating the package list](https://adamtheautomator.com/wp-content/uploads/2022/03/image-341.png)

Updating the package list

5\. Install PostgreSQL on Ubuntu by running the following command. Wait for the installation to complete.

```bash
sudo apt install -y postgresql postgresql-contrib postgresql-client
```

6\. Confirm the PostgreSQL version you installed from the official repository by running the below command.

```bash
sudo dpkg --status postgresql
```

As you can see below, the version from the official repository is version 14 as of this writing.

![Verifying the PostgreSQL installation from the official repository](https://adamtheautomator.com/wp-content/uploads/2022/03/image-342.png)

Verifying the PostgreSQL installation from the official repository

### Starting and Verifying the PostgreSQL Service

Once the installation completes, run the below `systemctl` command to start the PostgreSQL service.

```bash
sudo systemctl start postgresql.service
```

Finally, verify the PostgreSQL status by running the below command.

```bash
sudo systemctl status postgresql.service
```

The service status should be `active`, as shown in the image below.

![Checking the Postgres service status](https://adamtheautomator.com/wp-content/uploads/2022/03/image-343.png)

Checking the Postgres service status

Related:[Controlling Systemd services with Ubuntu systemctl](https://adamtheautomator.com/ubuntu-systemctl/)

## Securing the Default PostgreSQL User and Enabling Password Encryption

The PostgreSQL installation creates a default user account called `postgres` that has complete administrative access to PostgreSQL. But, this special account does not have a password. Any user who can log in to the Ubuntu server can access PostgreSQL as the `postgres` user without providing a password.

Considering that the `postgres` account is unprotected, the first action you should take is to set a new password. To do so, proceed as below.

1\. Log in to the PostgreSQL shell (`psql`) by running the below command.

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

As you can see below, any user who can log in to the Ubuntu server can also access the PostgreSQL database without providing a password.

![Accessing the PostgreSQL prompt](https://adamtheautomator.com/wp-content/uploads/2022/03/image-344.png)

Accessing the PostgreSQL prompt

2\. Run the following command at the prompt to initiate a password change. Provide a strong password of your choice and press Enter.

```bash
\password
```

![Setting the postgres account password](https://adamtheautomator.com/wp-content/uploads/2022/03/image-345.png)

Setting the postgres account password

3\. After setting the new password, type `\q` and press Enter to exit the `psql` shell.

4\. By default, PostgreSQL sends passwords in plain text, which can be a security risk. To rectify this potential attack vector, change the PostgreSQL password **[authentication method](https://www.postgresql.org/docs/9.1/auth-methods.html)** to a secured one, such as `md5`.

Open the _/etc/postgresql/12/main/pg\_hba.conf_ file in nano for editing. This file is the main configuration file for PostgreSQL’s authentication methods.

```bash
sudo nano /etc/postgresql/12/main/pg_hba.conf
```

5\. Find the line that reads as the below.

```powershell
# Database administrative login by Unix domain socket
local   all             postgres                                peer
```

Once you’ve located the line, replace the word peer with md5. After editing the line, your file should now look like the below image. Lastly, save the file and exit the editor.

![Changing the authentication method](https://adamtheautomator.com/wp-content/uploads/2022/03/image-346.png)

Changing the authentication method

> _The md5 authentication method uses a message-digest algorithm to encrypt the password before sending it to the server._

6\. Restart the PostgreSQL server for the changes to take effect.

```bash
sudo systemctl restart postgresql
```

7\. Reconnect to the PostgreSQL server as the `postgres` user.

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

Enter the password at the prompt and press Enter.

![Accessing the PostgreSQL shell with password authentication](https://adamtheautomator.com/wp-content/uploads/2022/03/image-347.png)

Accessing the PostgreSQL shell with password authentication

## Creating and Deleting a PostgreSQL Database

Now that you have installed and configured your PostgreSQL server, you can now start creating a database. After all, what’s an RDBMS without a database? Follow the below steps to test your server by creating and deleting a database.

1\. Log in to the PostgreSQL shell (`psql`) if you haven’t already.

2\. Run the following command to create a new database named `ata_db`.

> _Refer to the_ [_PostgreSQL documentation_](https://www.postgresql.org/docs/) _to learn more about the database and the different data types._

```sql
CREATE DATABASE ata_db;
```

3\. Next, confirm that you’ve successfully created the database by listing all available databases on the server.

```bash
\l
```

As you can see below, the new database named ata\_db you created in the previous step now exists in the server.

![Listing all of the databases on your PostgreSQL server](https://adamtheautomator.com/wp-content/uploads/2022/03/image-348.png)

Listing all of the databases on your PostgreSQL server

4\. Run the following command to connect to the `ata_db` database.

```bash
\c ata_db;
```

The below image confirms that connecting to the new database was successful. You can see a status message saying to which database you connected and with which user account.

![Connecting to the database](https://adamtheautomator.com/wp-content/uploads/2022/03/image-349.png)

Connecting to the database

You can now [_create tables_](https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-create-table/) and perform the _create_, _read_, _update_, and _delete_ (CRUD) actions on the database.

5\. If eventually, you do not need the database anymore and need to delete it, run the following commands in `psql` to delete the database.

The first line disconnects from the ata\_db database by switching the acting connection to the default postgres database. You cannot delete a database if it is open.

The second line deletes the database.

```sql
\c postgres
DROP DATABASE ata_db;
```

![Drop Database](https://adamtheautomator.com/wp-content/uploads/2022/03/image-350.png)

Drop Database

6\. Finally, list all of the databases on your PostgreSQL server again. You will see that the `ata_db` database no longer exists, as shown below.

```sql
\l
```

You will see that the ata\_db database no longer exists, as shown below.

![Confirming the database no longer exists](https://adamtheautomator.com/wp-content/uploads/2022/03/image-351.png)

Confirming the database no longer exists

## Conclusion

In this tutorial, you have learned how to install PostgreSQL on a Ubuntu server and configure basic security settings. You have also learned how to create, connect, and delete a new database on your PostgreSQL server.

Keep in mind that what you’ve learned here is just the basics. There are more you can do with PostgreSQL, such as configuring [**SSL certificate-based authentication**](https://www.postgresql.org/docs/14/ssl-tcp.html), creating new database users, and setting up replication.

With this new knowledge, you are now in a better position to use PostgreSQL in your [**applications**](https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-django-application-on-ubuntu-14-04).

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Finstall-postgresql-on-a-ubuntu%2F&text=Install%20PostgreSQL%20on%20a%20Ubuntu%20Server%20for%20Security%20Configurations)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Finstall-postgresql-on-a-ubuntu%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Finstall-postgresql-on-a-ubuntu%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Practical-Linux-Unix-Tee-Commands-for-the-Linux-Admin.jpg)

### [Master Unix tee Commands for Real-World Linux Admin Tasks](/unix-tee/)

Simplify Linux output management and streamline your workflow with the “Unix tee” command. This tutorial guides you through its practical applications.

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Useful-Linux-Dig-Examples-for-the-Network-Admin.jpg)

### [Linux Dig for Network Admins: Practical Guide with Examples](/linux-dig/)

Become a network troubleshooting expert! This comprehensive Linux Dig guide empowers you with essential DNS query commands for immediate results.

![](https://adamtheautomator.com/wp-content/uploads/2021/12/install-phpmyadmin.jpg)

### [Step-by-Step Tutorial to Install phpMyAdmin Securely on Linux](/install-phpmyadmin/)

Discover how to securely install phpMyAdmin on Linux with our concise guide. Learn essential steps for a safe and efficient setup, tailored for Linux users.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
