1 Upgrade Postgres (Major Versions)
deon edited this page 2024-10-23 04:55:27 +00:00

Upgrading postgres is done via a backup/restore operation.

The easiest way to upgrade is (example, upgrade from Postgresl 15 to Postgres 17):

  • Stop all services
  • Move existing database into a directory, eg: mv postgres postgres.15
  • Update the docker-compose file
  postgres_old:
    image: postgres:15-alpine
    deploy:
      resources:
        limits:
          memory: 512M
    environment:
      POSTGRES_DB: ${DB_DATABASE:-clrghouz}
      POSTGRES_USER: ${DB_USERNAME:-clrghouz}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    shm_size: 1g
    volumes:
    - ${VOL_PREFIX:-/srv/docker/clrghouz}/postgres.15:/var/lib/postgresql/data

 postgres:
    image: postgres:17-alpine
    deploy:
      resources:
        limits:
          memory: 512M
    environment:
      POSTGRES_DB: ${DB_DATABASE:-clrghouz}
      POSTGRES_USER: ${DB_USERNAME:-clrghouz}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    shm_size: 1g
    volumes:
    - ${VOL_PREFIX:-/srv/docker/clrghouz}/postgres.17:/var/lib/postgresql/data

Start the environment, and stop all the containers except the two database environments (postgres_15,postgres).

Export the database out of the old environment and into the new, where:

  • 2b5fae8b7394 is the container ID of the old database (postgres.15)
  • 08cb53dfa922 is the container ID of the new database (postgres)

docker exec -t 2b5fae8b7394 pg_dumpall -U ${DB_DATABASE:-clrghouz} | docker exec -i 08cb53dfa922 psql -U ${DB_DATABASE:-clrghouz}

The old DB can now be stopped, and the old DB configuration in the docker-compose file can be commented out.

In case something goes pear shaped, the old database is still there if you need to revert to it (it wasnt affected by this operation).