Skip to content

Instantly share code, notes, and snippets.

@TaQuanMinhLong
Last active September 28, 2024 04:00
Show Gist options
  • Select an option

  • Save TaQuanMinhLong/0f926ceddca62b087ad56c4b6c8198eb to your computer and use it in GitHub Desktop.

Select an option

Save TaQuanMinhLong/0f926ceddca62b087ad56c4b6c8198eb to your computer and use it in GitHub Desktop.
Upgrade postgres version from docker-compose

Make sure postgres is running at the current old version

image: postgres:16-alpine

Pull new image if needed

docker pull postgres:17-alpine

Make sure to map tmp folder from docker postgres to the host

For example:

volumes:
  - ./database/postgres:/var/lib/postgresql/data
  - ./database/tmp:/tmp

From docker postgres old version, backup data with the following command

Entering the container

docker exec -it postgres bash

Exporting data

pg_dumpall -U postgres > /tmp/dump.sql

Make sure to have dump.sql in the host directory!

ls ./database/tmp

Stop database service

docker compose down

Rename as the old postgres binary for backup

For example, if the binary is located in /data/database/postgres

sudo mv -r  /data/database/postgres  /data/database/pg16

Edit docker-compose.yml to latest version, then up the compose

docker compose up -d

Restore your data

Enter the postgres container

docker exec -it postgres bash

Import data back to the database

psql -U postgres -d postgres < /tmp/dump.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment