#2121: Updating PostgreSQL 14 → 18 for Swarmica
Отредактирована: сегодняThis guide is applicable for Swarmica versions 6.1.2 and higher.
Instructions
- Create a backup of the database:
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:
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.
- Stop Swarmica services:
cd /root/swarmica
docker compose down
- 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.
docker volume rm swarmica_swarmica_postgres_data
- Create a new empty volume:
docker volume create swarmica_swarmica_postgres_data
- Edit
docker-compose.yml. Open the file in a text editor and changeimageandvolumein thepostgresservice as shown below:
<...>
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
<...>
- Start only PostgreSQL 18:
docker compose up -d postgres
Wait for the healthy status:
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.
- Copy the database backup from step 1 into the container:
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
- Restore data from the backup:
docker exec -it swarmica-postgres sh -c 'psql -U $POSTGRES_USER -d postgres < /tmp/full_backup_pg14.sql'
- Start the remaining Swarmica services:
cd /root/swarmica
docker compose up -d
- Perform final procedures:
docker exec --env-file .env -it swarmica-django-1 python manage.py post_start
- Delete the temporary dump from the container:
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.