> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Install PostgreSQL on Ubuntu, Debian and AlmaLinux

> Install PostgreSQL on Ubuntu or Debian with sudo apt install postgresql, log in with sudo -u postgres psql, create a user and database, and allow remote access.

To install PostgreSQL on Ubuntu or Debian, run `sudo apt update` and `sudo apt install postgresql`. The service starts by itself. Open the database shell with `sudo -u postgres psql`, then create a login with `CREATE ROLE alex WITH LOGIN PASSWORD '…';` and a database with `CREATE DATABASE appdb OWNER alex;`. On AlmaLinux or Rocky, install `postgresql-server` and run `sudo postgresql-setup --initdb` first.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Beginner | 10 minutes | Ubuntu 22.04 / 24.04, Debian 12 / 13, AlmaLinux / Rocky 9 / 10 | sudo or root access |

## Before you start

* You need a user with `sudo` rights. If you are logged in as `root`, leave out `sudo` (but keep `-u postgres`).
* [Update your packages](https://help.freakhosting.com/portals/other/how-to-update-linux-packages.md) first.
* PostgreSQL is a database server, like MySQL, but with its own commands and tools. Use it if your app asks for PostgreSQL (for example many Django, Rails and Nextcloud setups).

## Which PostgreSQL version will I get?

| OS | Version from the OS repositories |
| - | - |
| Ubuntu 24.04 LTS | 16 |
| Debian 12 | 15 |
| Debian 13 | 17 |
| AlmaLinux / Rocky 9 | 13 (15, 16 and 18 as modules) |
| AlmaLinux / Rocky 10 | 16 (18 from 10.2) |

Sources: [Debian 13 release notes](https://www.debian.org/releases/trixie/release-notes/whats-new.en.html), [Red Hat PostgreSQL guide for RHEL 9](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/configuring_and_using_database_servers/using-postgresql_configuring-and-using-database-servers) and [for RHEL 10](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html/configuring_and_using_database_servers/using-postgresql).

## How to install PostgreSQL

> [tab] Ubuntu / Debian
>
> **1. Install the package**
>
> ```bash theme={null}
> sudo apt update
> sudo apt install postgresql
> ```
>
> **2. Check that it is running**
>
> ```bash theme={null}
> sudo systemctl status postgresql
> pg_lsclusters
> ```
>
> `pg_lsclusters` should show your cluster as `online`.
>
> Need a newer major version? The PostgreSQL project's own repository has it. Follow the [official Ubuntu instructions](https://www.postgresql.org/download/linux/ubuntu/):
>
> ```bash theme={null}
> sudo apt install -y postgresql-common
> sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
> sudo apt install postgresql-18
> ```

> [tab] AlmaLinux / Rocky
>
> **1. Install the package**
>
> ```bash theme={null}
> sudo dnf install postgresql-server
> ```
>
> On version 9 you can pick a newer release instead, for example `sudo dnf module install postgresql:16/server`.
>
> **2. Create the database cluster**
>
> ```bash theme={null}
> sudo postgresql-setup --initdb
> ```
>
> This creates the data folder `/var/lib/pgsql/data`.
>
> **3. Start it and enable it at boot**
>
> ```bash theme={null}
> sudo systemctl enable --now postgresql
> ```

## How to log in and create a user and database

PostgreSQL creates a Linux user and a database superuser, both called `postgres`. Locally it uses **peer** authentication: your Linux user name must match the database role, so you log in by switching to `postgres`.

**1. Open psql**

```bash theme={null}
sudo -u postgres psql
```

The prompt changes to `postgres=#`.

**2. Create a login role**

```sql theme={null}
CREATE ROLE alex WITH LOGIN PASSWORD 'Str0ng-Passw0rd!';
```

**3. Create a database owned by it**

```sql theme={null}
CREATE DATABASE appdb OWNER alex;
```

The owner has full rights on the database.

**4. Leave psql**

Type `\q` and press Enter.

## How to check it worked

Connect as the new user over TCP on the server itself:

```bash theme={null}
psql -h 127.0.0.1 -U alex -d appdb -c "SELECT current_user, current_database();"
```

```text theme={null}
 current_user | current_database
--------------+------------------
 alex         | appdb
(1 row)
```

If you see `Ident authentication failed` (possible on AlmaLinux or Rocky), open `pg_hba.conf`, set the last column of the `host` lines for `127.0.0.1/32` and `::1/128` to `scram-sha-256`, and restart PostgreSQL.

## Where are the PostgreSQL config files?

| File | Ubuntu / Debian | AlmaLinux / Rocky |
| - | - | - |
| Main settings | `/etc/postgresql/16/main/postgresql.conf` | `/var/lib/pgsql/data/postgresql.conf` |
| Who may connect | `/etc/postgresql/16/main/pg_hba.conf` | `/var/lib/pgsql/data/pg_hba.conf` |

Replace `16` with your version. The default port is `5432`, and the server only listens on `localhost` after installation.

## How to allow remote PostgreSQL connections

Only do this if another server must connect. For your own admin tools, use an SSH tunnel instead.

**1. Listen on the network**

In `postgresql.conf`, set:

```ini theme={null}
listen_addresses = '*'
```

**2. Allow one client in pg\_hba.conf**

Add a line at the end, using the client's IP:

```text theme={null}
host    appdb    alex    198.51.100.25/32    scram-sha-256
```

**3. Restart PostgreSQL**

```bash theme={null}
sudo systemctl restart postgresql
```

**4. Open port 5432 to that IP only**

UFW: `sudo ufw allow from 198.51.100.25 to any port 5432 proto tcp`. For firewalld, use a rich rule as in [set up firewalld](https://help.freakhosting.com/portals/other/how-to-set-up-a-firewall-with-firewalld.md).

> [!WARNING]
>
> Never add a `pg_hba.conf` line with `0.0.0.0/0` and `trust`. `trust` lets anyone log in without a password. Always use `scram-sha-256` and the narrowest IP range possible.

## How to back up a PostgreSQL database

```bash theme={null}
sudo -u postgres pg_dump -Fc appdb > appdb.dump
```

Restore it into an empty database:

```bash theme={null}
sudo -u postgres createdb -O alex appdb_restore
sudo -u postgres pg_restore -d appdb_restore < appdb.dump
```

`-Fc` writes PostgreSQL's compressed custom format, which `pg_restore` reads. For every database and all roles, use `sudo -u postgres pg_dumpall > all.sql`.

## Frequently asked questions

### Why does psql say "Peer authentication failed for user"?

You ran `psql -U alex` as a different Linux user. Add `-h 127.0.0.1` to log in with a password over TCP, or switch to the matching Linux user.

### How do I set a password for the postgres user?

Run `sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'X-Strong-Pass1';"`. You only need it if a tool must log in as `postgres` over TCP.

### Can PostgreSQL and MySQL run on the same server?

Yes. They use different ports (`5432` and `3306`) and do not conflict.

### Which is better, PostgreSQL or MySQL?

Use the one your app supports. WordPress, for example, needs MySQL or MariaDB. If your app supports both, pick the one you already know how to run and back up.

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every guide in this section.

> [card] [Install MariaDB or MySQL](https://help.freakhosting.com/portals/other/how-to-install-mysql-or-mariadb.md)
>
> The other common database server.

> [card] [Install Redis](https://help.freakhosting.com/portals/other/how-to-install-redis.md)
>
> Add a fast cache next to PostgreSQL.

> [card] [Deploy a Python app](https://help.freakhosting.com/portals/other/how-to-deploy-a-python-app-with-gunicorn.md)
>
> Run an app that uses PostgreSQL.

> [card] [Set up UFW](https://help.freakhosting.com/portals/other/how-to-set-up-a-firewall-with-ufw.md)
>
> Keep port 5432 closed to the internet.

***

> [!NOTE]
>
> **Checked against the official [Ubuntu Server PostgreSQL guide](https://ubuntu.com/server/docs/how-to/databases/install-postgresql/), [PostgreSQL download page](https://www.postgresql.org/download/linux/ubuntu/) and [Red Hat PostgreSQL guide](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html/configuring_and_using_database_servers/using-postgresql)** on 4 November 2026; install, role, database, pg\_dump and pg\_restore commands tested on Ubuntu 24.04 LTS with PostgreSQL 16. Commands can differ on other distributions or releases.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.