Underoos
Underoos

Reputation: 5200

Unable to start postgres docker container from docker-compose

I am trying to start a postgresql docker container which is of version 10.5.

But before that I have used 9.6 version in the same docker-compose.yml file and there is no data populated in the database.

And now after changing the version of postgres container, I'm not able to run the docker-compose up. It is throwing the below error.

FATAL: database files are incompatible with server

DETAIL: The data directory was initialized by PostgreSQL version 9.6, which is not compatible with this version 10.5 (Debian 10.5-2.pgdg90+1)

This is how the docker-compose.yml file looks like.

version: '2'

services:

  postgres_service:
   container_name: postgresql_container
   restart: always
   image: postgres:10.5
   volumes:
     - postgres-data:/var/lib/postgresql/data
     - ./postgresql/init:/docker-entrypoint-initdb.d
   ports:
     - "5432:5432"
   environment:
     - POSTGRES_USER=admin
     - POSTGRES_PASSWORD=password
volumes:
  postgres-data:
    driver: local

Can someone please let me know where the issue is. Where am I making mistake? Do I need to delete any volumes before proceeding with the new postgres version?

I also have postgresql installed in my local.

postgres=# select version();
                                                               version                                                               
-------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 10.10 (Ubuntu 10.10-1.pgdg18.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0, 64-bit
(1 row)

Will this cause any issue?

Upvotes: 10

Views: 16216

Answers (2)

daniel costa
daniel costa

Reputation: 21

What worked for me was deleting pgdata folder inside the root of my project and running docker-compose build -d. It then showed me

/usr/local/bin/docker-entrypoint.sh: /docker-entrypoint-initdb.d/create-multiple-postgres-databases.sh: /bin/bash: bad interpreter: Permission denied

To fix it, I ran

chmod +x pg-init-scripts/create-multiple-postgresql-databases.sh

Notice that the .sh file name should match the one you have. And finally, docker-compose up -d.

Upvotes: 1

Toan Quoc Ho
Toan Quoc Ho

Reputation: 3378

The problem caused because the volume which your compose created to store your database still keep old data which initiated by PostgreSQL 9.6. That volume name is postgres-data which created when you use named volume on your docker-compose.yml. So simply to get rid of this, you can use some ways below:

  1. Using docker-compose command:

Run docker-compose down -v, this will stop all your container inside that compose and remove all named volume inside that compose.

You could take a look at docker-compose down command

  1. Using docker volume command:

Run docker volume ls to get list of current volumes on your machine, I think you will see your volume on that list too:

DRIVER              VOLUME NAME
local               postgres-data

Run docker volume rm postgres-data to remove that volume, if your container still running and you couldn't remove it then you can use -f to force remove it

Hope that helps!

Upvotes: 23

Related Questions