UbuntuUbuntu Based

How To Install PostgreSQL on Ubuntu 24.04 LTS

Install PostgreSQL on Ubuntu 24.04

In this tutorial, we will show you how to install PostgreSQL on Ubuntu 24.04 LTS. PostgreSQL, often referred to as Postgres is a powerful, open-source object-relational database system known for its reliability, scalability, and robustness. It has become a go-to choice for businesses and developers who require a stable and feature-rich database solution. PostgreSQL offers advanced features like multi-version concurrency control (MVCC), point-in-time recovery, and a flexible extension system, making it suitable for a wide range of applications.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of the PostgreSQL open-source object-relational database system on Ubuntu 24.04 (Noble Numbat). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.

Prerequisites

  • A server running one of the following operating systems: Ubuntu and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies.
  • An Ubuntu 24.04 system with root access or a user with sudo privileges.

Install PostgreSQL on Ubuntu 24.04 LTS Noble Numbat

Step 1. Updating the Package Repository.

To ensure a smooth installation process and maintain system stability, it is crucial to update your Ubuntu system to the latest available packages. Open a terminal and execute the following commands:

sudo apt update

The apt update command refreshes the package list, while apt upgrade installs the available updates. This step helps resolve any dependency issues and provides access to the latest security patches and bug fixes.

Step 2. Installing PostgreSQL on Ubuntu 24.04.

Ubuntu 24.04 includes PostgreSQL in its default repositories, making the installation process straightforward. To install PostgreSQL, run the following command in your terminal:

sudo apt install postgresql postgresql-contrib

This command installs the PostgreSQL server along with the postgresql-contrib package, which provides additional utilities and extensions to enhance PostgreSQL’s functionality.

During the installation, you may be prompted to confirm the installation by pressing Y and then Enter. The package manager will then download and install the necessary files.

After the installation is complete, it’s essential to verify that PostgreSQL is running correctly. Use the following command to check the status of the PostgreSQL service:

sudo systemctl status postgresql

If the installation was successful, you should see output similar to the following:

postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
   Active: active (exited) since Thu 2024-05-11 10:30:00 UTC; 5min ago
 Main PID: 1234 (code=exited, status=0/SUCCESS)
    Tasks: 0 (limit: 9443)
   Memory: 0B
   CGroup: /system.slice/postgresql.service

Step 4. Configuring PostgreSQL.

By default, PostgreSQL is configured to use peer authentication, which allows connections only from the local system. To enable remote connections, you need to modify the PostgreSQL configuration files.

First, open the pg_hba.conf file using a text editor with sudo privileges:

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

Locate the lines that specify the authentication method for local and remote connections. By default, they are set to peer and md5, respectively. Change the peer method to md5 for the local connections to allow password-based authentication.

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

Save the changes and exit the editor, then open the postgresql.conf file to allow remote connections:

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

Locate the line that starts with #listen_addresses and uncomment it by removing the # symbol. Set the value to '*' to allow connections from any IP address.

listen_addresses = '*'

Save the changes and exit the editor, then restart the PostgreSQL service to apply the new configuration:

sudo systemctl restart postgresql

Step 5. Creating a New Database and User.

With PostgreSQL installed and configured, you can now create a new database and user for your application. To create a new database, use the createdb command with the desired database name:

sudo -u postgres createdb mydatabase

Replace mydatabase with your preferred database name.

To create a new user and grant privileges, use the createuser command with the --interactive flag:

sudo -u postgres createuser --interactive

You will be prompted to enter the username and specify the user’s privileges. Follow the on-screen instructions to create the user according to your requirements.

Step 6. Additional Tools and Extensions.

PostgreSQL offers a wide range of additional tools and extensions to enhance its functionality and ease of use. One popular tool is pgAdmin, a web-based graphical user interface for managing PostgreSQL databases.

To install pgAdmin, run the following command:

sudo apt install pgadmin4

Once installed, you can access pgAdmin by opening a web browser and navigating to http://localhost/pgadmin4. Use the credentials you set during the installation to log in and start managing your PostgreSQL databases.

Install PostgreSQL on Ubuntu 24.04 LTS Noble Numbat

Step 7. Troubleshooting Common Installation Issues

Despite following the installation steps carefully, you may encounter issues during the process. Here are a few common problems and their solutions:

  1. PostgreSQL service fails to start: Check the PostgreSQL log files located in /var/log/postgresql/ for any error messages. Ensure that the configuration files are properly set up and there are no syntax errors.
  2. “Peer authentication failed” error: This error occurs when the authentication method is set to peer instead of md5. Double-check the pg_hba.conf file and ensure that the authentication method is correctly configured.
  3. “Could not connect to server” error: Verify that the PostgreSQL service is running using the sudo service postgresql status command. Check the postgresql.conf file to ensure that the listen_addresses parameter is set correctly.

If you encounter any other issues, consult the official PostgreSQL documentation or seek assistance from the PostgreSQL community forums.

Congratulations! You have successfully installed PostgreSQL. Thanks for using this tutorial for installing the PostgreSQL open-source object-relational database system on the Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the PostgreSQL website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button