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
Percy
Percy14mo ago
Project ID: f0e06b4d-4d9e-4d8d-a668-01321847d526
BigBellyBigDreams
f0e06b4d-4d9e-4d8d-a668-01321847d526
Brody
Brody14mo ago
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
BigBellyBigDreams
@Brody Variables on the working service
No description
BigBellyBigDreams
^^ also my backend service
BigBellyBigDreams
Variables on my cron job
No description
BigBellyBigDreams
The DATABASE_URL value is the exact same on both and they are both referencing the same connection object
Brody
Brody14mo ago
is the value sensitive?
BigBellyBigDreams
wdym sensitive? oh sorry yes
Brody
Brody14mo ago
does it contain the actual url
BigBellyBigDreams
they are sensitive
Brody
Brody14mo ago
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
BigBellyBigDreams
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
Brody
Brody14mo ago
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
BigBellyBigDreams
DATABASE_URL=${{Postgres.DATABASE_URL}}
Brody
Brody14mo ago
perfect alright so go ahead and run the cron job now and see if you still get an error
BigBellyBigDreams
I still got the error Can't reach database server at containers-us-west-110.railway.app:6540 ^^ this is it specifically
Brody
Brody14mo ago
thought so, a reference still had to be setup so no loss on that aspect
BigBellyBigDreams
hahaha true true thanks for telling me ab that
Brody
Brody14mo ago
can you show me the code that connects to it?
BigBellyBigDreams
of course!
Brody
Brody14mo ago
side note, are you using the private network in anyway?
BigBellyBigDreams
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
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
now i have a file within my src/ that is called connection.ts and returns the connection object
import { PrismaClient } from "@prisma/client";

export const prisma = new PrismaClient();
import { PrismaClient } from "@prisma/client";

export const prisma = new PrismaClient();
that connection object is used on my backend services, specifically in my controllers/ directory imported directly and works fine on railway
Brody
Brody14mo ago
looks good to me
BigBellyBigDreams
now for my cron job the code looks like this
import { prisma } from "./connection";

(async () => {
try {
const l = await prisma.event.deleteMany();
console.log(l);
} catch (error) {
console.log(error);
}
})();
import { prisma } from "./connection";

(async () => {
try {
const l = await prisma.event.deleteMany();
console.log(l);
} catch (error) {
console.log(error);
}
})();
so the same thing
Brody
Brody14mo ago
can you provide the full error printout also please answer this question
BigBellyBigDreams
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
Brody
Brody14mo ago
yes private networking is on by default
BigBellyBigDreams
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 case
Brody
Brody14mo ago
disable the private network
BigBellyBigDreams
alright ill lyk if that worked bro i love you you deserve a gold medal it worked
Brody
Brody14mo ago
now can i ask a favor from you
BigBellyBigDreams
yes
Brody
Brody14mo ago
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?
BigBellyBigDreams
sure why not im actually curious as to why that would cause an issue
Brody
Brody14mo ago
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 before
BigBellyBigDreams
oh what the heck that worked too with private networking
Brody
Brody14mo ago
awsome leave it like that
BigBellyBigDreams
why did that work?
Brody
Brody14mo ago
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
BigBellyBigDreams
ohh okay i see thats pretty interesting; does railway mention that somewhere? I feel like if thats a common issue it should be mentioned
Brody
Brody14mo ago
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
BigBellyBigDreams
ohhhh okay okay i see well thanks for the insight this was incredibly helpful thanks sm for helping out dude really appreciate it
Brody
Brody14mo ago
no problem! i assume this was you refactoring your node-cron app to run with railways cron?
BigBellyBigDreams
yep that exactly i like to have everything centralized on railway
Brody
Brody14mo ago
awsome, just make sure you do the task first thing when the app starts, do it as fast, and exit cleanly
BigBellyBigDreams
will do!
Brody
Brody14mo ago
@Angelo - priv net init issue
Want results from more Discord servers?
Add your server