13

Postgres container crash on launch with the following error message

(project) ➜  project git:(feature/62-api-custom-image-categories) ✗ docker-compose up postgres
Starting project_postgres_1 ... 
Starting project_postgres_1 ... done
Attaching to project_postgres_1
postgres_1        | FATAL:  database files are incompatible with server
postgres_1        | DETAIL:  The data directory was initialized by PostgreSQL version 9.5, which is not compatible with this version 9.6.3.
project_postgres_1 exited with code 1

What options do I have? II have updated image to the latest one

Portion of my docker-compose.yml

version: '2'

volumes:
  ipython_history: {}

services:
  postgres:
    image: mdillon/postgis
    ports:
     - "5432:5432"
    environment:
      - POSTGRES_DB=p_dev
      - POSTGRES_USER=p_user
      - POSTGRES_PASSWORD=password

Is it possible to convert the data or the only option is to delete the container (losing all its data) and then re-create it?

DmitrySemenov
  • 9,204
  • 15
  • 76
  • 121

5 Answers5

19

Additionally don't forget to clear the old pgdata volume:

docker volume ls and then docker volume rm <volume-name>

Otherwise your error will still remain.

See https://github.com/ckan/ckan/issues/4164#issuecomment-388069003

prograils
  • 2,248
  • 1
  • 28
  • 45
7

I got this error because i was always using the latest version of postgres by not defining a tag at the end. Then the latest version switched to 13 and the data was not compatible anymore. Maybe just adding a version at the end might help too. -> postgres:12

postgres:
image: postgres:12
restart: always
environment: 
  POSTGRES_USER: ${POSTGRES_USER}
  POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
ports:
  - "5432:5432"
volumes:
  - db_data:/var/lib/postgresql/data
Martin
  • 217
  • 3
  • 3
3

You are on time to save it, but you need to rollback to previous version, then:

docker exec -it <postgres-container-id> pg_dump db_name > local.dump.sql

Then, after checking that the dump is OK, empty the volume of the database, upgrade postgres and restore de dump:

https://www.postgresql.org/docs/9.1/static/backup-dump.html

Robert
  • 33,429
  • 8
  • 90
  • 94
  • Sorry if this sounds noobish, but I can't seem to find the precise way for rolling back to the previous version. My guess is that one should modify the `yml` file and run create again, but I'm afraid I might loose any unsaved data if I do so. Can you provide some detailed instructions ? – AdelaN Aug 18 '17 at 09:33
  • just change the name of the image back to the previous version in the docker-compose.yml file and then start again.. – Kashif Jul 02 '20 at 09:56
1

I know that I am 3 years late, but the following link answered this question for me. tianon/docker-postgres-upgrade does a pg_upgrade of the postgres data to the new postgres version you want to run. Was as easy as creating a service in a docker-compose file and running it.

Matey Aryeh
  • 116
  • 5
1

docker volume prune

This will remove all unused volumes. Unused here meaning any volume not used by at LEAST one container.

Although this worked for me, I'm concerned by what unused is really defining here. Does unused mean the volume exists and the image that was using it is destroyed? Or the image just isn't running anymore?

https://docs.docker.com/engine/reference/commandline/volume_prune/

Sam Autrey
  • 121
  • 1
  • 4