In this blog post, we will cover how to install PostgreSQL on Ubuntu 24.04 step-by-step.
PostgreSQL is a powerful, open-source relational database management system (RDBMS) that is widely used for its reliability, feature set, and performance. This feature-rich database is used by many tech giants, such as Apple, IMDB, Instagram, and so on.
PostgreSQL supports large number of the SQL standard and is constructed to be extensible by users in many aspects. Some of the salient features include ACID transactions, foreign keys, subqueries, triggers, user-defined types, functions, etc.
Prerequisites
Before installing the PostgreSQL database server, make sure following things are in place.
- A system running Ubuntu 24.04
- A regular user with sudo rights
- An active internet connection
- At least 2 GB of RAM
Please note that this is a minimal requirement for the demo environment. The actual hardware configuration will vary with data volume.
Without any further delay, let’s deep dive into PostgreSQL installation steps.
1) Update the System
Start by updating the system’s package index to ensure all packages are up to date. Run the following apt commands from the terminal.
$ sudo apt update && sudo apt upgrade -y
2) Install PostgreSQL on Ubuntu 24.04
PostgreSQL 16 and its dependencies are included in Ubuntu’s default package repositories.
To install it, run the following command:
$ sudo apt install postgresql postgresql-client -y
Above command will install PostgreSQL 16.
Installing PostgreSQL 17 on Ubuntu 24.04
If you want to install latest version of PostgreSQL (i.e postgresql 17) then first, we need to add its official package repository.
Add PostgreSQL 17 APT Repository
Run the following set of commands one after the another.
$ sudo apt install curl ca-certificates $ sudo install -d /usr/share/postgresql-common/pgdg $ sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc $ sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
Now, let’s fetch the latest versions of the packages. We can achieve this using the apt update command as shown below:
$ sudo apt update
The above command will take a few seconds to complete.
Execute the following command to install latest version of PostgreSQL,
$ sudo apt install postgresql-17 postgresql-client-17 -y
After the installation, verify the version, run,
$ psql --version
Next, let’s verify whether PostgreSQL service is up and running:
$ sudo systemctl status postgresql
Output above shows that postgresql service is up and running.
3) Set PostgreSQL Admin User Password
By default, we can connect to the PostgreSQL server without using any password. Let’s see this in action using the psql utility:
$ sudo -u postgres psql postgres=#
In the above output, the postgres=# prompt indicated the active connection with the PostgreSQL server.
In this example, we have used the postgres user. This is an admin user of PostgreSQL, and it gets created during the installation process.
Allowing administrative access to the database without any password isn’t a good idea. So, let’s set the password for the postgres user:
postgres=# ALTER USER postgres PASSWORD 'demoPassword';
The above SQL query sets the user password to demoPassword. Please note that, we have used a very simple password because this is a demo environment. However, the same is not recommended in the production environment.
Let’s verify that the password has been set successfully. So first, terminate the current session with the server using the \q command.
postgres=# \q
Output of above commands,
Now, let’s connect to the database server again:
$ psql -h localhost -U postgres
Let’s enter the demoPassword string as a password and now we are connected to the database.
4) Configure PostgreSQL to Allow Remote Connections
By default, PostgreSQL accepts connections from the localhost only. However, we can easily modify the configuration to allow connection from remote clients.
PostgreSQL reads its configuration from the postgresql.conf file which is located in the /etc/postgresql/<version>/main/ directory. Here, the version indicates the major version of PostgreSQL.
For example, in our case the full path of the file is /etc/postgresql/17/main/postgresql.conf
Now, open the postgresql.conf file in a text editor, uncomment the line that starts with the listen_addresses, and replace ‘localhost’ with ‘*’.
This setting is located under the CONNECTIONS AND AUTHENTICATION section. After modification the file will look like this:
$ sudo vi /etc/postgresql/17/main/postgresql.conf
Save and close the file.
Next, edit the IPv4 local connections section of the pg_hba.conf file to allow IPv4 connections from all clients. Please note that this file is also located in /etc/postgresql/17/main/ directory.
$ sudo vi /etc/postgresql/17/main/pg_hba.conf
After modification the file will look like this:
In the above configuration indicates to allow connection from the network 192.168.1.0/24
Note: In case, Ubuntu firewall is running on your system then allow PostgreSQL 5432 port using following command,
$ sudo ufw allow 5432/tcp
Verifying Remote Connection
Finally, restart the service and verify it’s up and running:
$ sudo systemctl restart postgresql $ sudo systemctl status postgresql
Now, let’s try to access DB from remote client.
$ psql -h 192.168.1.11 -U postgres
In this example, 192.168.1.11 is the IP address of the PostgreSQL database server.
Here we can see that we are able to access DB from the remote client.
Troubleshooting Tips
If you encounter issues:
1. Check PostgreSQL logs:
$ sudo tail -f /var/log/postgresql/postgresql-17-main.log
2. Verify port availability:
$ sudo netstat -plunt | grep postgres
That’s all from this blog post, I hope you have found it informative and useful. Please do post your queries and feedback in the below comments section.
Read Also: How to Install pgAdmin on Ubuntu 24.04
Great article! Well written and laid out.
Awesome article! It would be even better if you add steps to install pgadmin + adding connection to the deployed database.