Search code examples
pythondjangoherokuenvironment-variablesdjango-environ

Improperly Configured DATABASE_NAME env var


I made a .env file in the same directory as my settings.py file and have some environmental variables in there such as: secret_key, database_name, etc. However, it doesn't seem to be reading the database name correctly in the .env file. I feel like I followed the docs, but still get the improperly configured error when pushing to Heroku. It does work when running the server locally though.

settings.py

from pathlib import Path
import os
from datetime import timedelta

import environ
env = environ.Env()
environ.Env.read_env()

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Take environment variables from .env file
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env('DEBUG')

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

.env (example)

SECRET_KEY=django-insecure-vdihiodnsdkcndocndcndocdcoidcosjvodjv
DEBUG=True

DATABASE_NAME=vjiojjoj3oj3ioj3
DATABASE_USER=vdijvodivjdivfv
...

error

File "/app/project_name/settings.py", line 94, in <module>
    'NAME': env('DATABASE_NAME'),
  File "/app/.heroku/python/lib/python3.10/site-packages/environ/environ.py", line 175, in __call__
    return self.get_value(
  File "/app/.heroku/python/lib/python3.10/site-packages/environ/environ.py", line 371, in get_value
    raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the DATABASE_NAME environment variable

django-environ docs: https://github.com/joke2k/django-environ

EDIT: Okay it looks like pushing to Heroku with a .env file is not the way to go. Will be trying to link my github repo with heroku and configuring the vars in the settings. We'll see if that will do it.


Solution

  • As you may have guessed judging by your edit, Heroku doesn't support pushing .env files. This is because it uses an ephemeral filesystem.

    Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. [...] any files written will be discarded the moment the dyno is stopped or restarted. For example, this occurs any time a dyno is replaced due to application deployment and approximately once a day as part of normal dyno management.

    You'd be better off using Heroku's built-in config var support. Here are some examples:

    heroku config # View config vars
    heroku config:set TEST=test # Sets TEST to "test"
    heroku config:unset TEST # Reverses setting TEST
    heroku config:get TEST # Returns value of TEST