Upgrade PostgreSQL version from 10 to 11 running on Docker Compose
Situation
FIrst, I've run PostgreSQL10 using following docker-compose.yml.
code:docker-compose.yml
version: '3'
services:
db:
image: postgres:10-alpine
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=XXX
- POSTGRES_USER=XXX
- POSTGRES_DB=XXX
volumes:
- pgdata:/var/lib/postgresql/data
container_name: db
volumes:
pgdata:
In this case, pgdata docker voume has been created.
code:bash
$ docker volume ls
local <path of dir>_pgdata
Complication
I want to upgrade PostgreSQL version to 11 keeping volume data.
Write image: postgres:11-alpine on docker-compose.yml , run docker-compose up, and get following error.
code:bash
$ docker-compose up
db | 2019-04-12 01:32:37.744 UTC 1 FATAL: database files are incompatible with server db | 2019-04-12 01:32:37.744 UTC 1 DETAIL: The data directory was initialized by PostgreSQL version 10, which is not compatible with this version 11.2.
db exited with code 1
Solution
Save current data
code:bash
$ pg_dump -h <Server Hostname> -U <DB user name> -Fc -f <dump file path> <DB name>
Remove volume
code:bash
$ docker volume ls
$ docker volume rm <path of dir>_pgdata
Run the container of PostgreSQL 11
Change image: to postgres:11-alpine
Run docker-compose up
Restore saved data
code:bash
$ pg_restore -h <Server Hostname> -U <DB user name> -d <DB name> -c --if-exists -j <number of jobs> <dump file path>