Unable to start postgres docker container from docker-compose
Asked Answered
J

2

10

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?

Jaclynjaco answered 27/9, 2019 at 21:12 Comment(8)
Do I need to delete any volumes before proceeding with the new postgres version?: Yes, you should.Maleate
Yes. I just deleted /var/lib/postgresql/10/main/. But still getting the same issue. Is that the volume that is supposed to be deleted? I don't see any /var/lib/postgresql/data in my machine.Jaclynjaco
Seem like you're mapping your volume with a folder on your project. postgres-data this one, remove it because when ever you restart your container all files inside that folder will be mapped into your postgresql containerMaleate
I don't see any postgres-data folder in my machine. Not sure where it is. Not in project folder or in root - /Jaclynjaco
I mean outside of the container. When you use Docker, your machine is host OS. Container running and mapping a folder inside container with a folder on host OS. Simply take a look at a folder which contain your docker-compose.yml file then you will see that folder at that placeMaleate
That's what I'm saying. There is no such folder postgres-data in the location.Jaclynjaco
I even uninstalled everything related to postgresql.Jaclynjaco
Sorry, my mistake. I will give you an answer belowMaleate
M
23

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!

Maleate answered 28/9, 2019 at 13:9 Comment(2)
Thanks for the tip! Although using only docker-compose down -v didn't fix it for me. I additionaly needed to restart docker desktop and manually delete "db" folder inside my docker ".data" folder.Proportionable
Yeah @PaulMark, the reason it didn't fix for you is because you're using host volume, it will be persistent until you delete the bind folder of your host computer. That's why it works when you delete ".data" folder Paul. Hope this could give you a little bit information for your case.Maleate
A
1

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.

Apivorous answered 1/3, 2022 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.