DATABASE_URL key error during deployment

My first deploy fails on the following error. I have tried to use runtime secrets, build-time secrets (link), and passing the DATABASE_URL with a --build-arg flag. I carefully followed the Django Deployment Guide. Within my .env file I included the DATABASE_URL variable with the connection string provided during the fly launch. I’m not sure what else to try?

 => ERROR [7/7] RUN python manage.py collectstatic --noinput                                                                    0.9s
------
 > [7/7] RUN python manage.py collectstatic --noinput:
#10 0.887 Traceback (most recent call last):
#10 0.887   File "/usr/local/lib/python3.10/site-packages/environ/environ.py", line 387, in get_value
#10 0.887     value = self.ENVIRON[var_name]
#10 0.887   File "/usr/local/lib/python3.10/os.py", line 680, in __getitem__
#10 0.887     raise KeyError(key) from None
#10 0.887 KeyError: 'DATABASE_URL'

Hi @zip!

The .env file is used on your local development. In production, you should set the secrets with:

fly secrets set DATABASE_URL

However, if you are set up a Postgres DB during the fly launch, the DATABASE_URL should be set already. You can check all your secrets with:

fly secrets list
NAME            DIGEST                  CREATED AT
DATABASE_URL    cc999c17fa021988        2023-02-07T19:48:55Z
SECRET_KEY      e0a6dbbd078004f7        2023-02-07T19:47:33Z

The issue happens during the build time: it’s required to have a default value for DATABASE_URL.

You can set a fallback value in your Dockefile, for example:

# Dockerfile
...
# Set DATABASE_URL for building purposes
ENV DATABASE_URL "sqlite://:memory:"  # <- Updated
# Set SECRET_KEY for building purposes
ENV SECRET_KEY "non-secret-key-for-building-purposes"
RUN python manage.py collectstatic --noinput

I’m not sure why this was not happening before, but for now, that will do it :slight_smile:

1 Like

Hello @katia,

Thank you for your reply. I had already tried the various things you are suggesting, but your post clarified a few things for me and now I have been attempting to try again while following your instructions. So I destroyed the app and the database in order to try to start fresh, but each time I execute the fly launch command it fails to create the database, and the database gets stuck in some sort of limbo state. So I destroy it and try again, but it hasn’t worked yet.

I am hoping it will work eventually. I have found that some of the flyctl commands seem to work at some times and not at others. This has been especially true for me when trying to create or attach databases.

Thanks - it is working now! I tried some different server locations, and one of them worked. The key that got it to work was this line:

1 Like