R
Railwayβ€’14mo ago
Nim

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.
No description
Solution:
now remove it and try adding connect_timeout=5 to the pool options, for a less hacky option
Jump to solution
51 Replies
Percy
Percyβ€’14mo ago
Project ID: 593bc68b-e170-43c8-8b0b-c85983a0c936
Nim
Nimβ€’14mo ago
Project ID is: 593bc68b-e170-43c8-8b0b-c85983a0c936
Brody
Brodyβ€’14mo ago
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
DATABASE_URL=postgres://${{POSTGRES_USER}}:${{POSTGRES_PASSWORD}}@${{RAILWAY_TCP_PROXY_DOMAIN}}:${{RAILWAY_TCP_PROXY_PORT}}/${{POSTGRES_DB}}
DATABASE_PRIVATE_URL=postgres://${{POSTGRES_USER}}:${{POSTGRES_PASSWORD}}@${{RAILWAY_PRIVATE_DOMAIN}}:${{PORT}}/${{POSTGRES_DB}}
DATABASE_URL=postgres://${{POSTGRES_USER}}:${{POSTGRES_PASSWORD}}@${{RAILWAY_TCP_PROXY_DOMAIN}}:${{RAILWAY_TCP_PROXY_PORT}}/${{POSTGRES_DB}}
DATABASE_PRIVATE_URL=postgres://${{POSTGRES_USER}}:${{POSTGRES_PASSWORD}}@${{RAILWAY_PRIVATE_DOMAIN}}:${{PORT}}/${{POSTGRES_DB}}
and this is how i referenced those variables on the test service
PGVECTOR_URL=${{pgvector.DATABASE_URL}}?sslmode=disable
PGVECTOR_PRIVATE_URL=${{pgvector.DATABASE_PRIVATE_URL}}?sslmode=disable
PGVECTOR_URL=${{pgvector.DATABASE_URL}}?sslmode=disable
PGVECTOR_PRIVATE_URL=${{pgvector.DATABASE_PRIVATE_URL}}?sslmode=disable
Nim
Nimβ€’14mo ago
Unfortunately my issue may not be SSL related. The error I get when I look at the logs is:
"psycopg2.OperationalError: could not translate host name"
"psycopg2.OperationalError: could not translate host name"
But just incase, I appended
?sslmode=disable
?sslmode=disable
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.
akshay deo
akshay deoβ€’14mo ago
AFAIK private networking required host to listen on ipv6 and not ipv4. Probably that’s the problem
Brody
Brodyβ€’14mo ago
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?
Nim
Nimβ€’14mo ago
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
Brody
Brodyβ€’14mo ago
can you show me a screenshot of the pgvectors setting page
Nim
Nimβ€’14mo ago
No description
Nim
Nimβ€’14mo ago
No description
Nim
Nimβ€’14mo ago
No description
Brody
Brodyβ€’14mo ago
show me the variables you have set in the service that connect to it of course hiding anything sensitive
Nim
Nimβ€’14mo ago
As mentioned earlier, when I set the db host to
monorail.proxy.rlwy.net
monorail.proxy.rlwy.net
and the port to 4266X, I am able to connect fine. But when I change the host to
pgvector.railway.internal
pgvector.railway.internal
I get the error.
Brody
Brodyβ€’14mo ago
are you building your app with a dockerfile?
Nim
Nimβ€’14mo ago
The second service is a python fastapi application and in the code, here is how the defined variables are used:
No description
Nim
Nimβ€’14mo ago
And: DB_HOST, DB_PORT, ... etc are the variables being used When I set the host variable to this, it works fine.
Brody
Brodyβ€’14mo ago
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
Nim
Nimβ€’14mo ago
Okay noted. Will change, although at the moment the service is built from github
jr
jrβ€’14mo ago
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
Nim
Nimβ€’14mo ago
No description
Nim
Nimβ€’14mo ago
Here is the variable tab with the relevant keys Let me try that and see. Private dns with port 5432
Brody
Brodyβ€’14mo ago
set DB_HOST to ${{pgvector.RAILWAY_PRIVATE_DOMAIN}}
Nim
Nimβ€’14mo ago
No description
Nim
Nimβ€’14mo ago
Okay, I'll redeploy with this just to be safe
Brody
Brodyβ€’14mo ago
let me know how that goes, i have some more ideas if this doesn't work
Nim
Nimβ€’14mo ago
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
Brody
Brodyβ€’14mo ago
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
Nim
Nimβ€’14mo ago
hahaha, there are quite a bunch in their. Including some OCR ones
Brody
Brodyβ€’14mo ago
are they all needed? a lot of time people will pip freeze every python package on their computer into their projects requirements.txt file
Nim
Nimβ€’14mo ago
Yes, they are all needed. I've made sure to cut down on the ones not needed Here is my .toml file:
Nim
Nimβ€’14mo ago
No description
Brody
Brodyβ€’14mo ago
what are you talking about, that is by no means a lot
Nim
Nimβ€’14mo ago
Unstructured is quite chunky to be fair
Brody
Brodyβ€’14mo ago
ive seen 200 line requirements.txt files, you are good
Nim
Nimβ€’14mo ago
Its completed building But unfortunately issue persists πŸ˜…
Brody
Brodyβ€’14mo ago
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 starts
Nim
Nimβ€’14mo ago
That'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.
Brody
Brodyβ€’14mo ago
it do be better like that, but let me know if that sleep(5) changes anything
Nim
Nimβ€’14mo ago
That actually worked! πŸ˜ƒ catjamfasterthanever you! sleep(5) for the win
Solution
Brody
Brodyβ€’14mo ago
now remove it and try adding connect_timeout=5 to the pool options, for a less hacky option
Nim
Nimβ€’14mo ago
Alright, also changed the minimum pool to 0, as advised earlier:
Nim
Nimβ€’14mo ago
No description
Nim
Nimβ€’14mo ago
Re-deploying now.. All Good! salute
Brody
Brodyβ€’14mo ago
awesome
Nim
Nimβ€’14mo ago
Thanks for your help mate! Appreciate you
Brody
Brodyβ€’14mo ago
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
Ohad @ BlockMesh Network
I cut down build times by creating a base image with all deps. And the app pulls from it. Really shorten the time.
Nim
Nimβ€’14mo ago
Will probably try this too some time in the future.
Brody
Brodyβ€’14mo ago
thank you for the trains! πŸ™‚
Nicolas
Nicolasβ€’10mo ago
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?
Want results from more Discord servers?
Add your server