PostgreSQL :服务器是否在主机“localhost”(::1) 上运行并接受错误

问题描述

Celery 无法在我的 docker 服务中连接到 Postgresql 并收到此错误

Could not connect to server: Cannot assign requested address
celery_1       |        Is the server running on host "localhost" (::1) and accepting
celery_1       |        TCP/IP connections on port 5432?

虽然 Postgresql数据库工作正常,但我能够执行操作 只是在 celery 的情况下。

我现在在这个 celery 服务中有 2 个案例

  celery:
    build:
      context: ./
      dockerfile: Dockerfile
    command: celery -A sampleproject worker -l info
    environment:
    - POSTGRES_USER=${POSTGRES_USER}
    - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
    - POSTGRES_DB=${POSTGRES_DB}
    - POSTGRES_HOST=${POSTGRES_HOST}
    - POSTGRES_PORT=${POSTGRES_PORT}

    volumes:
      - .:/usr/src/app/

    depends_on:
     - database
     - app
     - redis

当我在 celery 环境中传递所有 Postgresql 变量 时,它的工作正常。当我删除它们时它不起作用。为什么会发生?我该如何解决这个问题?这样我就可以用正确的方式运行芹菜

解决方法

在搜索时发现了这个。我会将我的解决方案分享给发现此问题的其他人。

对我来说,问题是 ${POSTGRES_HOST} 主机设置为 localhost。它应该设置为 database