1

I am trying to figure out why I am getting an error about postgresql not running in my project. It is not connecting through Flask, nor when I try to access it through bash using the command docker-compose run postgres bash then psql returns the error:

bash-5.0# psql
psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

I tried running --force-recreate and to drop all abandoned orphan containers but this has not seemed to work. Similarly, I made sure it does not interfere with my local postgresql installation by uninstalling the local one and removing all files. I am pretty stumped on this.

Here is my docker-compose file:

version: "3"

services:

  webapp:
    build: .
    container_name: webapp
    ports:
      - "5000:5000"

  postgres:
    image: postgres:11-alpine
    container_name: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB=tmp
      - POSTGRES_USER=tmp
      - POSTGRES_PASSWORD=tmp_password
    volumes:  # Persist the db data
      - database-data:/var/lib/postgresql/data


volumes:
  database-data:

Any help is appreciated.

2
  • Your web app is django? Commented May 31, 2020 at 1:50
  • Flask. but I do not think that would make a different. Commented May 31, 2020 at 16:41

2 Answers 2

5

Please try the below docker-compose.yml in which depends_on, healthcheck and links are added as web service depends on db service.

version: "3"
services:
  webapp:
    build: .
    container_name: webapp
    ports:
      - "5000:5000"
    links:
      - postgres
    depends_on:
      - postgres

  postgres:
    image: postgres:11-alpine
    container_name: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB=tmp
      - POSTGRES_USER=tmp
      - POSTGRES_PASSWORD=tmp_password
    volumes:  # Persist the db data
      - database-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  database-data:

Sign up to request clarification or add additional context in comments.

5 Comments

Okay, I will give this a go. I had the depends on before but not the condition, healthcheck or links.
Got The Compose file './docker-compose.yml' is invalid because: services.webapp.depends_on contains an invalid type, it should be an array
Edited my answer, there was a typo
oddly, same issue. docker-compose run postgres bash then psql will give the same error. What is going on :( ? I also checked my local machine with netstat -anp|grep "5432" and nothing is using that port.
I am editing this. I saw that links_on is depreciated so I switched to using network
1

So I figured out the issue after trying to set up a mail server. Basically, I had installed postgresql on the local machine and then when I moved it to docker I forgot to uninstall it. When I uninstalled postgresql from the local machine the docker db now works.

1 Comment

actually turns out to be the same issue. hmmmm

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.