首页 > 解决方案 > 如何修复 Unix 域套接字“/var/run/postgresql/.s.PGSQL.5432”上的连接?运行 bitbucket 管道时出错

问题描述

我正在尝试使用 bitbucket 管道运行测试,但不幸的是,我无法连接到 postgresql。

所以我尝试添加rm -rf /tmp/.s.PGSQL.5432/到我的 bitbucket-pipeline.yml 但运行测试时没有任何改变

这是我得到的错误

+ python manage.py test
/usr/local/lib/python3.7/site-packages/django/db/backends/postgresql/base.py:265: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the first PostgreSQL database instead.
  RuntimeWarning
nosetests --with-coverage --cover-package=accounts,rest_v1, property --verbosity=1
Creating test database for alias 'default'...
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 174, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
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"?

bitbucket-pipelines.yml

image: python:3.6.2
pipelines:
  default:
    - step:
        script:
          - pip install -r requirements.txt
          - python manage.py test

  branches:
    develop:
    - step:
        caches:
        - node
        script:
        - pip install -r requirements.txt
        - python manage.py test

设置.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'db_name',
        'USER': 'user_name',
        'PASSWORD': 'db_password',
    }
}

我希望 bitbucket 管道能够毫无问题地运行测试,尤其是数据库连接问题

标签: pythondjangopostgresqlbitbucketbitbucket-pipelines

解决方案


您将需要使用services密钥并执行与definition服务中类似的操作。

pipelines:
    default:
        - step:
            image: node
            script:
              - npm install
              - npm test
            services:
              - postgres

definitions:
  services:
    postgres:
      image: postgres
      environment:
        POSTGRES_DB: pipelines
        POSTGRES_USER: test_user
        POSTGRES_PASSWORD: test_user_password

来源: https ://community.atlassian.com/t5/Bitbucket-questions/How-do-I-use-Postgres-in-Bitbucket-Pipelines/qaq-p/461910


推荐阅读