title: I use Django with PostgreSQL on docker compose, but django-test can't access database
body: <p>I practice <a href="https://docs.djangoproject.com/en/4.0/intro/tutorial05/" rel="nofollow noreferrer">Writing your first Django app, part 5</a>, it's django test section.
  And my environment is:</p>
  <ul>
  <li>Django 4.0</li>
  <li>Python 3.9</li>
  <li>Database PostgreSQL 14.2</li>
  <li>Docker compose</li>
  </ul>
  <p>In addition, the connection to PostgreSQL is configured via .pg_service.conf and .pgpass.</p>
  <p>docker-compose.yml</p>
  <pre><code>version: &quot;3.9&quot;
  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: &quot;./entrypoint.sh&quot;
      command: python manage.py runserver 0.0.0.0:8000
      ports:
        - &quot;8000:8000&quot;
      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:
  </code></pre>
  <p>compose .env file</p>
  <pre><code>DB_USER=postgres
  DB_PASSWORD=postgres
  DB_HOST=db
  DB_PORT=5432
  </code></pre>
  <p>.pg_service.conf</p>
  <pre><code>[DB]
  host=db
  user=postgres
  dbname=django_tutorial
  port=5432
  </code></pre>
  <p>.pgpass</p>
  <pre><code>db:5432:postgres:postgres:postgres
  </code></pre>
  <p>When <code>docker-compose up -d</code> is executed with the above configuration and <code>python manage.py migrate</code> is executed, the migration is performed no problems and the database is accessible.</p>
  <p>However, when I try to run the test as in <code>python manage.py test polls</code>, I get the following error.
  (The test is the same as the code in the link at the beginning of this article.)
  Do I need any additional configuration for the test DB?</p>
  <pre><code>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 &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py&quot;, line 244, in ensure_connection
      self.connect()
    File &quot;/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
      return func(*args, **kwargs)
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py&quot;, line 225, in connect
      self.connection = self.get_new_connection(conn_params)
    File &quot;/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
      return func(*args, **kwargs)
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py&quot;, line 203, in get_new_connection
      connection = Database.connect(**conn_params)
    File &quot;/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py&quot;, 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 &quot;/var/run/postgresql/.s.PGSQL.5432&quot;?
  
  
  The above exception was the direct cause of the following exception:
  
  Traceback (most recent call last):
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py&quot;, line 318, in _nodb_cursor
      with super()._nodb_cursor() as cursor:
    File &quot;/usr/local/lib/python3.9/contextlib.py&quot;, line 119, in __enter__
      return next(self.gen)
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py&quot;, line 656, in _nodb_cursor
      with conn.cursor() as cursor:
    File &quot;/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
      return func(*args, **kwargs)
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py&quot;, line 284, in cursor
      return self._cursor()
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py&quot;, line 260, in _cursor
      self.ensure_connection()
    File &quot;/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
      return func(*args, **kwargs)
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py&quot;, line 244, in ensure_connection
      self.connect()
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/utils.py&quot;, line 91, in __exit__
      raise dj_exc_value.with_traceback(traceback) from exc_value
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py&quot;, line 244, in ensure_connection
      self.connect()
    File &quot;/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
      return func(*args, **kwargs)
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py&quot;, line 225, in connect
      self.connection = self.get_new_connection(conn_params)
    File &quot;/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
      return func(*args, **kwargs)
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py&quot;, line 203, in get_new_connection
      connection = Database.connect(**conn_params)
    File &quot;/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py&quot;, 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 &quot;/var/run/postgresql/.s.PGSQL.5432&quot;?
  
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;/opt/apps/manage.py&quot;, line 22, in &lt;module&gt;
      main()
    File &quot;/opt/apps/manage.py&quot;, line 18, in main
      execute_from_command_line(sys.argv)
    File &quot;/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py&quot;, line 446, in execute_from_command_line
      utility.execute()
    File &quot;/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py&quot;, line 440, in execute
      self.fetch_command(subcommand).run_from_argv(self.argv)
    File &quot;/usr/local/lib/python3.9/site-packages/django/core/management/commands/test.py&quot;, line 24, in run_from_argv
      super().run_from_argv(argv)
    File &quot;/usr/local/lib/python3.9/site-packages/django/core/management/base.py&quot;, line 414, in run_from_argv
      self.execute(*args, **cmd_options)
    File &quot;/usr/local/lib/python3.9/site-packages/django/core/management/base.py&quot;, line 460, in execute
      output = self.handle(*args, **options)
    File &quot;/usr/local/lib/python3.9/site-packages/django/core/management/commands/test.py&quot;, line 68, in handle
      failures = test_runner.run_tests(test_labels)
    File &quot;/usr/local/lib/python3.9/site-packages/django/test/runner.py&quot;, line 1000, in run_tests
      old_config = self.setup_databases(
    File &quot;/usr/local/lib/python3.9/site-packages/django/test/runner.py&quot;, line 898, in setup_databases
      return _setup_databases(
    File &quot;/usr/local/lib/python3.9/site-packages/django/test/utils.py&quot;, line 220, in setup_databases
      connection.creation.create_test_db(
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/base/creation.py&quot;, line 63, in create_test_db
      self._create_test_db(verbosity, autoclobber, keepdb)
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/base/creation.py&quot;, line 199, in _create_test_db
      with self._nodb_cursor() as cursor:
    File &quot;/usr/local/lib/python3.9/contextlib.py&quot;, line 119, in __enter__
      return next(self.gen)
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py&quot;, line 344, in _nodb_cursor
      with conn.cursor() as cursor:
    File &quot;/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
      return func(*args, **kwargs)
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py&quot;, line 284, in cursor
      return self._cursor()
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py&quot;, line 260, in _cursor
      self.ensure_connection()
    File &quot;/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
      return func(*args, **kwargs)
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py&quot;, line 244, in ensure_connection
      self.connect()
    File &quot;/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py&quot;, line 26, in inner
      return func(*args, **kwargs)
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py&quot;, line 224, in connect
      conn_params = self.get_connection_params()
    File &quot;/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py&quot;, 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.
  
  </code></pre>