Skip to main content
How To

How to Install PostgreSQL on Ubuntu [Setup & Configurations]

Learn to install PostgreSQL on Ubuntu. Also learn how to set it up and configure it for the first use.

Sagar Sharma

Being open-source, and supporting SQL and JSON querying is one of the crucial reasons why it is one of the most popular names when choosing a database management system.

And PostgreSQL is available in the default repository of Ubuntu.

While writing, I got PostgreSQL version 14.6 from the default repo.

So if the version is not the major concern, it can be installed using a single command:

sudo apt install postgresql

But what if you want to have the most recent version? Well, that's what today's guide is all about.

Install the latest version of PostgreSQL on Ubuntu (if you need it)

To have the most recent version, you are required to utilize PostgreSQL's repository.

So first, create a repository configuration file using the following command:

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Next, add the repository signing keys to have the authentic packages:

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

To take effect from the steps you made recently, update system repositories:

sudo apt update 

And finally, you can have the latest version of PostgreSQL:

sudo apt install postgresql

The installed version of PostgreSQL can be retrieved from its shell prompt. So to start the prompt, use the following command:

sudo -u postgres psql

And now, execute the following command in PostgreSQL to check the installed version:

SELECT version();
check the installed version of PostgreSQL in ubuntu

How to configure PostgreSQL on Ubuntu

In this section, I will walk you through the two most basic configurations that you should do on your fresh PostgreSQL install:

  • Changing password
  • Allowing PostgreSQL to accept remote connections

So let me start with changing the password.

How to change user password in PostgreSQL

To change the password, first, connect to the PostgreSQL server that will get you to the PostgreSQL prompt:

sudo -u postgres psql

Now, you can follow the given command syntax to change the user password:

ALTER USER postgres PASSWORD 'Enter_password_here';

For reference, I changed my password to Sagar@Learnubuntu:

ALTER USER postgres PASSWORD 'Sagar@Learnubuntu';
change password in PostgreSQL

How to allow PostgreSQL to accept remote connections

In its default configuration, PostgreSQL can only be accessed via a loopback interface 127.0.0.1 on port 5432 and a UNIX socket.

So in simple words, you can only use PostgreSQL on the main machine where you have performed the installation.

Don't trust me? You can check the listing ports by yourself using the ss command:

ss -nlt
find listening ports on ubuntu

To use PostgreSQL remotely, you will have to make changes in postgresql.conf file.

But the location of that file might be different based on the version you are using. And to find the exact location postgresql.conf file, use:

sudo find / -name "postgresql.conf" 2>/dev/null

Here, I have redirected the errors to the /dev/null.

You can learn more about how you can redirect standard input, output, and errors by the given guide:

Input Output & Error Redirection in Linux [Beginner’s Guide]
Redirection is an essential concept in Linux. Learn how to use stdin, stdout, stderr and pipe redirection in Linux command line.

Once you find the exact location of the postgresql.conf, you can open that file using any of your preferred text editors.

Here, I'm going with nano:

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

In this config file, under the CONNECTIONS AND AUTHENTICATION section, you will find the following line:

#listen_addresses = 'localhost' 
listening addresses on PostgreSQL config file

Which needs to be replaced with:

listen_addresses = '*' 
Allow remote access in PostgreSQL in Ubuntu

Save changes and exit from the nano text editor.

To benefit from the configuration, restart the PostgreSQL:

sudo systemctl restart postgresql

And now, if you will use the ss command to list listening ports, you will find that PostgreSQL is listening on all interfaces:

ss -nlt
how to find listening ports on ubuntu

However, if you will try to use PostgreSQL remotely, it will throw the following error:

error connecting to the remote PostgreSQL database on ubuntu

To allow the remote connections, you will have to make changes in pg_hba.conf file.

And to know its exact path, use the following command:

sudo find / -name "pg_hba.conf" 2>/dev/null
find the location of pg_hba.conf file in ubuntu

Once you find the location of the pg_hba.conf file, open it using your preferred text editor. Here, I'm going with the nano:

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

Add the following line at the end of this config file:

host    all          all            0.0.0.0/0  md5
allow remote connections of PostgreSQL using ubuntu

Save changes and restart the PostgreSQL:

sudo systemctl restart postgresql

Now, let's connect from the client's machine

How to access PostgreSQL from the remote system

To access PostgreSQL on the remote machine, you'd have to install the PostgreSQL client on that system:

sudo apt-get install postgresql-client

Once done, you can access PostgreSQL from your system using the following command syntax:

psql -h [IP_of_host] -U postgres
access PostgreSQL from the remote system

Once you execute the above command, it will ask for the password.

Remember I started the configuration part with the password? You will have to enter that passphrase here.

Wrapping Up

This was a short tutorial on how you can install the latest version of PostgreSQL on Ubuntu.

I hope this guide will solve the query you had before coming to us.

And if you have any queries, let me know in the comments.  

Sagar Sharma