PostgreSQL is a powerful and popular open-source database management system. It is known for its reliability, robustness, and excellent performance. Many developers and companies use PostgreSQL to store and manage their data because it supports advanced data types and performance optimization features.
This guide will help you install PostgreSQL on Ubuntu 24.04 in a few simple steps. We will start by updating your system to ensure you have the latest packages. Then, we will add the PostgreSQL repository, which will allow us to install the latest version of PostgreSQL. After installing PostgreSQL, we will start the service and make sure it runs automatically whenever your system boots up. We will also cover basic security steps to protect your database. Finally, we will show you how to verify the installation and create a new database.
By following these instructions, you will have PostgreSQL up and running on your Ubuntu 24.04 (Noble Numbat) system.
Step 1: Update Your System
First, update your system to make sure all your packages are up-to-date. Open a terminal and run the following commands:
sudo apt update && sudo apt upgrade -y
These commands will update all packages on your system.
Step 2: Add the PostgreSQL Repository
PostgreSQL team provides an shell script to configure Apt repository on debain based systems. To use that script, you need to install the postgresql-common package first. This package includes utilities for managing PostgreSQL. Run this command:
sudo apt install -y postgresql-common
Next, you need to add the PostgreSQL repository to your system. This will ensure you get the latest version of PostgreSQL. Run this command:
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
This script adds the PostgreSQL Global Development Group (PGDG) APT repository to your system.
Step 3: Install PostgreSQL
After adding the PostgreSQL repository, update your package list again:
sudo apt update
This ensures that your system recognizes the new repository.
Now, install PostgreSQL by running this command:
sudo apt install -y postgresql
This will install may additional packages required to run Postgres database server.
Step 4: Start PostgreSQL Service
After the installation is complete, you need to start the PostgreSQL service. Run this command:
sudo systemctl start postgresql
This command starts the PostgreSQL service. To make sure PostgreSQL starts automatically when your system boots, enable the service with this command:
sudo systemctl enable postgresql
Step 5: Secure PostgreSQL Installation
By default, PostgreSQL is fairly secure, but there are a few steps you can take to improve security:
5.1: Change the PostgreSQL Password:
- Log in to the PostgreSQL prompt as the postgres user:
sudo -i -u postgres psql
- Change the password for the postgres user:
postgres=# \password postgres
Enter a strong password when prompted.
- Exit the PostgreSQL prompt by typing:
postgres=# \q
5.2: Configure Remote Access:
If you need to allow remote access to your PostgreSQL server, you will need to edit the configuration files. This step is optional and only needed if you plan to access your PostgreSQL server from another machine.
- Edit the postgresql.conf file:
sudo nano /etc/postgresql/14/main/postgresql.conf
- Uncomment and change the listen_addresses line to:
listen_addresses = '*'
Save and exit the file.
- Edit the pg_hba.conf file to allow remote access:
sudo nano /etc/postgresql/14/main/pg_hba.conf
- Add the following line to the end of the file to allow access from any IP address:
host all all 0.0.0.0/0 md5
Save and exit the file.
- Restart PostgreSQL to apply the changes:
sudo systemctl restart postgresql
Step 6: Verify PostgreSQL Installation
To check if PostgreSQL is installed and running correctly, log in to the PostgreSQL prompt with the postgres user:
sudo -i -u postgres psql
If you see the PostgreSQL prompt (postgres=#), it means PostgreSQL is installed and running correctly.
Step 7: Create a Database (Optional)
If you want to create a new database, you can do it from the PostgreSQL prompt. For example, to create a database named testdb, run:
postgres=# CREATE DATABASE testdb;
To see the list of databases, use:
postgres=# \l
To exit the PostgreSQL prompt, type:
postgres=# \q
Conclusion
You have successfully installed PostgreSQL on Ubuntu 24.04 (Noble Numbat) LTS system. You can now start using PostgreSQL to manage your databases. If you have any issues, you can refer to the PostgreSQL documentation or get the help from online communities and forums.