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
Percy15mo ago
Project ID: f0e06b4d-4d9e-4d8d-a668-01321847d526
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
f0e06b4d-4d9e-4d8d-a668-01321847d526
Brody
Brody15mo 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
BigBellyBigDreamsOP15mo ago
@Brody Variables on the working service
No description
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
^^ also my backend service
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
Variables on my cron job
No description
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
The DATABASE_URL value is the exact same on both and they are both referencing the same connection object
Brody
Brody15mo ago
is the value sensitive?
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
wdym sensitive? oh sorry yes
Brody
Brody15mo ago
does it contain the actual url
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
they are sensitive
Brody
Brody15mo 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
BigBellyBigDreamsOP15mo ago
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
Brody15mo 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
BigBellyBigDreamsOP15mo ago
DATABASE_URL=${{Postgres.DATABASE_URL}}
Brody
Brody15mo ago
perfect alright so go ahead and run the cron job now and see if you still get an error
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
I still got the error Can't reach database server at containers-us-west-110.railway.app:6540 ^^ this is it specifically
Brody
Brody15mo ago
thought so, a reference still had to be setup so no loss on that aspect
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
hahaha true true thanks for telling me ab that
Brody
Brody15mo ago
can you show me the code that connects to it?
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
of course!
Brody
Brody15mo ago
side note, are you using the private network in anyway?
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
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
Brody15mo ago
looks good to me
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
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
Brody15mo ago
can you provide the full error printout also please answer this question
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
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
Brody15mo ago
yes private networking is on by default
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
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
Brody15mo ago
disable the private network
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
alright ill lyk if that worked bro i love you you deserve a gold medal it worked
Brody
Brody15mo ago
now can i ask a favor from you
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
yes
Brody
Brody15mo 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
BigBellyBigDreamsOP15mo ago
sure why not im actually curious as to why that would cause an issue
Brody
Brody15mo 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
BigBellyBigDreamsOP15mo ago
oh what the heck that worked too with private networking
Brody
Brody15mo ago
awsome leave it like that
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
why did that work?
Brody
Brody15mo 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
BigBellyBigDreamsOP15mo ago
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
Brody15mo 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
BigBellyBigDreamsOP15mo ago
ohhhh okay okay i see well thanks for the insight this was incredibly helpful thanks sm for helping out dude really appreciate it
Brody
Brody15mo ago
no problem! i assume this was you refactoring your node-cron app to run with railways cron?
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
yep that exactly i like to have everything centralized on railway
Brody
Brody15mo ago
awsome, just make sure you do the task first thing when the app starts, do it as fast, and exit cleanly
BigBellyBigDreams
BigBellyBigDreamsOP15mo ago
will do!
Brody
Brody15mo ago
@Angelo - priv net init issue
Want results from more Discord servers?
Add your server