Search code examples
pythondjango-rest-frameworkdocker-composedeploymentuwsgi

Having issue with "ENVIRONMENT VARIABLE" when deploying a virtual web server


I am creating a Django rest framework backend project based on an Udemy Course that I have taken and everything seems fine during the development of the project. The project works well when I'm using a local host device.

When I'm trying to create a web server for the backend project, still I'm following the Udemy course learning one by one. Suddenly, I encountered a problem where my environment variable/virtual variable could not be detected, and automatically a blank string was created and assigned as the whole environment variable/virtual variable

Here is the error occurred in the Command Prompt:

time="2023-09-26T12:07:55+08:00" level=warning msg="The \"DB_NAME\" variable is not set. Defaulting to a blank string."

time="2023-09-26T12:07:55+08:00" level=warning msg="The \"DB_USER\" variable is not set. Defaulting to a blank string."

time="2023-09-26T12:07:55+08:00" level=warning msg="The \"DB_PASS\" variable is not set. Defaulting to a blank string."

time="2023-09-26T12:07:55+08:00" level=warning msg="The \"DJANGO_SECRET_KEY\" variable is not set. Defaulting to a blank string."

time="2023-09-26T12:07:55+08:00" level=warning msg="The \"DJANGO_ALLOWED_HOSTS\" variable is not set. Defaulting to a blank string."

There is another one error occured which is:

Error: Database is uninitialized and superuser password is not specified.
recipe-app-api-db-1     |        You must specify POSTGRES_PASSWORD to a non-empty value for the
recipe-app-api-db-1     |        superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
recipe-app-api-db-1     |
recipe-app-api-db-1     |        You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
recipe-app-api-db-1     |        connections without a password. This is *not* recommended.
recipe-app-api-db-1     |
recipe-app-api-db-1     |        See PostgreSQL documentation about "trust":
recipe-app-api-db-1     |        https://www.postgresql.org/docs/current/auth-trust.html
recipe-app-api-db-1     |

Can someone explain what happened actually and how can i fix this error? Step-by-step instructions would be nice to have.


Solution

  • This depends on where you are hosting the web server at

    On Heroku there should be an option in settings called "Config Vars", which you can fill out the name and values for enviroment variables for

    If you are hosting the project on your own machine or virtually anything where you utilize the command line then you can either make a .env file and read it from the program, or set the env variable itself in the OS with the export command, for example export DB_NAME=importantdatabase