0

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!

3 Answers 3

3

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

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

Comments

0

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

5 Comments

You should also try to put dev-db in your environtment: DATABASE_URL....
Why not make it simpler and and set all references to db same, say db. That way you could define only one value, like links: "db", and call the service db:, etc... Try to debug it like that.
I did try, somehow it still says it cant connect to postgres... It is also worth mentioning that gunicron has no problem with connecting to postgres, async script do.
Why you have port 8000 when docker-compose ps? Are you sure your DATABASE_URL var in flask has the right values for postgres host and database?
Kamil answer is correct and it helped me, thank you for your trying though!
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.