Not able to access Postgres from one service but able from the other?
So I'm running into this weird issue. I can access my postgres db with no trouble from one of my services but can't from my other that I have setup as a cron job through railway. Not sure why this is the case. I know it's not a problem with my actual code cause it works fine locally. Any thoughts on why this would be the case?
48 Replies
Project ID:
f0e06b4d-4d9e-4d8d-a668-01321847d526
f0e06b4d-4d9e-4d8d-a668-01321847d526
to be clear, code working locally but not on railway doesn't necessarily rule out a code issue
so let's start simple, show me your service variables please
@Brody Variables on the working service
^^ also my backend service
Variables on my cron job
The DATABASE_URL value is the exact same on both
and they are both referencing the same connection object
is the value sensitive?
wdym sensitive?
oh sorry
yes
does it contain the actual url
they are sensitive
okay it shouldn't be, you should be using a reference variables like that notification wants you to, please remove your database variables and add them back as a reference
oh shoot okay will do
alright i just replaced them with the reference variable
when i press the eye icon though i do still see the raw url
that's fine, as long as they are references in the raw editor
the eye icon just displays the rendered variable your app would end up seeing so you can confirm you setup the reference variable correctly
so instead of a screenshot, just send me that variable as displayed by the raw editor
DATABASE_URL=${{Postgres.DATABASE_URL}}
perfect
alright so go ahead and run the cron job now and see if you still get an error
I still got the error
Can't reach database server at
containers-us-west-110.railway.app
:6540
^^ this is it specificallythought so, a reference still had to be setup so no loss on that aspect
hahaha true true thanks for telling me ab that
can you show me the code that connects to it?
of course!
side note, are you using the private network in anyway?
so for context in my root of my project i have my prisma folder with prisma.schema with this at the top to configure it
now i have a file within my src/ that is called connection.ts and returns the connection object
that connection object is used on my backend services, specifically in my controllers/ directory imported directly and works fine on railway
looks good to me
now for my cron job the code looks like this
so the same thing
can you provide the full error printout
also please answer this question
oh sorry didnt see that
no im not using the private network
well actually my cron job i think defauled to it
and then i just generated a railway url thinking that it had something to do with that
yes private networking is on by default
ah okay i see
also for the error output
PrismaClientInitializationError:
Invalid
prisma.event.deleteMany()
invocation:
Can't reach database server at containers-us-west-110.railway.app
:6540
Please make sure your database server is running at containers-us-west-110.railway.app
:6540
.
at wn.handleRequestError (/app/node_modules/@prisma/client/runtime/library.js:123:7003)
at wn.handleAndLogRequestError (/app/node_modules/@prisma/client/runtime/library.js:123:6119)
at wn.request (/app/node_modules/@prisma/client/runtime/library.js:123:5839)
at async l (/app/node_modules/@prisma/client/runtime/library.js:128:9763) {
clientVersion: '5.3.1',
errorCode: undefined
}
maybe something with the top level self calling async function has something to do with it??
thats the only thing i can think of that differs in this casedisable the private network
alright ill lyk if that worked
bro i love you
you deserve a gold medal
it worked
now can i ask a favor from you
yes
if you where using private networking, you could not just disable it, so another work around has to be found
so wanna re-enable it, and try some other things?
sure why not
im actually curious as to why that would cause an issue
i know the answer to that, we can talk about that in a bit
in your cron service, change this to
DATABASE_URL=${{Postgres.DATABASE_URL}}?connect_timeout=30
make sure you have private networking enabled beforeoh what the heck
that worked too
with private networking
awsome
leave it like that
why did that work?
private networking takes a bit to initialize, so if you try to connect to some kind of service (api/database/etc) at the very beginning of the app lifecyle the connections can tend to fail with non descript errors
ohh okay i see thats pretty interesting; does railway mention that somewhere? I feel like if thats a common issue it should be mentioned
https://docs.railway.app/reference/private-networking#caveats
Private networks take 100ms to initialize on deploy, we ask that you set initial requests on a retry loop.though in practice its more like 2 seconds
ohhhh okay okay i see well thanks for the insight this was incredibly helpful
thanks sm for helping out dude
really appreciate it
no problem! i assume this was you refactoring your node-cron app to run with railways cron?
yep that exactly
i like to have everything centralized on railway
awsome, just make sure you do the task first thing when the app starts, do it as fast, and exit cleanly
will do!
@Angelo - priv net init issue