Upgrading Postgres in Docker (and keep your data!)

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.

  1. 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).
  2. Backup your data.
    pg_dump -d my_database > my_backup_script.sql
  3. Stop your Docker container.
    ctrl+c in the terminal window where you ran docker-compose up
  4. 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)
  5. Change the Postgres version (usually specified in a docker-compose.yml file).
  6. Start your Docker containers and tell Docker to rebuild the containers.
    docker-compose up --build
  7. Create the database.
    rake db:create
    OR psql -> CREATE DATABASE my_database
  8. Restore your database schema and data.
    psql -h localhost -d my_database > my_backup_script.sql

Your Thoughts?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s