Setting up test database credentials with CircleCI with Django is failing

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:

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:

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:

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

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?

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 this one’s as simple as:

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 are what you were looking for. Specifically:

If you’re using PostgreSQL, by default (empty 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 to ‘localhost’ or ‘127.0.0.1’ (‘host’ lines in pg_hba.conf ). On Windows, you should always define HOST , as UNIX domain sockets are not available.

Glad you got yourself sorted! :slight_smile: