Skip to content

Instantly share code, notes, and snippets.

@snopoke
Last active April 3, 2024 09:20
Show Gist options
  • Save snopoke/80e9a2e88ec493b7d4efae2578339f00 to your computer and use it in GitHub Desktop.
Save snopoke/80e9a2e88ec493b7d4efae2578339f00 to your computer and use it in GitHub Desktop.
docker pg-upgrade / migrate

Steps to upgrade the local postgres docker container / migrate to a new image

Based off https://gist.github.com/millerdev/547bac773483402554797e578fbd238f

Setup

export CONTAINER_NAME=open-chat-studio-db-1
export VOLUME_NAME=open-chat-studio_postgres_data

Check that these are correct:

docker ps | grep $CONTAINER_NAME
docker volume inspect $VOLUME_NAME

Dump the existing data from the running container

docker exec -i $CONTAINER_NAME pg_dumpall -U postgres | gzip > pg-backup.sql.gz

shutdown container

inv down

Backup old volume

Create new volume:

docker volume create --name "${VOLUME_NAME}_old"

Copy data from old to new:

docker run --rm -it -v $VOLUME_NAME:/from -v "${VOLUME_NAME}_old":/to alpine ash -c 'cd /from ; cp -av . /to'

Remove old volume:

docker volume rm $VOLUME_NAME

Update the docker compose file

Change the image tag / name

start new container

inv up

Restore the backed up data

gzip -cd pg-backup.sql.gz | docker exec -i $CONTAINER_NAME psql -U postgres
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment