1080*80 ad

Installing PostgreSQL on Ubuntu 24.04: A 2025 Step-by-Step Guide

How to Install PostgreSQL on Ubuntu 24.04: A Step-by-Step Guide

PostgreSQL is a powerful, open-source object-relational database system renowned for its reliability, feature robustness, and performance. For developers and system administrators using Ubuntu 24.04 (Noble Numbat), installing and configuring PostgreSQL is a fundamental skill.

This comprehensive guide will walk you through every step of the process, from initial installation to creating your first user and database, ensuring you have a secure and functional setup ready for your projects.

Prerequisites

Before we begin, ensure you have the following:

  • A system running Ubuntu 24.04.
  • A non-root user account with sudo privileges.
  • Access to the command line terminal.

Step 1: Update Your System’s Package Repository

First, it’s crucial to refresh your local package index. This ensures you are installing the latest available versions of the software and its dependencies. Open your terminal and run the following commands:

sudo apt update
sudo apt upgrade

This updates the list of available packages and then upgrades any outdated software on your system, which is a critical first step for stability and security.

Step 2: Install the PostgreSQL Server

Ubuntu’s default repositories contain PostgreSQL packages, making the installation process straightforward. We will install the main postgresql package along with postgresql-contrib, which provides additional utilities and functionality that are often useful for advanced database management.

To install PostgreSQL, execute this command:

sudo apt install postgresql postgresql-contrib

The system will prompt you to confirm the installation. Press Y and then Enter to proceed.

Step 3: Verify the PostgreSQL Installation

Once the installation is complete, the PostgreSQL service will automatically start. You can verify that the database server is active and running using the systemctl command.

sudo systemctl status postgresql

If the service is running correctly, you will see output indicating that the service is active (running). This confirms that your PostgreSQL server is up and operational. If it’s not running, you can start it with sudo systemctl start postgresql and enable it to start on boot with sudo systemctl enable postgresql.

Step 4: Accessing PostgreSQL

During installation, a system user account named postgres is automatically created. This user is configured with the default PostgreSQL administrative role. To connect to the database server, you must first switch to this user account.

Switch to the postgres user with the following command:

sudo -i -u postgres

Your command prompt will change to postgres@..., indicating you are now operating as the PostgreSQL superuser. From here, you can access the PostgreSQL command-line interface, known as psql.

psql

You are now inside the PostgreSQL prompt, where you can execute SQL queries and manage the database. To exit the psql interface, type \q and press Enter. To return to your regular system user, type exit.

Step 5: Secure Your Setup by Creating a New Role

For security, it is highly recommended not to use the default postgres role for your applications. Instead, you should create dedicated roles with specific privileges.

You can create a new role directly from your regular command line using the createuser utility. The --interactive flag will prompt you for the role name and its permissions.

sudo -u postgres createuser --interactive

Follow the prompts:

  • Enter name of role to add: myuser (or your preferred username)
  • Shall the new role be a superuser? (y/n): n (unless you have a specific reason to grant superuser privileges)

This creates a new user (role) that can be used to connect to and manage databases.

Step 6: Create a New Database

With your new role created, the next step is to create a database for it to manage. You can do this easily with the createdb command. It’s best practice to make the new user the owner of this database.

sudo -u postgres createdb -O myuser mydatabase

Let’s break down this command:

  • sudo -u postgres: Executes the command as the postgres user.
  • createdb: The command to create a new database.
  • -O myuser: Sets the owner of the database to the myuser role we created earlier.
  • mydatabase: The name of your new database.

Step 7: Connect to Your New Database

You have now successfully created a new user and a new database. To test the connection, you can try to connect to mydatabase using the myuser role.

psql -d mydatabase -U myuser

If everything is configured correctly, PostgreSQL will ask for the user’s password (if you set one) and then grant you access to the mydatabase prompt. This confirms your setup is working as intended.

Conclusion

You have successfully installed and configured a secure PostgreSQL server on your Ubuntu 24.04 system. By following these steps, you have not only installed the database but also implemented best practices by creating a dedicated, non-superuser role and a new database for your applications.

From here, you are ready to connect your applications, migrate data, or explore more advanced features like performance tuning and automated backups. Having a solid PostgreSQL foundation is a key step in building scalable and reliable software.

Source: https://www.redswitches.com/blog/how-to-install-postgresql-on-ubuntu/

900*80 ad

      1080*80 ad