7 months ago
Hi, I have a problem connecting my Django App with Postgres DB within same project. I use reference ENV variable to my postgres and it wont work. I did the same way for my Redis instance in same project for my Celery worker, it works. But for postgres it can't connect:
Here is settings from my django app db connection:
DATABASES = {
"default": djdatabaseurl.config(
default=os.environ["DATABASEURL"], connmaxage=600, sslrequire=ENV == "production",
)
}
8 Replies
7 months ago
The private network is not available during the build; you will want to run any database migrations in a pre-deploy command.
7 months ago
^ You will want to run any database migrations in a pre-deploy command.
7 months ago
Yes
7 months ago
The issue is due to the environment variable name. In Django, the database configuration expects the variable to be named exactly as used in the code. Currently, DATABASEURL is used instead of the standard DATABASEURL format required by dj_database_url. To fix this, make sure your environment variable and settings match perfectly. For example:
DATABASES = { "default": dj_database_url.config( default=os.environ.get("DATABASEURL"), conn_max_age=600, ssl_require=(ENV == "production"), ) }
Ensure that your .env file has the correct value like:DATABASEURL=postgres://user:password@localhost:5432/mydb
Once both names are consistent, the connection will work.