How to Install PostgreSQL on Ubuntu 25.04

Updated on 25 April, 2025
How to Install PostgreSQL on Ubuntu 25.04 header image

PostgreSQL is a reliable and scalable open-source relational database system that supports complex queries, custom data types, and JSON. It's ideal for managing large datasets in applications like analytics, GIS, and dynamic websites. It has a rich feature set perfected over decades and a strong community that ensures resilience and data integrity. Developers prefer it for its flexibility and compatibility with modern data workflows.

This article explains how to install PostgreSQL on a Ubuntu 24.04 server. You will enable the PostgreSQL database server and secure it for production use on your server.

Prerequisites

Before you begin, you need to:

  • Have access to an Ubuntu 25.04 instance as a non-root user with sudo privileges.

Install PostgreSQL

PostgreSQL is available in Ubuntu’s default APT package list, so you can install it easily using the terminal. Follow the steps below to set up PostgreSQL, add the official repository, and ensure that the database starts automatically when your server boots. After completing these steps, your server will be ready to use PostgreSQL for storing and managing data.

  1. Update the APT package index.

    console
    $ sudo apt update
    
  2. Install the postgresql-common package, which includes the common utilities and configurations for managing PostgreSQL.

    console
    $ sudo apt install -y postgresql-common -y
    
  3. Run the setup script to add the PostgreSQL APT repository on your server.

    console
    $ sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
    

    When prompted, press the Enter key to set up the repository on your server:

    This script will enable the PostgreSQL APT repository on apt.postgresql.org on
    your system. The distribution codename used will be plucky-pgdg.
    
    Press Enter to continue, or Ctrl-C to abort.
  4. Install the PostgreSQL server package.

    console
    $ sudo apt install -y postgresql
    
  5. Start the PostgreSQL service.

    console
    $ sudo systemctl start postgresql
    
  6. Enable the PostgreSQL systemd service to start at the time of system boot.

    console
    $ sudo systemctl enable postgresql
    

    Your output should be similar to the one below:

    Synchronizing state of postgresql.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
    Executing: /usr/lib/systemd/systemd-sysv-install enable postgresql
  7. View the status of the PostgreSQL service to confirm it's in active state.

    console
    $ sudo systemctl status postgresql
    

    Your output should be similar to the one below:

    ● postgresql.service - PostgreSQL RDBMS
         Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled)
         Active: active (exited) since Wed 2025-04-23 13:47:47 UTC; 3min 25s ago
     Invocation: 40d8e6047ffc4124834b0fb0d1fc16d8
       Main PID: 4811 (code=exited, status=0/SUCCESS)
       Mem peak: 1.6M
            CPU: 6ms

Secure the PostgreSQL Database Server

PostgreSQL uses the default postgres superuser account to handle administrative tasks like managing databases and users. On Ubuntu, it defaults to peer authentication, which permits access only to local system users without requiring a password. Follow the steps below to improve security, allow broader access control, enable password authentication and restrict access to only authorized users.

  1. Check the installed PostgreSQL version.

    console
    $ psql --version
    

    Your output should be similar to the one below:

    psql (PostgreSQL) 17.4 (Ubuntu 17.4-1.pgdg25.04+2)
  2. Log in to PostgreSQL server as the postgres user.

    console
    $ sudo -u postgres psql
    
  3. Set a new strong password for the postgres user.

    psql
    postgres=# ALTER USER postgres WITH ENCRYPTED PASSWORD 'strong_password';
    

    Replace strong_password with a strong password including random characters.

  4. Create a new user named park_admin and set a strong password for secure authentication.

    psql
    postgres=# CREATE USER park_admin ENCRYPTED PASSWORD 'strong_password';
    

    Replace strong_password with a strong password including random characters.

  5. Exit the PostgreSQL console.

    psql
    postgres=# EXIT;
    
  6. Enable password authentication by modifying the pg_hba.conf file.

    console
    $ sudo sed -i '/^local/s/peer/scram-sha-256/' /etc/postgresql/17/main/pg_hba.conf
    

    Replace 17 with your installed PostgreSQL version if it's different.

  7. Restart the PostgreSQL service to apply the changes made to the configuration.

    console
    $ sudo systemctl restart postgresql
    
  8. View the service status to confirm PostgreSQL service is in active state.

    console
    $ sudo systemctl status postgresql
    

    Your output should be similar to the one below:

    ● postgresql.service - PostgreSQL RDBMS
         Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled)
         Active: active (exited) since Wed 2025-04-23 13:54:24 UTC; 19s ago
     Invocation: 3bb07d5e081e4289976cf4d957be27a0
        Process: 7257 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
       Main PID: 7257 (code=exited, status=0/SUCCESS)
       Mem peak: 1.6M
            CPU: 6ms

Access the PostgreSQL Database Server

You can access the PostgreSQL database console through the pre-installed psql utility, which comes with the server package. Alternatively, you can use graphical tools that establish a direct connection to the server. Follow the steps below to access the PostgreSQL database console and create a new sample database for use with a non-privileged user.

  1. Create a new sample PostgreSQL database amusement_park and grant the ownership to the park_admin user.

    console
    $ sudo -u postgres createdb amusement_park -O park_admin
    

    When prompted, enter the postgres user password you created earlier.

  2. Log in to the PostgreSQL database as the park_admin user.

    console
    $ sudo -u postgres psql -U park_admin -d amusement_park
    

    When prompted, Enter the park_admin user password and press Enter to access the database.

  3. Create a new table visitors in the amusement_park database.

    psql
    amusement_park=> CREATE TABLE visitors (
               visitor_id SERIAL PRIMARY KEY,
               first_name VARCHAR(50),
               last_name VARCHAR(50),
               entry_date DATE
          );
    

    The above SQL statement creates a new table in the amusement_park database with the following columns:

    • visitor_id is a PRIMARY KEY that uniquely identifies each visitor.
    • SERIAL generates a new visitor_id for each new record.
    • first_name and last_name store the visitor’s name.
    • entry_date stores the date the visitor entered the amusement park.
  4. Insert sample data into the visitors table.

    psql
    amusement_park=> INSERT INTO visitors
           ( first_name, last_name, entry_date)
           VALUES
           ( 'Alice', 'Wong', '2024-11-15'),
           ( 'Brian', 'Taylor', '2024-02-28'),
           ( 'Clara', 'Nguyen', '2024-04-10');
    
  5. Query the visitors table to view all available records.

    psql
    amusement_park=> SELECT * FROM visitors;
    

    Your output should be similar to the one below:

     visitor_id | first_name | last_name | entry_date 
    ------------+------------+-----------+------------
              1 | Alice      | Wong      | 2024-11-15
              2 | Brian      | Taylor    | 2024-02-28
              3 | Clara      | Nguyen    | 2024-04-10
    (3 rows)
  6. Exit the PostgreSQL console.

    psql
    amusement_park=> EXIT;
    

Conclusion

You have installed PostgreSQL on your Ubuntu 25.04 server and used the psql utility to create databases and manage records. You can now integrate PostgreSQL with your applications to securely manage data. For additional information and configuration options, refer to the official PostgreSQL documentation.

Comments

No comments yet.