Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 671
Underoos Avatar asked Sep 27 '19 21:09

Underoos


People also ask

Why is my PostgreSQL database not working in Docker Compose?

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:

How to connect pgadmin4 to Postgres in Docker?

The easiest way to set that up to allow pgadmin4 container to communicate with the postgres container is to add pgadmin as a service in your docker-compose.yml file. Update your docker-compose.yml file to contain the following configuration:

What is Docker Compose and how to use it?

Putting it shortly, docker compose isolates each of the containers network. This is done by adding them to the <myapp>_default which is a newly created network, where <myapp> is the name of the directory. Under this each of the containers is added via it’s name.

What version of PostgreSQL do I need to run a container?

We will be using the latest version 14.1 of PostgreSQL. The default bullseye version of Postgres docker image is 130 MB whereas the alpine one for the same version is 78 MB which is a lot smaller. To simply run the container using the Postgres 14.1 alpine image we can execute the following command:


1 Answers

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!

like image 91
Toan Quoc Ho Avatar answered Oct 08 '22 20:10

Toan Quoc Ho