Django can't connect to postgres.railway.internal
farismecinovic
PROOP

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",
)
}

$10 Bounty

8 Replies

farismecinovic
PROOP

7 months ago

16ebaf43-5b4e-42f4-97cb-4e7bda2842c8


brody
EMPLOYEE

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.


farismecinovic
PROOP

7 months ago

Exactly, it fails to migrate from my docker file? What should I do?


brody
EMPLOYEE

7 months ago

^ You will want to run any database migrations in a pre-deploy command.


farismecinovic
PROOP

7 months ago

Same for seeding the database?


brody
EMPLOYEE

7 months ago

Yes


farismecinovic
PROOP

7 months ago

Thanks so much! 🙂


saimprojects
HOBBY

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.


Loading...