Home Using Postgres with Docker on Windows
Post
Cancel

Using Postgres with Docker on Windows

I was looking for something short about getting started with Docker Compose and I found this video: Docker Compose in 12 Minutes (Mar 14, 2017) - Learn how to use Docker Compose to run multi-container applications easily. This is the second video in this Docker series.

It is a nice video with a very simple example well explained. There is a project to play with on GitHub.

After this sample I did remember this post: ASP.Net Core Web API with Docker Compose, PostgreSQL and EF Core. and I like to try it. Here the source code.

But before that I wanted to start using PostgreSql with Docker. So I followed this post: Setup PostgreSQL on Windows with Docker and this one: Stop Installing Postgres on Your Laptop : Use Docker Instead.

So this was my first idea for a docker container:

1
docker run -p 5432:5432 --name postgres_db -e POSTGRES_PASSWORD=password -d postgres

with all databases data in the container.

Looking on Postgres Official Docker Image I found the use of mapping a host’s folder with postgres data folder inside the container in order to maintain the data if you delete the container or want to use that data with another container for another project.

So this was the evolution:

1
docker run -p 5432:5432 --name postgres_db -e POSTGRES_PASSWORD=password -v /c/Users/myuser/DockerProjects/postgres/postgres_db:/var/lib/postgresql/data -d postgres

But with Docker Toolbox for Windows 10 Home (my case), my postgres database didn’t work.

Looking with docker logs postgres_db I found the problem:

1
FATAL: data directory "/var/lib/postgresql/data" has wrong ownership

Here one solution:

1
2
3
4
5
6
7
8
postgresql:
  image: postgres:93
  volumes:
  - postgres:/var/lib/postgresql/data
  - ./postgresql/conf:/etc/postgresql/
...
volumes:
  postgres:

Here something similar:

To summarize: The workaround is to create a (local) volume with:

1
$ docker volume create --name data-postgresql --driver local

And the docker-compose.yml looks something like that:

1
2
3
4
5
6
7
8
services:
  pgsql:
    volumes:
      - data-postgresql:/var/lib/postgresql

volumes:
  data-postgresql:
    external: true

I also looked at Start a container with a volume in the Use Volume - Docker Documentation.

This is my solution for a test container:

1
2
3
$ docker volume create --name postgres-volume

$ docker run -p 5432:5432 --name postgres_db -e POSTGRES_PASSWORD=password -v postgres-volume:/var/lib/postgresql/data -d postgres

At the end I’m able to play with Postgres :-)

This post is licensed under CC BY 4.0 by the author.