DEV Community

Andy Allison
Andy Allison

Posted on

Postgres, Docker and Windows

This has had me tearing my hair out for a fair old chunk of time.

The main issue was I wanted to spin up a simple container with postgres in it for a little dev project I'm playing with. Historically I've used Heroku but I wanted to be a good modern dev and slam some containers about the boat.

The initial article I found had a few decent bits in it but I did run into issues straight away.

The first one was that I needed to sort the Docker for windows sharing out.
cloud app

Docker Settings (TaskBar Tray) > Resources > File Sharing > C:
This then requested my admin user and password to allow permissions.

Next I set up the docker-compose.yml file. This in itself caused a number of permissions problems.

version: "3"
services:
#  Create a service named db.
  db:
#   Use the Docker Image postgres. This will pull the newest release.
    image: "postgres"
#   Give the container the name my_postgres. You can changes to something else.
    container_name: "localpg"
#   Setup the username, password, and database name. You can changes these values.
    environment:
      - POSTGRES_USER=john
      - POSTGRES_PASSWORD=pwd0123456789
      - POSTGRES_DB=mydb
#   Maps port 54320 (localhost) to port 5432 on the container. You can change the ports to fix your needs.
    ports:
      - "54320:5432"
#   Set a volume some that database is not lost after shutting down the container.
#   I used the name postgres-data but you can changed it to something else.
    volumes:
      - postgresql-volume:/var/lib/postgresql/data
volumes: 
  postgresql-volume:
    external: true

The main errors being

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 20
selecting default shared_buffers ... 400kB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
2019-10-17 16:09:15.251 UTC [77] FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
2019-10-17 16:09:15.251 UTC [77] HINT:  The server must be started by the user that owns the data directory.
child process exited with exit code 1
initdb: removing contents of data directory "/var/lib/postgresql/data"

What I found with this was you needed to create a volume for docker as it just doesn't seem to play nice with the windows file system at its rawest.

This involved running this command.

$ docker volume create --name=postgresql-volume

And making sure this was in my compose file

volumes: 
  postgresql-volume:
    external: true

Top comments (1)

Collapse
 
vyrru5 profile image
VyRru5

Perfect, my friend as this problem, we are fixed with this method, thanks a lot.
I think it was docker agent in windows wtih low permissions.