Migrate PostgreSQL 13 to 16, both in Docker

Yesterday I came around to something I wanted to do for a long time: install watchtower on my local home server. So I did, and was reminded of an error I made years ago: I used postgres without any tag, so “latest” was used – which was 13 then, but is 16 now. So watchtower did what it should do and updated the container to PostgreSQL 16. This in itself is fine, but the data format has changed and so the container couldn’t start.

I looked for a convenient way to migrate the data, and I found an article using docker-compose. I found that too complicated, so here’s the (simple) procedure using only docker:

  • start both containers in parallel, each on a separate data directory
  • now export everything from the old version and directly pipe it into the new version:
    docker exec postgres-13 pg_dumpall -U postgres | docker exec -i postgres-16 psql -U postgres

Note the “-i” in the second docker exec command. It’s necessary, don’t forget it.

From all this, I learned that I shouldn’t use the tag “latest”. Not even implicitly. 😉