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

a year 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": dj_database_url.config(

default=os.environ["DATABASE_URL"],

conn_max_age=600,

ssl_require=ENV == "production",

)

}

$10 Bounty

8 Replies

farismecinovic
PROOP

a year ago

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


a year 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

a year ago

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


a year ago

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


farismecinovic
PROOP

a year ago

Same for seeding the database?


a year ago

Yes


farismecinovic
PROOP

a year ago

Thanks so much! 🙂


saimprojects
HOBBY

a year 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.


Welcome!

Sign in to your Railway account to join the conversation.

Loading...