CircleCI 与 Postgres - 无法连接

CircleCI with Postgres - cannot connect

在我的 Circle 构建中,我尝试使用 Postgres 容器进行测试。测试数据库将自动创建,但 Python 未找到该数据库。这是我的 config.yml:

version: 2.1
​
orbs:
  python: circleci/python@0.2.1
​
jobs:
  build:
    executor: python/default
    docker:
      - image: circleci/python:3.8.0
        environment:
          - ENV: CIRCLE
      - image: circleci/postgres:9.6
        environment:
          POSTGRES_USER: circleci
          POSTGRES_DB: circle_test
          POSTGRES_HOST_AUTH_METHOD: trust
    steps:
      - checkout
      - python/load-cache
      - run:
          command: |
            python3 -m venv venv
            . venv/bin/activate
            pip3 install -r requirements.txt
      - python/save-cache
      - run:
          name: Running tests
          command: |
            . venv/bin/activate
            python3 ./api/manage.py test
      - store_artifacts:
          path: test-reports/
          destination: python_app
​
workflows:
  main:
    jobs:
      - build

在测试开始之前似乎一切都很好:

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"?

没有这样的文件夹:

ls -a /var/run/
.  ..  exim4  lock  utmp

它看起来像 psycopg2 defaults to using the UNIX socket,因此您需要在 connect() 调用中将数据库主机指定为 localhost

顺便提一下,您似乎在指定额外但不需要的 Python 图片。该作业的执行程序已设置为 python/default,因此下一张图片 circleci/python:3.8.0 似乎没有根据您粘贴的配置执行任何操作。

PGHOSTDATABASE_URL 配置中的一些更改解决了它:

 jobs:
  build:
    executor: python/default
    docker:
      - image: circleci/python:3.8.0
        environment:
          ENV: CIRCLE
          DATABASE_URL: postgresql://circleci@localhost/circle_test
      - image: circleci/postgres:9.6
        environment:
          PGHOST: localhost
          PGUSER: circleci
          POSTGRES_USER: circleci
          POSTGRES_DB: circle_test
          POSTGRES_HOST_AUTH_METHOD: trust
    steps:
      - checkout
      - python/load-cache
      - run:
          name: Wait for db to run
          command: dockerize -wait tcp://localhost:5432 -timeout 1m
      - run:
          command: |
            python3 -m venv venv
            . venv/bin/activate
            pip3 install -r requirements.txt
      - python/save-cache
      - run:
          name: Running tests
          command: |
            . venv/bin/activate
            cat /etc/hosts
            python3 ./django/gbookapi/manage.py test gbook.tests
      - store_test_results:
          path: test-reports.xml
      - store_artifacts:
          path: test-reports/
          destination: python_app

workflows:
  main:
    jobs:
      - build