> ## 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 & Configure PostgreSQL on Amazon Linux
- URL: https://devopscube.com/install-configure-postgresql-amazon-linux/
- Published: 2023-01-12T11:28:12.000Z
- Updated: 2025-03-14T15:00:39.000Z
- Author: devopscube
- Tags: AWS, HOW TO GUIDES, #Migrated-1741795015845, #wp, #wp-post, #Import 2025-03-12 15:57, #blog

If you want to install PostgreSQL on Amazon Linux (AWS ec2), this guide is for you. It covers installation, configuration, and enabling remote connection.

## Install PostgreSQL on Amazon Linux

Follow the steps given below to install the latest version of PostgreSQL on RedHat Linux

**Step 1:** Let's upgrade the system Yum packages.

```
sudo yum update -y
```

**Step 2:** PostgreSQL is part of the amazon extras library. Install the PostgreSQL amazon extras repository. At the time of writing, PostgreSQL 14 is the latest package available in the extras library.

```
sudo amazon-linux-extras enable postgresql14
```

**Step 3:** Install the PostgreSQL server.

```
sudo yum install postgresql-server -y
```

**Step 4:** Initialize the DB.

```
sudo postgresql-setup initdb
```

**Step 5:** Add the PostgreSQL service to the system startup.

```
sudo systemctl start postgresql
sudo systemctl enable postgresql
```

**Step 6:** Check the status of PostgreSQL using the following command.

```
sudo systemctl status postgresql
```

## Set Password For Postgres User

Now, let's set a password for the default Postgres user and secure it.

First login to the database using the following command.

```
sudo -u postgres psql
```

Set the password for the Postgres user so that we can use it to log in remotely. Replace `myPassword` with the required password.

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

## Enable Remote Connection For PostgreSQL on Amazon Linux

By default the remote PostgreSQL connection is disabled. You need to add the following configuration to enable remote connectivity.

Open the `postgresql.conf` file in the [vi editor](https://devopscube.com/linux-vi-editor-shortcuts-beginners/).

```
sudo vi /var/lib/pgsql/data/postgresql.conf
```

Locate the line that starts with "**listen\_addresses**". Uncomment and change it to "**listen\_addresses = '\*'**". This will allow connections from any IP address.

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

Next, open` /var/lib/pgsql/data/pg_hba.conf` file

```
sudo vi /var/lib/pgsql/data/pg_hba.conf
```

Add the following to the end of the file to allow client connections to all databases.

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

To apply all the changes, restart the PostgreSQL service using the following command.

```
sudo systemctl restart postgresql
```

Now, in the ec2 security group of the Amazon Linux ec2 server, allow **incoming traffic on port 5432**, which is the default port used by PostgreSQL

## Important PostgreSQL Server Configurations on Amazon Linux

The following table contains important PostgreSQL configurations on the Amazon Linux ec2 server.

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

## Conclusion

In this guide, we looked at the steps to install PostgreSQL on an Amazon Linux ec2 server.

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

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

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

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