Postgres: The data directory was initialized by PostgreSQL version 11, which is not compatible with this version 12.2.

Created on 17 Feb 2020  路  3Comments  路  Source: docker-library/postgres

Hello,

The following Dockerfile example works well in PostgreSQL 11 and the database is initialized with SQL files copied from the host init folder to the container /docker-entrypoint-initdb.d

FROM postgres:11.0-alpine
COPY ./init/ /docker-entrypoint-initdb.d/

But a similar Dockerfile for PostgreSQL 12 fails with the error below.

FROM postgres:alpine
COPY ./init/ /docker-entrypoint-initdb.d/

The data directory was initialized by PostgreSQL version 11, which is not compatible with this version 12.2.

What would be the best way to reproduce the behavior from v11 to v12?
Thank you

question

Most helpful comment

Note I let Docker create the volume automatically with Docker Compose like this. Would there be a better way to do that?

A named volume is fine. Docker will keep it around; use docker volume ls to see them. docker compose will reuse the volume by name (so you don't lose data). You have to migrate the data to a new volume (with something like tianon/docker-postgres-upgrade) when you move major postgres versions or delete the old volume if you don't have any useful data there.

All 3 comments

Are you running them with the same volume? You cannot use the same database volume from 11 to 12. You must do an upgrade to move between major versions. https://github.com/tianon/docker-postgres-upgrade is a good docker example of how to move from one to the next.

Here is upstream's documentation: https://www.postgresql.org/docs/current/upgrading.html

Duplicate of #37, #618.

Thanks for the hint about the volume! I think I was running them on different volumes but I am not able to reproduce the issue today so maybe I just got confused and forgot to remove the v11 volume before running v12.

Note I let Docker create the volume automatically with Docker Compose like this. Would there be a better way to do that?

version: "3.1"
services:
    db:
        container_name: mobydq-db
        restart: always
        image: mobydq-db
        build:
            context: ./db
        volumes:
            - db:/var/lib/postgresql/data
        env_file:
            - ./.env
        networks:
            - network

networks:
    network:

volumes:
    db:

Note I let Docker create the volume automatically with Docker Compose like this. Would there be a better way to do that?

A named volume is fine. Docker will keep it around; use docker volume ls to see them. docker compose will reuse the volume by name (so you don't lose data). You have to migrate the data to a new volume (with something like tianon/docker-postgres-upgrade) when you move major postgres versions or delete the old volume if you don't have any useful data there.

Was this page helpful?
0 / 5 - 0 ratings