我在Docker compose上使用Django和PostgreSQL,但是Django-test无法访问数据库

iyfjxgzm  于 2022-12-03  发布在  Docker
关注(0)|答案(1)|浏览(143)

我练习Writing your first Django app, part 5,这是django的测试部分,我的环境是:

  • Django 4.0
  • Python 3.9语言
  • 数据库PostgreSQL 14.2
  • Docker合成

此外,通过.pg_service.conf和. pgpass配置与PostgreSQL的连接。
docker-compose.yml

version: "3.9"
services:
  web:
    build:
      context: .
    container_name: web
    restart: unless-stopped
    tty: true
    working_dir: /opt/apps
    volumes:
      - .:/opt/apps/
      - .pgpass:/root/.pgpass
      - .pg_service.conf:/root/.pg_service.conf
    entrypoint: "./entrypoint.sh"
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - "8000:8000"
    depends_on:
      - db
  db:
    image: postgres
    container_name: db
    environment:
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    ports:
      - ${DB_PORT}:5432
    volumes:
      - postgres_data:/var/lib/postgresql/data
volumes:
  postgres_data:
networks:
  django-tutorial:

编写.env文件

DB_USER=postgres
DB_PASSWORD=postgres
DB_HOST=db
DB_PORT=5432

.pg_服务.conf

[DB]
host=db
user=postgres
dbname=django_tutorial
port=5432

.pgpass

db:5432:postgres:postgres:postgres

当使用上述配置执行docker-compose up -d并执行python manage.py migrate时,迁移将顺利执行,并且数据库可访问。
但是,当我尝试像在python manage.py test polls中那样运行测试时,我得到了以下错误。(测试与本文开头链接中的代码相同。)我需要对测试DB进行任何额外的配置吗?

docker compose exec web python manage.py test polls                                                                                                                                                                                                     ✔  15:20:03  
Found 1 test(s).
Creating test database for alias 'default'...
/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py:323: 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.
  warnings.warn(
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 225, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 203, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, 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"?

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 318, in _nodb_cursor
    with super()._nodb_cursor() as cursor:
  File "/usr/local/lib/python3.9/contextlib.py", line 119, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 656, in _nodb_cursor
    with conn.cursor() as cursor:
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 284, in cursor
    return self._cursor()
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 260, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 225, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 203, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.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"?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/apps/manage.py", line 22, in <module>
    main()
  File "/opt/apps/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/test.py", line 24, in run_from_argv
    super().run_from_argv(argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 414, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 460, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/test.py", line 68, in handle
    failures = test_runner.run_tests(test_labels)
  File "/usr/local/lib/python3.9/site-packages/django/test/runner.py", line 1000, in run_tests
    old_config = self.setup_databases(
  File "/usr/local/lib/python3.9/site-packages/django/test/runner.py", line 898, in setup_databases
    return _setup_databases(
  File "/usr/local/lib/python3.9/site-packages/django/test/utils.py", line 220, in setup_databases
    connection.creation.create_test_db(
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/creation.py", line 63, in create_test_db
    self._create_test_db(verbosity, autoclobber, keepdb)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/creation.py", line 199, in _create_test_db
    with self._nodb_cursor() as cursor:
  File "/usr/local/lib/python3.9/contextlib.py", line 119, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 344, in _nodb_cursor
    with conn.cursor() as cursor:
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 284, in cursor
    return self._cursor()
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 260, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 224, in connect
    conn_params = self.get_connection_params()
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 162, in get_connection_params
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the NAME or OPTIONS['service'] value.
vdzxcuhz

vdzxcuhz1#

目前不支持使用服务名进行测试。我怀疑这就是python manage.py test不工作的原因。
在此查看票证https://code.djangoproject.com/ticket/33685
如果这是问题的原因,python manage.py runserver应该可以工作。如果需要使用测试,请尝试旧的配置。

相关问题