Error connecting to PostgresDB within the same service
Hello Everyone,
I hope you're all well.
I've been having issues connecting to the database spinned from the template at: https://railway.app/template/3jJFCA, from my python fast api app on the same network.
I can connect to the database fine using table plus and tcp config details.
However my instance with python crashes when it attempts to connect, no matter what variables I try. I get the error:
psycopg2.OperationalError: could not translate host name "xxx" to address: Name or service not known
Any thoughts or ideas on what could be wrong?
Thanks in advance for your help.
Solution:Jump to solution
now remove it and try adding
connect_timeout=5
to the pool options, for a less hacky option51 Replies
Project ID:
593bc68b-e170-43c8-8b0b-c85983a0c936
Project ID is: 593bc68b-e170-43c8-8b0b-c85983a0c936
will do some tests and get back to you
i kept getting
SSL is not enabled on the server
but after appending ?sslmode=disable
onto the reference variable, everything worked.
these are the variables i changed on the pgvector service
and this is how i referenced those variables on the test service
Unfortunately my issue may not be SSL related. The error I get when I look at the logs is:
But just incase, I appended to my variable and after deployment, the issues still persist
Okay, additional info: I am now currently able to connect fine from my python fastapi web service if I set the HOST and Socket variable to the public networking / TCP configs. (Just like how I'm able to connect fine from outside using table plus).
But although they are within the same network, when I set the HOST to the Private networking url, it doesn't seem to work.
AFAIK private networking required host to listen on ipv6 and not ipv4. Probably thatβs the problem
so just so we are on the same page, you are deploying postgres from the new official railway template?
https://railway.app/template/postgres
and not the old plugin system?
does your postgres service show that it has a volume attached to it?
Yes that's correct. Although not exactly the official template but a slight variant found here: https://railway.app/template/3jJFCA. I deployed it after watching the official video intro on Next gen databases at https://www.youtube.com/watch?v=UJEc6EcqXFA
And yes, It has a volumn attached to it and not using the old plugin system.
Railway
YouTube
How We Built Next-Gen Databases
As part of Launch Week 01, Product Engineer JR introduces Next-Gen Databases, now available on Railway.
For more information, visit the Launch Week landing page: https://railway.app/launch-week-01
can you show me a screenshot of the pgvectors setting page
show me the variables you have set in the service that connect to it
of course hiding anything sensitive
As mentioned earlier, when I set the db host to and the port to 4266X, I am able to connect fine. But when I change the host to I get the error.
are you building your app with a dockerfile?
The second service is a python fastapi application and in the code, here is how the defined variables are used:
And:
DB_HOST, DB_PORT, ... etc are the variables being used
When I set the host variable to this, it works fine.
note, you want the pool min to be zero, a pool min of 1 or above will cause issues in docker environments such as railway
though that is not the issue right now
show me the service variables for the python service please, the ones in the variables tab, not in code
Okay noted. Will change, although at the moment the service is built from github
I am guessing you are connecting via the wrong port. If you are using the private dns name you should be connecting on the port 5432
Here is the variable tab with the relevant keys
Let me try that and see. Private dns with port 5432
set
DB_HOST
to ${{pgvector.RAILWAY_PRIVATE_DOMAIN}}
Okay, I'll redeploy with this just to be safe
let me know how that goes, i have some more ideas if this doesn't work
Evaluates to the same thing. Currently re-deploying. I'll let you know in about 8mins cuz that's how the long the build takes π
Really need to get around to dockerizing it to speed up the process
railway caches by default
building with a dockerfile in a lot of cases can increase build time vs building with nixpacks
are you installing 200 python deps or somethig lol
hahaha, there are quite a bunch in their. Including some OCR ones
are they all needed? a lot of time people will pip freeze every python package on their computer into their projects requirements.txt file
Yes, they are all needed. I've made sure to cut down on the ones not needed
Here is my .toml file:
what are you talking about, that is by no means a lot
Unstructured is quite chunky to be fair
ive seen 200 line requirements.txt files, you are good
Its completed building
But unfortunately issue persists π
it hasnt been 8 minutes!
as a dirty hack, chuck in a
sleep(5)
after the def and before the return lines
this is because the private networks dns resolver is not available right when the app startsThat's what I thought too before, but after restarting a bunch of times it crashes
For now, I'm fine with using the TCP configs as it works, but I thought it'd be better in the future to disable public access to the DB and why I was keen on getting the private networking to work.
it do be better like that, but let me know if that sleep(5) changes anything
That actually worked! π you!
sleep(5) for the win
Solution
now remove it and try adding
connect_timeout=5
to the pool options, for a less hacky optionAlright, also changed the minimum pool to 0, as advised earlier:
Re-deploying now..
All Good!
awesome
Thanks for your help mate! Appreciate you
I really hope they can fix the private networking initialisation times, you wouldn't believe how many help threads it causes with wacky errors, through this thread did throw me for a loop at first
I cut down build times by creating a base image with all deps. And the app pulls from it. Really shorten the time.
Will probably try this too some time in the future.
thank you for the trains! π
Wow, just spent a few hours trying to solve the exact same issue. I was using the public URL until I realised hat was going in my bill as egress, maybe it could go in the doc somewhere? Also, the django sample app uses the public network which also through me off since I thought this would be best practise?