Database URL different and not working
I'm struggling to set up my Django project, it can't use the DATABSE_URL value the way it normally would. I get this error:
Traceback (most recent call last):
File "/opt/venv/lib/python3.10/site-packages/django/db/backends/base/base.py", line 275, in ensure_connection
self.connect()
File "/opt/venv/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
return func(args, **kwargs)
File "/opt/venv/lib/python3.10/site-packages/django/db/backends/base/base.py", line 256, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/venv/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
return func(args, kwargs)
File "/opt/venv/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 277, in get_new_connection
connection = self.Database.connect(conn_params)
File "/opt/venv/lib/python3.10/site-packages/psycopg2/init.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "postgres.railway.internal" to address: Name or service not known
I am using the following code to connect to the DB:
db_url = os.environ["DATABASE_URL"]
DATABASES = {"default": dj_database_url.parse(db_url, engine='django.db.backends.postgresql')}
This works great in another project I've deployed. But when I investigate the DATABASE_URL value in that project, it's different. Rather than using "postgres.railway.internal" it uses "roundhouse.proxy.rlwy.net"
Is this a recent change to database config on railway side, or am I just doing something wrong?
13 Replies
Project ID:
dc4f0585-2e8b-4706-bd9a-ff136257ec90
dc4f0585-2e8b-4706-bd9a-ff136257ec90
yes, but default railway will use the private network for the database URL, if you need to access it locally you will need to use the public url
Got it, so just use the public url value? If by locally you mean connecting to it remotely from my local instance, I'm not doing that. This occurs on the actual deploy. I may be misunderstanding what you mean by locally though.
ah okay, you never directly stated if this error was happening on railway, or locally so I took a guess based on how most people encounter this error.
is the Postgres database in the same project as the Django service?
Sorry for the lack of clarity. Yes, the postgres DB is in the same project. I added the variable by using the reference function
is your Django service on the V2 runtime? check your service settings
Where would I find that?
service settings
Its on Legacy
Solution
switch to v2
I'll give it a shot
That did it! Thanks.
no problem!