We’re going to backup our database, stop the old Postgres, start the new one, and then restore our database on the new version. It sounds a little more complicated than it really is.
If you’re curious on how Postgres backup and restore works, check out this earlier post.
- Start your old version of Postgres in Docker.
docker-compose up
(alternatively, you can only start Postgres with “docker-compose up db”, where “db” is your Postgres service name in docker-compose.yml).
- Backup your data.
pg_dump -d my_database > my_backup_script.sql
- Stop your Docker container.
ctrl+c in the terminal window where you ran docker-compose up
- Delete your old Postgres “volume”. The volume is essentially the virtual hard drive for your Postgres server.
docker-compose down -v
(note this will delete all data tied to your docker volume)
- Change the Postgres version (usually specified in a
docker-compose.yml
file). - Start your Docker containers and tell Docker to rebuild the containers.
docker-compose up --build
- Create the database.
rake db:create
ORpsql
->CREATE DATABASE my_database
- Restore your database schema and data.
psql -h localhost -d my_database > my_backup_script.sql