=[object Object]

A Comprehensive Guide to Installing PostgreSQL on Ubuntu: Step-by-Step Instructions

Cyber Security

Introduction

PostgreSQL, commonly known as Postgres, is a powerful open-source object-relational database management system (DBMS) renowned for its robustness and reliability. Its adherence to SQL standards and features such as ACID compliance make it a favorite among developers for complex data-driven applications.

In this guide, we will walk you through the essential steps required to successfully install PostgreSQL on an Ubuntu system. Whether you're a beginner or experienced, these instructions will simplify the process of setting up your database server.

Prerequisites

Before we dive into the installation steps, ensure you have:

  • An Ubuntu server set up with a non-root user.
  • Sudo privileges for executing administrative commands.
  • A basic firewall configuration.

Step 1: Updating the System

To kick off the installation, first, update your server's package index. This ensures that you have the latest updates and security patches:

  1. sudo apt update

Step 2: Installing PostgreSQL

Next, install PostgreSQL along with the additional utilities that enhance its functionality:

  1. sudo apt install postgresql postgresql-contrib

After installation, verify the version installed:

  1. psql --version

Start the PostgreSQL service to ensure it's running:

  1. sudo systemctl start postgresql.service

Using PostgreSQL Roles and Databases

PostgreSQL utilizes a role-based authentication system, which is somewhat akin to Unix-style users and groups. Upon installation, a default user named postgres is created, which allows you to access the database management system.

Accessing the PostgreSQL Command Line

To interact with PostgreSQL, you can switch to the postgres user:

  1. sudo -i -u postgres

Then enter the PostgreSQL shell:

  1. psql

To exit, simply type:

  1. \q

Creating New Roles and Databases

Creating new roles and databases is straightforward. For example, to create a user named sammy with superuser privileges:

  1. sudo -u postgres createuser --interactive

To create a new database for the user:

  1. sudo -u postgres createdb sammy

Enabling Remote Access Securely

By default, PostgreSQL is configured to only accept connections from localhost. To enable remote connections securely, follow these steps:

Configuring PostgreSQL

1. Edit the main configuration file:

  1. sudo nano /etc/postgresql/17/main/postgresql.conf

2. Find the listen_addresses line and change it:

  1. listen_addresses = '*'

3. Update the pg_hba.conf file for host-based authentication:

  1. sudo nano /etc/postgresql/17/main/pg_hba.conf

Add a line for the user 'sammy' connecting from a specific IP:

  1. host all sammy 203.0.113.10/32 scram-sha-256

Firewall Configuration

To allow traffic on the default PostgreSQL port (5432), run:

  1. sudo ufw allow 5432/tcp

Finally, restart PostgreSQL to apply all changes:

  1. sudo systemctl restart postgresql

Backup and Recovery Strategies

A reliable backup strategy is vital for any database. PostgreSQL offers two primary methods for backing up your data:

  • Logical Backups: Use the pg_dump utility to create a backup file containing SQL commands.
  • Physical Backups: Use pg_basebackup along with Write-Ahead Logs (WAL) for Point-in-Time Recovery (PITR).

Conclusion

In this guide, we've explored the essential steps to install and configure PostgreSQL on Ubuntu. From setting up roles and databases to enabling secure remote access and establishing a backup strategy, you now have a robust foundation for managing your databases. As you move forward, consider diving deeper into database design and security best practices to optimize your PostgreSQL experience.

Share this article:

Thomas Wells

About Thomas Wells

Izende Studio Web has been serving St. Louis, Missouri, and Illinois businesses since 2013. We specialize in web design, hosting, SEO, and digital marketing solutions that help local businesses grow online.

Need Help With Your Website?

Whether you need web design, hosting, SEO, or digital marketing services, we're here to help your St. Louis business succeed online.

Get a Free Quote