
# Install PostgreSQL Ubuntu 24.04: Setup, Remote Access and Backups

**To install PostgreSQL Ubuntu 24.04 style with current versions, add the official PGDG repository with the script from `postgresql-common`, then run `sudo apt install postgresql-18`.** The package creates a cluster, starts it on port 5432 and enables it at boot. From there you create an application role and database, and, only if another server needs it, open remote access over SSL with `scram-sha-256` and a ufw rule for one IP. As of Nov 20, 2026 the current releases are PostgreSQL 18.7 and the brand-new 19.0, while Ubuntu's own archive stops at 16.


Prompt for your AI agent:


```text
Create an s2-4 server with Ubuntu 24.04 on 0x.cloud. Show the dry_run price and wait for my "yes". Install PostgreSQL 18 from the official apt.postgresql.org repository, create a role appuser with a password and a database appdb, keep it listening on 127.0.0.1 only and set up a nightly pg_dump cron job with 7-day retention. Send me the connection string.
```


## Requirements and plan

| Workload | Plan |
|---|---|
| A bot, small site or n8n database | s1-2: 1 vCPU, 2 GB RAM, 30 GB NVMe, $7.99/mo |
| Several apps, databases up to tens of GB | s2-4: 2 vCPU, 4 GB RAM, 60 GB NVMe, $15.99/mo |
| Analytics and heavy queries | s4-8: 4 vCPU, 8 GB RAM, 120 GB NVMe, $31.99/mo |

All plans run on NVMe, which matters more for a database than CPU clock speed. If data outgrows the disk, you can attach extra NVMe at $0.08/GB per month.

