# Setting up test database credentials with CircleCI with Django is failing

**URL:** <https://discuss.circleci.com/t/setting-up-test-database-credentials-with-circleci-with-django-is-failing/42396>\
**Category:** Build Environment\
**Tags:** python, docker, django\
**Created:** [December 28, 2021, 4:54am UTC](https://discuss.circleci.com/t/setting-up-test-database-credentials-with-circleci-with-django-is-failing/42396 "2021-12-28T04:54:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanmw](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/jeanmw/32/28965_2.png) [@jeanmw](https://discuss.circleci.com/u/jeanmw)\
**Post date:** [December 28, 2021, 4:54am UTC](https://discuss.circleci.com/t/setting-up-test-database-credentials-with-circleci-with-django-is-failing/42396/1 "2021-12-28T04:54:01Z")

</div>

Hello,  
I am a bit new to this, so apologies if this question is something basic but I have been unable to get a concrete answer.  
I am trying to set up CircleCI with a Django app, python3, and I cannot figure out how to configure the test database credentials for circleci.  
The app is deployed on Heroku and I have the database credentials referenced as environmental variables in settings.py:

```auto
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': env('DATABASE_NAME'),
        'USER': env('DATABASE_USER'),
        'PASSWORD': env('DATABASE_PASSWORD'),
        'HOST': env('DATABASE_HOST'),
        'PORT': env('DATABASE_PORT'),
    }
}

```

My config.yml for circleci:

```auto
version: 2.1

jobs:
  tests:
    docker:
      - image: circleci/python:3.7
        environment:
          DATABASE_URL: postgresql://root@localhost/circle_test?sslmode=disable
      - image: circleci/postgres:9.6.2
        environment:
          POSTGRES_USER: root
          POSTGRES_DB: circle_test
    steps:
      - checkout
      - run: sudo chown -R circleci:circleci /usr/local/bin
      - run: sudo chown -R circleci:circleci /usr/local/lib/python3.7/site-packages
      - restore_cache:
          key: deps9-{{ .Branch }}-{{ checksum "requirements.txt" }}
      - run:
          name: Install Dependencies
          command: |
            pip3 install --user -r requirements.txt
      - save_cache:
          key: deps9-{{ .Branch }}-{{ checksum "requirements.txt" }}
          paths:
            - ".venv"
            - "/usr/local/bin"
            - "/usr/local/lib/python3.7/site-packages"
      - run: python3 manage.py test

workflows:
  main:
    jobs:
      - tests

```

I am not sure what is supposed to be used for the environmental variables for the test DB, and I can’t seem to find a concrete answer in the docs.  
I am getting `django.core.exceptions.ImproperlyConfigured: Set the DATABASE_NAME environment variable" does not exist` in my builds. The `circleci/postgres:9.6.2-alpine` step is failing, but I don’t know how to configure what the test database credentials should be. The postgres container build is failing as expected.

I’ve also tried adding what I think are the default values:

```auto
jobs:
  tests:
    docker:
      - image: circleci/python:3.7
        environment:
          DATABASE_NAME: circle_test
          DATABASE_URL: postgresql://root@localhost/circle_test?sslmode=disable
          DATABASE_USER: root
          DATABASE_PASSWORD: ''
          DATABASE_PORT: 5432
          DATABASE_HOST: postgresql://root@localhost/circle_test?sslmode=disable
      - image: circleci/postgres:9.6.2
        environment:
          DATABASE_NAME: circle_test
          DATABASE_USER: root
          DATABASE_URL: postgresql://root@localhost/circle_test?sslmode=disable
          DATABASE_PASSWORD: ''
          DATABASE_PORT: 5432
          DATABASE_HOST: postgresql://root@localhost/circle_test?sslmode=disable

```

and get `django.db.utils.OperationalError: could not translate host name "postgresql://root@localhost/circle_test?sslmode=disable" to address: Name or service not known`

---

<div class="post-metadata">

**Author:** ![jeanmw](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/jeanmw/32/28965_2.png) [@jeanmw](https://discuss.circleci.com/u/jeanmw)\
**Post date:** [December 28, 2021, 5:32am UTC](https://discuss.circleci.com/t/setting-up-test-database-credentials-with-circleci-with-django-is-failing/42396/2 "2021-12-28T05:32:29Z")

</div>

I found the issue - set DATABASE\_HOST to `localhost` in the config.yml.  
I feel that the default values should be documented somewhere, are they?

---

<div class="post-metadata">

**Author:** ![timvisher](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/timvisher/32/27678_2.png) [@timvisher](https://discuss.circleci.com/u/timvisher)\
**Post date:** [January 4, 2022, 10:10pm UTC](https://discuss.circleci.com/t/setting-up-test-database-credentials-with-circleci-with-django-is-failing/42396/3 "2022-01-04T22:10:19Z")

</div>

> [@jeanmw](#):
>
> I feel that the default values should be documented somewhere, are they?

Almost all the more common `circleci` images (_not_ necessarily the new `cimg` ones) are tiny wrappers around a public image. In this case that’s `postgres:latest` at least as of the time they built it (5 years ago).

I can’t find the source code online at the moment but if you’re willing to believe the [docker readme](https://hub.docker.com/r/circleci/postgres) this one’s as simple as:

```auto
FROM postgres:latest
ENV POSTGRES_DB=circle_test

```

I’m honestly not sure how many of the environment variables you’re setting on the PG container are making a difference. At least I haven’t found anything that suggests that they’re used by that image for configuration.

OTOH, on your python container it looks like maybe the Django [docs](https://docs.djangoproject.com/en/4.0/ref/settings/#host) are what you were looking for. Specifically:

> If you’re using PostgreSQL, by default (empty [`HOST`](https://docs.djangoproject.com/en/4.0/ref/settings/#std:setting-HOST)), the connection to the database is done through UNIX domain sockets (‘local’ lines in `pg_hba.conf` ). If your UNIX domain socket is not in the standard location, use the same value of `unix_socket_directory` from `postgresql.conf` . If you want to connect through TCP sockets, set [`HOST`](https://docs.djangoproject.com/en/4.0/ref/settings/#std:setting-HOST) to ‘localhost’ or ‘127.0.0.1’ (‘host’ lines in `pg_hba.conf` ). On Windows, you should always define [`HOST`](https://docs.djangoproject.com/en/4.0/ref/settings/#std:setting-HOST), as UNIX domain sockets are not available.

Glad you got yourself sorted! 🙂
