> ## Content Index
> Fetch the complete content index at: https://devopscube.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Install PostgreSQL on Ubuntu [Setup & Configurations Covered]
- URL: https://devopscube.com/install-postgresql-on-ubuntu/
- Published: 2023-01-12T03:49:00.000Z
- Updated: 2025-06-19T13:15:29.000Z
- Author: Bibin Wilson
- Tags: HOW TO GUIDES, #Migrated-1741795015845, #wp, #wp-post, #Import 2025-03-12 15:57, #blog

This guide will teach you how to **install PostgreSQL on Ubuntu** and change its key configurations.

[PostgreSQL](https://www.postgresql.org/?ref=devopscube.com) is one of the best open source [object-relational database systems](https://en.wikipedia.org/wiki/Object%E2%80%93relational%5Fdatabase?ref=devopscube.com). Whether you are a developer, sysadmin, or DevOps engineer, it is essential to know the basic setup and configurations involved in a PostgreSQL setup.

As per the latest StackOverflow developer survey, [40.42% of developers use PostgreSQL](https://insights.stackoverflow.com/survey/2021?ref=devopscube.com) as a database.

![](https://storage.ghost.io/c/5f/2f/5f2f4d20-2abf-4534-8d40-7aa233aedd43/content/images/2025/03/image-25-28.png)

## Install PostgreSQL on Ubuntu

It is always better to download the latest version available in the official PostgreSQL Ubuntu repository.

```

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

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

sudo apt-get update
```

Install the latest version of PostgreSQL

```
sudo apt-get -y install postgresql
```

If you want to install a specific version of PostgreSQL, you can search for the available versions [from here](https://www.postgresql.org/download/linux/ubuntu/?ref=devopscube.com) and use the version with the install command as shown below.

```
sudo apt-get -y install postgresql-17
```

Lets validate PostgreSQL by checking the service status.

```
sudo systemctl status postgresql
```

## Connect to PostgreSQL & Change Password

You can connect to the PostgreSQL server using the `psql` client

When you install PostgreSQL, the `psql` client also gets installed.

Execute the following command to connect to the server.

```
sudo -u postgres psql
```

Let's check the version of PostgreSQL using the following command.

```
SELECT version();
```

Now, we have to change the `postgres` user's password so that we can **allow remote PostgreSQL connection** using password authentication.

Execute the following query to set a password. Replace myPassword with a password of your choice.

```
ALTER USER postgres PASSWORD 'myPassword';
```

## PostgreSQL Remote Connection Configuration

By default, the PostgreSQL server is accessible only on the loopback interface (127.0.0.1) on port 5432 and through the Unix socket. Therefore, you can only connect to the PostgreSQL server from within the Ubuntu server where PostgreSQL is installed .

![PostgreSQL remote connection architecture.](https://storage.ghost.io/c/5f/2f/5f2f4d20-2abf-4534-8d40-7aa233aedd43/content/images/2025/03/image-26-24.png)

You can verify it by listing all the TCP connections using the `ss` [Linux networking command.](https://devopscube.com/list-linux-networking-troubleshooting-and-commands-beginners/)

```
ss -nlt
```

![PostgreSQL listening on loopback interface (127.0.0.1)](https://storage.ghost.io/c/5f/2f/5f2f4d20-2abf-4534-8d40-7aa233aedd43/content/images/2025/03/image-20-35.png)

To enable remote connection to PostgreSQL, we need to allow it in the postgresql.conf file.  
For Ubuntu systems, `postgresql.conf `is located in `/etc/postgresql/17/main/` location. 17 is the version number.

If you are using a different PostgreSQL version, change it accordingly.

Or you can use the following find command to locate the file.

```
sudo find / -name "postgresql.conf"
```

Open the file `postgresql.conf` file.

```
postgresql.conf
```

Under `CONNECTIONS AND AUTHENTICATION` section you will find the following commented parameter.

```
#listen_addresses = 'localhost' 
```

![PostgreSQL listen_addresses parameter](https://storage.ghost.io/c/5f/2f/5f2f4d20-2abf-4534-8d40-7aa233aedd43/content/images/2025/03/image-22-30.png)

Replace it with the following and save the file. With this configuration, we are enabling PostgreSQL server connections to accept connections from all IP addresses.

```
listen_addresses = '*' 
```

To apply the change, restart the PostgreSQL service.

```
sudo systemctl restart postgresql
```

Now, if you list all the TCP connections, you can see the PostgreSQL service listening on all interfaces, as shown below.

```
ss -nlt
```

![PostgreSQL listening on all interfaces](https://storage.ghost.io/c/5f/2f/5f2f4d20-2abf-4534-8d40-7aa233aedd43/content/images/2025/03/image-21-35.png)

We have allowed only connections on all interfaces. However, If you try to connect to PostgreSQL from a remote machine, you will get the following error because you need to enable client connections to all the Databases and users.

```
psql: error: connection to server at "", port 5432 failed: FATAL:  no pg_hba.conf entry for host "", user "postgres", database "postgres", SSL encryption
connection to server at "", port 5432 failed: FATAL:  no pg_hba.conf entry for host "", user "postgres", database "postgres", no encryption
```

To allow client connections to all databases, you need to edit the **pg\_hba.conf** file.

Open **pg\_hba.conf** file.

```
sudo vi /etc/postgresql/17/main/pg_hba.conf 
```

Add the following line to the end of the file.

```
host    all          all            0.0.0.0/0  md5
```

![PostgreSQL pg_hba.conf configuration](https://storage.ghost.io/c/5f/2f/5f2f4d20-2abf-4534-8d40-7aa233aedd43/content/images/2025/03/image-23-34.png)

To apply the change, restart the PostgreSQL service.

```
sudo systemctl restart postgresql
```

## Test PostgreSQL Remote Connection

Now, let's try connecting PostgreSQL from a remote machine.

First, install the PostgreSQL client on the system you are trying to connect to the PostgreSQL server.

For Ubuntu,

```
sudo apt-get install postgresql-client
```

For MAC

```
brew install libpq
brew link --force libpq
```

Once the client is installed, execute the following psql command to connect to the PostgreSQL remotely. Replace 192.168.5.5 with your PostgreSQL server IP. When it prompts for a password, enter the password we generated during the initial configuration.

```
psql -h 192.168.5.5 -U postgres
```

![PostgreSQL remote login](https://storage.ghost.io/c/5f/2f/5f2f4d20-2abf-4534-8d40-7aa233aedd43/content/images/2025/03/image-24-28.png)

## Important PostgreSQL Server Configurations

The following table contains important PostgreSQL configurations.

| Config                                                  | Details                                                     |
| ------------------------------------------------------- | ----------------------------------------------------------- |
| PostgreSQL default port                                 | 5432                                                        |
| Default user                                            | postgres                                                    |
| Config files location (postgresql.conf & pg\_hba.conf ) | /etc/postgresql/postgresql.conf/etc/postgresql/pg\_hba.conf |
| Default database                                        | postgres                                                    |
| Default data directory                                  | /var/lib/postgresql/                                        |

## PostgreSQL Server Installation FAQs

The following are the frequently asked questions about PostgreSQL Server Installation.

### Does PostgreSQL have a default database?

Yes. When you install PostgreSQL, a default database named Postgres gets created. It contains user information, utilities, and third-party applications.

### What is the location of postgresql.conf file?

The `postgresql.conf` file is located at the `/etc/postgresql/` location. For example, if you are using PostgreSQL 17, the absolute url of `postgresql.conf` will be `/etc/postgresql/17/main/postgresql.conf`.

### What is the location of pg\_hba.conf file?

The `pg_hba.conf` file is located at the `/etc/postgresql/ `location. For example, if you are using PostgreSQL 17, the absolute url of `pg_hba.conf` would be `/etc/postgresql/17/main/pg_hba.conf `.

## Conclusion

This guide looked at the steps to install PostgreSQL on a Ubuntu server.

For the Redhat server, Checkout the [PostgreSQL installation on Redhat](https://devopscube.com/install-postgresql-redhat-linux/)

For the Amazon Linux server, Checkout the [PostgreSQL installation on Amazon Linux](https://devopscube.com/install-configure-postgresql-amazon-linux/)

Also, if you are using [Kubernetes](https://devopscube.com/kubernetes-tutorials-beginners/), check out my guide on [setting up PostgreSQL statefulset on Kubernetes](https://devopscube.com/deploy-postgresql-statefulset/)

As a DevOps engineer, it is essential to know the basic configurations involved in a Database.

If you are starting your DevOps engineer journey, look at my comprehensive [guide to becoming a DevOps engineer](https://devopscube.com/become-devops-engineer/).