Damian Busz
Damian Busz

Reputation: 1848

Cant connect to postgres database inside docker container

My problem is I have a script that should scrap data and put it inside postgres database, however it has a problem to reach out postgres container.

When I run my docker-compose here is the result:

           Name                     Command               State              Ports             
------------------------------------------------------------------------------------------
orcsearch_dev-db_1   docker-entrypoint.sh postgres    Up      0.0.0.0:5432->5432/tcp       
orcsearch_flask_1    gunicorn wsgi:application  ...   Up      0.0.0.0:80->80/tcp, 8000/tcp 

We can clearly see that postgres is on 5432 port.

This is my python script database setting:(ofcourse I removed password for obvious reason)

class App():
    settings = {
        'db_host': 'db',
        'db_user': 'postgres',
        'db_pass': '',
        'db_db': 'orc',
    }
    db = None
    proxies = None

and this is my docker-compose.yml

version: '2'

services:
  flask:
    build:
      context: ./backend
      dockerfile: Dockerfile.dev
    volumes:
      - ./backend:/app
      - pip-cache:/root/.cache
    ports:
      - "80:80"
    links:
      - "dev-db:db"
    environment:
      - DATABASE_URL=postgresql://postgres@db:5432/postgres
    stdin_open: true
    command: gunicorn wsgi:application -w 1 --bind 0.0.0.0:80 --log-level debug --reload
    networks:
      app:
        aliases:
          - flask

  dev-db:
    image: postgres:9.5
    ports:
      - "5432:5432"
    networks:
      app:
        aliases:
          - dev-db

volumes:
  pip-cache:
    driver: local

networks:
  app:

When going into exec flask bash(inside flask container) and running script command I get this error:

psycopg2.OperationalError: 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"?

Obviously there is postgres running on this port and I cant figure out what wrong do I do. Any help would be nice!

Upvotes: 0

Views: 1174

Answers (3)

Kamil Sokołowski
Kamil Sokołowski

Reputation: 333

Probably you are using DSN instead of URI, and PostgreSQL thinks that "db" is not a host because it's hard to tell if "db" is host or path to socket. To fix it, use URI instead of DSN if you use >=9.2 version of PostgreSQL.

Example of URI: postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]

https://www.postgresql.org/docs/9.2/static/libpq-connect.html#LIBPQ-CONNSTRING

Upvotes: 3

Stefano
Stefano

Reputation: 5076

I think that the problem is related to the fact that you're using the network and the link together. Try remove the link and change the postgres address to dev-db or change the alias to:

networks:
  app:
    aliases:
      - dev-db
      - db

Upvotes: 0

Andrija Ćeranić
Andrija Ćeranić

Reputation: 1733

In your App class it should be 'db_host': 'dev-db', Seems like that hostname is exposed, not db.

Upvotes: 0

Related Questions