How to Install and Configure PostgreSQL on Ubuntu: Complete Beginner’s Guide

1. Introduction

PostgreSQL is a highly reliable and high-performance relational database system widely used in many applications and systems on Ubuntu environments. This article explains how to install PostgreSQL on Ubuntu and perform basic configurations. Each step is clearly explained for beginners, including installation checks and troubleshooting connectivity issues, so you can set up your environment with confidence.

2. Prerequisites and Preparation

First, ensure that your Ubuntu version is either 20.04 or 22.04. Before installing PostgreSQL, update the package list to retrieve the latest package information.

sudo apt update

This ensures the installation process proceeds smoothly.

3. PostgreSQL Installation Steps

3.1 Add the PostgreSQL Repository

The default Ubuntu repository may not include the latest PostgreSQL version. Add the official PostgreSQL repository to install the most up-to-date release.

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
sudo wget -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo tee /etc/apt/trusted.gpg.d/pgdg.asc

3.2 Install PostgreSQL

Once the repository is added, install PostgreSQL and additional tools with the following commands:

sudo apt update
sudo apt install postgresql postgresql-contrib

3.3 Verify Installation

After installation, verify that PostgreSQL is installed correctly by checking its version.

postgres --version

4. Initial Configuration

4.1 Configure the PostgreSQL User

During installation, a system user named “postgres” is created. Switch to this user to perform database operations.

sudo -i -u postgres

4.2 Edit Local Connection Settings

Edit the pg_hba.conf file to set authentication methods. By default, only local connections are allowed. To enable remote access, modify the following file:

sudo nano /etc/postgresql/14/main/pg_hba.conf

For example, you can enforce “md5” authentication to enhance security:

local   all             postgres                                md5
host    all             all             127.0.0.1/32            md5

After editing, restart the PostgreSQL service to apply the changes.

sudo systemctl restart postgresql

5. Basic Operation Checks

5.1 Start and Stop PostgreSQL

PostgreSQL starts automatically upon installation, but you can manually start, stop, and check its status using the following commands:

sudo systemctl status postgresql
sudo systemctl start postgresql
sudo systemctl stop postgresql

5.2 Check Databases

Use the psql command to connect to PostgreSQL and view existing databases.

sudo -u postgres psql

At the command prompt, enter \l to list the current databases.

6. Install and Configure pgAdmin (Optional)

pgAdmin is a GUI tool that simplifies PostgreSQL administration. Install it with the following command and manage PostgreSQL through your browser:

sudo apt install pgadmin4

After installation, access the interface via http://localhost/pgadmin.

7. Troubleshooting Common Errors

7.1 Installation and Repository Errors

If you encounter dependency or repository errors during installation, verify the repository URL and update the package list again.

sudo apt update

7.2 Connection Errors

If you receive errors such as “Password authentication failed,” check your pg_hba.conf file, verify your password, and restart the service.

sudo systemctl restart postgresql

7.3 Network Error Resolution

If remote connections fail, the postgresql.conf file may have listen_addresses set to “localhost”. Modify it as follows to allow remote connections:

sudo nano /etc/postgresql/14/main/postgresql.conf

Change the setting as shown below:

listen_addresses = '*'

Restart the service to apply the changes.

sudo systemctl restart postgresql

8. Conclusion

This guide explained how to install PostgreSQL on Ubuntu, configure it, and perform basic operation checks. With pgAdmin, remote access configurations, and troubleshooting tips included, even first-time users should be able to set up the environment smoothly.

侍エンジニア塾