How to choose: PostgreSQL is fastest when the working set fits in memory (`shared_buffers` plus the OS cache). A few-GB database behind one app is fine on 2 GB of RAM; once several services hit it or data grows into tens of GB, you want 4 GB or more. All plans and add-ons are on [VPS pricing](https://0x.cloud/pricing). You can start on hourly billing: s2-4 is $0.109520 an hour ($15.99 / 730 × 5) and never more than $15.99 in a calendar month. Setup is $0.


[Deploy to 0x.cloud](https://0x.cloud/cabinet/create?plan=s2-4&app=postgresql)


## Prepare the server

Open the web console of the server in your cabinet (an agent runs the same commands with `run_command`) and update packages:

```bash
sudo apt update && sudo apt upgrade -y
```

Turn on the firewall with SSH only. The database port stays closed unless you open it for a specific client later:

```bash
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status
```

## Install PostgreSQL Ubuntu 24.04 packages from PGDG

**Step 1. Add the official repository.** This is the automatic setup from postgresql.org. It imports the signing key and writes the source file for your Ubuntu codename (`noble`):

```bash
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
```

Press Enter when it asks to continue.

**Step 2. Install a specific major version.** Name it explicitly; the unversioned `postgresql` package would pull the newest branch, which is now 19:

```bash
sudo apt install -y postgresql-18
```

Check the result:

```bash
pg_lsclusters
sudo -u postgres psql -c "SELECT version();"
```

`pg_lsclusters` lists version, port, status and data directory. You want `18 main 5432 online`.

**Step 3. Create a role and a database.** Applications shouldn't connect as the `postgres` superuser:

```bash
sudo -u postgres createuser --pwprompt appuser
sudo -u postgres createdb -O appuser appdb
```

The password is stored as a scram-sha-256 hash, the default since PostgreSQL 14. Test a password login over TCP:

```bash
psql -h 127.0.0.1 -U appuser -d appdb
```

**Step 4. Basic memory settings.** For s2-4, edit `/etc/postgresql/18/main/postgresql.conf`:

```ini
shared_buffers = 1GB
effective_cache_size = 3GB
```

That's roughly 25% and 75% of RAM, the usual starting point. Apply with `sudo systemctl restart postgresql`.

## Secure remote access over SSL

If your app runs on the same server, skip this section: `127.0.0.1` is the safest connection there is. Incoming connections from the internet don't reach 0x.cloud servers, so the rest of this section is for a server with a public address. For ad hoc admin work from your laptop, an SSH tunnel beats an open port:

```bash
ssh -L 5433:127.0.0.1:5432 user@SERVER_IP
# in another terminal: psql -h 127.0.0.1 -p 5433 -U appuser appdb
```

When another server (say, an app at `203.0.113.10`) needs the database:

1. Listen on the network in `postgresql.conf`:

```ini
listen_addresses = '*'
```

2. Append one rule to `/etc/postgresql/18/main/pg_hba.conf`:

```
hostssl  appdb  appuser  203.0.113.10/32  scram-sha-256
```

`hostssl` rejects unencrypted connections, `/32` means exactly one address, and `scram-sha-256` verifies the password without sending it in the clear. Ubuntu's packages enable SSL out of the box with a self-signed certificate.

3. Open 5432 for that client only and restart (ufw rules explained):

```bash
sudo ufw allow from 203.0.113.10 to any port 5432 proto tcp
sudo systemctl restart postgresql
```

Client connection string:

```
postgresql://appuser:PASSWORD@SERVER_IP:5432/appdb?sslmode=require
```

Check: on the server, `sudo -u postgres psql -c "SHOW ssl;"` prints `on` and `sudo ss -tlnp | grep 5432` shows `0.0.0.0:5432`; from the client, `psql "postgresql://appuser@SERVER_IP:5432/appdb?sslmode=require" -c "SELECT 1;"` returns `1` after the password prompt. From any other IP the connection should time out.

For full certificate checks (`sslmode=verify-full`), issue a Let's Encrypt certificate for the database hostname and point `ssl_cert_file` and `ssl_key_file` at it.

## Start on boot

The `postgresql` service is enabled during installation. Confirm after a reboot:

```bash
sudo systemctl is-enabled postgresql
sudo reboot
# after reconnecting
pg_lsclusters
```

## Upgrading PostgreSQL

Minor releases (18.7 to 18.8) arrive with regular `sudo apt update && sudo apt upgrade`, and the service restarts itself. For a major upgrade, take a backup, then use Ubuntu's `pg_upgradecluster`:

```bash
sudo apt install -y postgresql-19
sudo pg_dropcluster 19 main --stop
sudo pg_upgradecluster 18 main
pg_lsclusters
sudo pg_dropcluster 18 main
```

Installing 19 creates an empty cluster on port 5433, which you drop. `pg_upgradecluster` then moves your data and settings into 19/main on port 5432. Remove the old cluster only after your app checks out.

## Nightly backups with pg_dump and cron

Create a backup folder and edit the `postgres` user's crontab:

```bash
sudo mkdir -p /var/backups/pg && sudo chown postgres:postgres /var/backups/pg
sudo crontab -u postgres -e
```

Add:

```
0 2 * * * pg_dump -Fc appdb > /var/backups/pg/appdb-$(date +\%F).dump
30 2 * * * pg_dumpall --globals-only > /var/backups/pg/globals-$(date +\%F).sql
0 4 * * * find /var/backups/pg -type f -mtime +7 -delete
```

`pg_dump -Fc` writes a compressed custom-format dump you restore with `pg_restore -d appdb file.dump`. `pg_dumpall --globals-only` saves roles and passwords, which per-database dumps don't include. The `%` must be escaped in crontab; more on scheduling in [cron basics](https://0x.cloud/kb/servers/cron).

Copy dumps off the server with rsync or rclone. On 0x.cloud you can also turn on automatic server backups (7 daily copies, +20% of the plan price); see [VPS backup options](https://0x.cloud/kb/servers/vps-backup).

## Common errors

| Error | Cause and fix |
|---|---|
| `Peer authentication failed for user "appuser"` | Socket connections require a matching Linux user. Add `-h 127.0.0.1` |
| `no pg_hba.conf entry for host "…", SSL off` | The client isn't using SSL or its IP doesn't match. Add `sslmode=require` and check the rule |
| `Connection refused` from another server | `listen_addresses` isn't set or ufw blocks the port |
| `password authentication failed` | Wrong password. Reset it: `sudo -u postgres psql -c "\password appuser"` |
| A new version shows up on port 5433 | That's a second cluster. Check `pg_lsclusters` |
| Config changes don't apply | `listen_addresses` needs a restart; `pg_hba.conf` only needs a reload |

Official reference: [PostgreSQL downloads for Ubuntu](https://www.postgresql.org/download/linux/ubuntu/). 0x.cloud servers are in Warsaw, and support answers in about 30 minutes on average during business hours (06:00–20:00 UTC). Need a database with an instant API, auth and a dashboard? See self-hosting Supabase. For caching and queues next to PostgreSQL, most stacks add [Redis](https://0x.cloud/deploy/redis).

## FAQ

**Which PostgreSQL version should I install on Ubuntu 24.04?**
For production, PostgreSQL 18 (18.7 as of Nov 20, 2026) has a year of bug fixes behind it. PostgreSQL 19 was released on Nov 19, 2026; it's a fine choice for new projects, but many teams wait for 19.1 before moving existing databases.

**Why not just run apt install postgresql?**
Ubuntu 24.04's own archive ships PostgreSQL 16 and never gets newer major versions. The PGDG repository at apt.postgresql.org is maintained by the PostgreSQL project and carries every supported version.

**How do I allow remote connections to PostgreSQL?**
Set listen_addresses = '*' in postgresql.conf, add a hostssl line for the client IP with scram-sha-256 to pg_hba.conf, allow port 5432 in ufw for that IP only, then restart PostgreSQL.

**Where are the PostgreSQL config files on Ubuntu?**
In /etc/postgresql/18/main/: postgresql.conf, pg_hba.conf and pg_ident.conf. Data lives in /var/lib/postgresql/18/main and logs in /var/log/postgresql.

**How do I install PostgreSQL 17 instead of 18?**
After adding the PGDG repository, run sudo apt install postgresql-17. Paths change accordingly, for example /etc/postgresql/17/main. Every step in this guide works the same.


[Deploy to 0x.cloud](https://0x.cloud/cabinet/create?plan=s2-4&app=postgresql)



[Create server: s2-4, $15.99/mo](https://0x.cloud/cabinet/create?plan=s2-4)
