Could not translate host name “db” to address using Postgres, Docker Compose and Psycopg2

后端 未结 6 681
不思量自难忘°
不思量自难忘° 2021-02-01 16:39

In one folder I have 3 files: base.py, Dockerfile and docker-compose.yml.

base.py:

import psycopg2

conn = psycopg2.connect(\"dbname=\'b         


        
6条回答
  •  青春惊慌失措
    2021-02-01 17:30

    If you're using docker-compose first add this line to your .yml:

    environment:
      - "POSTGRES_HOST_AUTH_METHOD=trust"
    

    After that you start only you db docker:

    docker-compose up db
    

    It should start normally with a message like:

    Recreating db... done
    Attaching to db
    db_1           | ********************************************************************************
    db_1           | WARNING: POSTGRES_HOST_AUTH_METHOD has been set to "trust". This will allow
    db_1           |          anyone with access to the Postgres port to access your database without
    db_1           |          a password, even if POSTGRES_PASSWORD is set. See PostgreSQL
    db_1           |          documentation about "trust":
    db_1           |          https://www.postgresql.org/docs/current/auth-trust.html
    db_1           |          In Docker's default configuration, this is effectively any other
    db_1           |          container on the same system.
    db_1           | 
    db_1           |          It is not recommended to use POSTGRES_HOST_AUTH_METHOD=trust. Replace
    db_1           |          it with "-e POSTGRES_PASSWORD=password" instead to set a password in
    db_1           |          "docker run".
    db_1           | ********************************************************************************
    

提交回复
热议问题