Postgres

DANGER

Be aware of any operation regarding database could cause data lost. BACKUP FIRST!

Upgrade Postgres major version in Docker

In every major release of Postgres, there are always bunch of breaking changes introduced, regarding it's store engine, so it's impossible to upgrade Postgres by just changing the tag of postgres image from like :16 to :17.

A tool comes with postgres image called pg_upgrade is made to help you deal with the breaking changes.

The blow demonstrates upgrading Postgres service in docker compose from 16 to 17

1. Backup

Do a manual backup like the way we introduced in Backup and Restore.

2. Update compose

compose.yml
services:
  # ...
  postgres:
    image: postgres:16 
    image: postgres:17 
    container_name: affine_postgres

3. Restore data

Import your data by pg_restore

Use standalone Postgres

If you prefer to use a standalone Postgres, you can simply add DATABASE_URL to your .env file and remove the default one in docker-compose.yml.

.env
DATABASE_URL=postgresql://user:password@host:post/db 
compose.yml
services:
  # ...
  affine:
    environment:
      DATABASE_URL: ... 
  affine_migration:
    environment:
      DATABASE_URL: ... 
  # ...
  postgres: 
    # ...