# Updating PostgreSQL 14 → 18 for Swarmica
:::caution
This guide is applicable for Swarmica versions 6.1.2 and higher.
:::

## Instructions

1. Create a backup of the database:

```bash
cd /root/swarmica

# Full logical dump of all databases and roles inside the container
docker exec -it --env-file .env swarmica-postgres sh -c 'pg_dumpall -U $POSTGRES_USER > /backups/full_backup_pg14.sql'

# Copy the dump to the host
docker cp swarmica-postgres:/backups/full_backup_pg14.sql /root/swarmica/
```

Check the backup:

```bash
ls -lh /root/swarmica/full_backup_pg14.sql
grep -c "CREATE DATABASE" /root/swarmica/full_backup_pg14.sql
```

If the second command returned ≥ 1 — the dump contains databases, everything is fine.

2. Stop Swarmica services:

```bash
cd /root/swarmica
docker compose down
```

3. Delete the old data volume:

PostgreSQL 18 **will not start** on a volume initialized by version 14, the container will crash with an incompatibility error. Therefore, the volume must be deleted and created again.

```bash
docker volume rm swarmica_swarmica_postgres_data
```

4. Create a new empty volume:

```bash
docker volume create swarmica_swarmica_postgres_data
```

5. Edit `docker-compose.yml`. Open the file in a text editor and change `image` and `volume` in the `postgres` service as shown below:

```yaml
<...>
postgres:
  image: postgres:18-bookworm                          # ← CHANGED: was postgres:14.2-bullseye
  container_name: swarmica-postgres
  shm_size: 1gb
  command: postgres -N 200 -c work_mem=8MB -c temp_buffers=16MB -c shared_buffers=1GB -c track_activity_query_size=16384
  healthcheck:
    test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
    interval: 10s
    timeout: 5s
    retries: 5
    start_period: 10s
  restart: always
  volumes:
    - swarmica_postgres_data:/var/lib/postgresql:z     # ← CHANGED: was /var/lib/postgresql/data
<...>
```

6. Start only PostgreSQL 18:

```bash
docker compose up -d postgres
```

Wait for the `healthy` status:

```bash
docker compose ps
docker compose logs postgres | tail -20
```

The logs should contain `database system is ready to accept connections`. This means that `initdb` created a new PostgreSQL 18 cluster in the fresh volume.

7. Copy the database backup from step 1 into the container:

```bash
docker cp /root/swarmica/full_backup_pg14.sql swarmica-postgres:/tmp/full_backup_pg14.sql
docker exec -it swarmica-postgres ls -lh /tmp/full_backup_pg14.sql
```

8. Restore data from the backup:

```bash
docker exec -it swarmica-postgres sh -c 'psql -U $POSTGRES_USER -d postgres < /tmp/full_backup_pg14.sql'
```

9. Start the remaining Swarmica services:

```bash
cd /root/swarmica
docker compose up -d
```

10. Perform final procedures:

```txt
docker exec --env-file .env -it swarmica-django-1 python manage.py post_start
```

11. Delete the temporary dump from the container:

```bash
docker exec -it swarmica-postgres rm /tmp/full_backup_pg14.sql
```

Keep the dump on the host (`/root/swarmica/full_backup_pg14.sql`) for several days as insurance. Delete it only after complete confidence in stability.