Django can't connect to postgres.railway.internal

farismecinovic
PRO

9 days 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

1 Replies

farismecinovic
PRO

9 days ago

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


9 days ago

The private network is not available during the build; you will want to run any database migrations in a pre-deploy command.


farismecinovic
PRO

9 days ago

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


9 days ago

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


farismecinovic
PRO

9 days ago

Same for seeding the database?


9 days ago

Yes


farismecinovic
PRO

9 days ago

Thanks so much! 🙂


saimprojects
HOBBY

7 days 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.


Django can't connect to postgres.railway.internal - Railway Help Station