R
Railway•3mo ago
n8levine

Errors Connecting to Postgres with a Cron Job

Hi, I'm running a couple services on Railway, connected to a Postgres instance using the internal connection, which has been working great. Recently I set up a cron job trying to connect to the same Postgres, and sometimes it works fine, but intermittently it gets this error:
Error: getaddrinfo ENOTFOUND postgres.railway.internal

at /app/node_modules/.pnpm/pg-pool@3.6.1_pg@8.11.3/node_modules/pg-pool/index.js:45:11

at processTicksAndRejections (node:internal/process/task_queues:95:5)

at PostgresDriver.acquireConnection (/app/node_modules/.pnpm/kysely@0.26.3/node_modules/kysely/dist/cjs/dialect/postgres/postgres-driver.js:21:24)
Error: getaddrinfo ENOTFOUND postgres.railway.internal

at /app/node_modules/.pnpm/pg-pool@3.6.1_pg@8.11.3/node_modules/pg-pool/index.js:45:11

at processTicksAndRejections (node:internal/process/task_queues:95:5)

at PostgresDriver.acquireConnection (/app/node_modules/.pnpm/kysely@0.26.3/node_modules/kysely/dist/cjs/dialect/postgres/postgres-driver.js:21:24)
When that happens it hangs the cron run as well, preventing any later ones from running (they say skipped). My regular app runs out of the same codebase with very little different but has no issues connecting. Any ideas what's going wrong here?
Solution:
do you have a 3 second sleep in your start script?
Jump to solution
13 Replies
Percy
Percy•3mo ago
Project ID: 32bfb0d3-b1b4-4af3-ab7f-efdd0d2968cd
n8levine
n8levine•3mo ago
32bfb0d3-b1b4-4af3-ab7f-efdd0d2968cd Also I've experimented with various cron intervals, but that doesn't seem to help much
Brody
Brody•3mo ago
nixpacks or dockerfile based build?
n8levine
n8levine•3mo ago
Nixpacks
Solution
Brody
Brody•3mo ago
do you have a 3 second sleep in your start script?
n8levine
n8levine•3mo ago
Hm, no not that I know of. Is that something you recommend?
Brody
Brody•3mo ago
yes it's needed because the private network is not available for the first 3 or so seconds
n8levine
n8levine•3mo ago
ooh interesting. That probably explains it then, it's basically just racing and sometimes is slow enough to wait for the network, other times not Feel like that should be documented somewhere 😅
Brody
Brody•3mo ago
it is
n8levine
n8levine•3mo ago
oh, where?
Brody
Brody•3mo ago
https://docs.railway.app/reference/private-networking#caveats though whoever wrote that got the time very wrong
n8levine
n8levine•3mo ago
Got it, yea that's a bit misleading But appreciate the help! gonna go play around with the startup script
Brody
Brody•3mo ago
sounds good!