In this article, we will guide you through the process of installing and configuring PostgreSQL on Ubuntu 22.04. PostgreSQL is a powerful, open-source relational database system widely used for applications such as web development, data warehousing, and analytics. Whether you’re setting up a simple database or a complex data solution, PostgreSQL provides the flexibility and performance you need.


Prepare Your Instance

Step 1: Initiate Instance Creation

Begin by creating a new virtual machine instance on your chosen hosting provider’s platform.

Shape.host Resources dashboard showing the Create menu and available instance templates.

Step 2: Choose Your Data Center Location

Select the appropriate data center location that best fits your needs.

Shape.host instance form with Amsterdam selected as the data center location.

Step 3: Select a Hosting Plan

Pick a hosting plan suitable for your requirements, ensuring Ubuntu 22.04 is selected as the operating system.

Shape.host instance form with the $22 monthly plan and Ubuntu image selected.

Step 4: Set Up Authentication

Configure authentication using SSH keys or a password, then finalize the instance creation process.

Shape.host instance form showing authentication, hostname and instance creation options.

Step 5: Connect to Your Instance

Use the following command to connect to your server via SSH:

ssh username@your-server-ip-address

Replace username and your-server-ip-address with your actual details.


With your instance prepared, proceed to the following steps.

Step 1: Update and Upgrade the System

First, update your package list and upgrade your system packages to ensure everything is up to date:

apt update -y
apt upgrade -y
Terminal showing APT package management commands and output.

Step 2: Install Necessary Dependencies

Install wget, curl, and gnupg2 to manage the PostgreSQL repository:

apt install wget curl gnupg2 -y
Terminal showing APT package management commands and output.

Step 3: Add PostgreSQL APT Repository and Import Signing Key

Add the PostgreSQL APT repository to your sources list and import the signing key in one line:

sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' && wget -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc | tee /etc/apt/trusted.gpg.d/pgdg.asc &>/dev/null
Terminal showing sh commands or output during Add PostgreSQL APT Repository and Import Signing Key.

Step 4: Install PostgreSQL

Update the package list again and install PostgreSQL along with its client:

apt update -y
apt install postgresql postgresql-client -y
Terminal showing APT package management commands and output.
Terminal showing APT package management commands and output.

Step 5: Verify PostgreSQL Installation

After the installation, check the status of the PostgreSQL service to ensure it is running:

systemctl status postgresql
Terminal showing systemd service status details.

You can further verify the installation by connecting to the PostgreSQL server and checking the version:

psql -U postgres -c "SELECT version();"
Terminal showing sudo commands or output during Verify PostgreSQL Installation.
PostgreSQL terminal displaying the server version.

Step 6: Configure PostgreSQL

To configure PostgreSQL for your specific needs, follow these steps:

  1. Edit the pg_hba.conf file:
nano /etc/postgresql/16/main/pg_hba.conf
Terminal showing nano commands or output during Configure PostgreSQL.

Make the following changes:

local   all             all                                     trust
host    all             all             0.0.0.0/0                md5
PostgreSQL client authentication configuration with local and network access rules.

This file controls client authentication, such as how users are allowed to connect.

  1. Edit the postgresql.conf file:
nano /etc/postgresql/16/main/postgresql.conf
Terminal showing nano commands or output during Configure PostgreSQL.

Make the following change:

listen_addresses='*'
Terminal showing ip commands or output during Configure PostgreSQL.

This file includes general server settings, like port numbers and logging.

  1. Restart PostgreSQL to apply the changes:
systemctl restart postgresql
Terminal showing systemd service management commands or output.

Step 7: Create a Database and User

Connect to the PostgreSQL shell:

psql -U postgres
Terminal showing systemd service management commands or output.

Create a new role with superuser privileges:

CREATE ROLE root WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'password';
PostgreSQL terminal showing a database role creation command.

Verify the new role:

\du
PostgreSQL terminal displaying database roles and privileges.

Create a new database:

create database wpdb;
Terminal showing SQL database and user configuration commands.

Create a new user with an encrypted password:

create user wpuser with encrypted password 'password';
Terminal showing SQL database and user configuration commands.

List all databases:

\l
PostgreSQL terminal displaying a list of databases.

Step 8: Connect to PostgreSQL from a Remote Machine

To connect to your PostgreSQL instance from a remote machine, use the following command:

psql 'postgres://wpuser:password@postgres-ip-address:5432/wpdb?sslmode=disable'
Terminal showing a PostgreSQL remote connection command.

Replace wpuser, password, postgres-ip-address, and wpdb with your actual PostgreSQL username, password, server IP address, and database name.

You have successfully installed and configured PostgreSQL on Ubuntu 22.04. This robust database system is now ready for your applications.

If you’re looking for reliable hosting services, consider Shape.host for Cloud VPS solutions that offer great performance and scalability. Shape.host provides high-performance cloud VPS hosting with flexible plans, allowing you to scale resources as your needs grow. Whether you’re hosting a small application or a large enterprise database, Shape.host’s cloud infrastructure is designed to ensure maximum uptime, security, and speed, giving you peace of mind while you focus on your business.