Question

Exception("DATABASE_URL environment variable not defined") When running local server

I built a django app on my local machine, and then used this tutorial to deploy it with app platform (https://docs.digitalocean.com/tutorials/app-deploy-django-app/) The deployment was successful.

However, now when I try to run my django app locally to continue development, I’m getting the following error:

…/settings.py", line 95, in <module> raise Exception(“DATABASE_URL environment variable not defined”) Exception: DATABASE_URL environment variable not defined

Current settings in settings.py: DEVELOPMENT_MODE = os.getenv(“DEVELOPMENT_MODE”, “False”) == “True”

DEBUG = os.getenv(“DEBUG”, “False”) == “True”

I am unsure of how to reset my settings.py file so that it points to a local sqlite3 db for development.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Jon Friesen
DigitalOcean Employee
DigitalOcean Employee badge
December 16, 2022

👋 @43e88fede83e446f9e392debd41d3c

Giving that tutorial a quick read over, it looks like you’ll need to set an environment variable to indicate that you’re in a development environment.

So in your dev env, set DEVELOPMENT_MODE=true.

This way the code will use a sqlite db instead of looking for a postgres instance, snippet from the tutorial:


if DEVELOPMENT_MODE is True:
    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.sqlite3",
            "NAME": os.path.join(BASE_DIR, "db.sqlite3"),
        }
    }

The dev env defaults to being disabled just above the snippet above:


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DEVELOPMENT_MODE = os.getenv("DEVELOPMENT_MODE", "False") == "True"

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up