Intermittent DB connection issue
stevenpila
PROOP

18 days ago

We're getting an intermittent DB connection error to NEON using prisma during deployment.

The error is

Error: P1001: Can't reach database server at `ep-...hnf.ap-southeast-2.aws.neon.tech:5432`

We also have a static enabled IP configured.

Sometimes it works but most of the time no so we keep on redeploying until it succeeds.

Any idea how we can address this issue? it really blocks us every time since it takes time to deploy successfully.

We tried to disable static IP and the intermittent error is not occurring but we need it.

We also tried adding connect_timeout to the DB URL but still the same issue.

Also in neon, we set the network as Allow traffic via the public internet .

Thank you.

Solved$20 Bounty

1 Replies

Railway
BOT

18 days ago

This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.

Status changed to Open Railway 18 days ago


Status changed to Awaiting Railway Response Railway 11 days ago


Status changed to Awaiting User Response Railway 11 days ago


xordanblu
FREETop 10% Contributor

10 days ago

The first thing I would check is where Prisma is trying to connect from.

Railway Static Outbound IP applies to traffic from the running service replica. If prisma migrate deploy, prisma db push, or any DB check runs during the build phase, that traffic may not be coming from the same static egress path as the deployed service. That can look exactly like this: deploy sometimes fails with P1001, but the app works after enough redeploys.

Recommended setup:

  1. Do not run DB network calls in npm install, postinstall, Docker build, or Railway build command. Build should do only prisma generate and app compilation.
  2. Run migrations after the container starts, from a service that has Static Outbound IP enabled, or create a small dedicated migrate service/job in the same project that runs npx prisma migrate deploy and exits.
  3. In Prisma with Neon, use two URLs:
datasource db {
  provider  = "postgresql"
  url       = env("DATABASE_URL")
  directUrl = env("DIRECT_URL")
}
  • DATABASE_URL: Neon pooled connection string for normal app traffic.
  • DIRECT_URL: Neon direct connection string for migrations.
  1. Keep sslmode=require and raise timeout on the direct URL, for example connect_timeout=30.
  2. If you have Prisma commands in the app start command, make them explicit and fail-fast, e.g. npx prisma migrate deploy && npm run start, rather than hiding them in package scripts.

How I would confirm the cause:

  • Temporarily remove any Prisma DB command from the build phase and deploy.
  • Open a Railway shell/one-off command from the running service with Static Outbound IP enabled and run:
npx prisma migrate status
npx prisma migrate deploy

If that works reliably from the running service but not during build, the issue is build-time egress vs runtime egress.

Also double-check region and URL choice:

  • Your Neon endpoint is ap-southeast-2. If the Railway service is in a far region, TLS/connect timeouts are more likely under static egress. Put the Railway service in the closest available region.
  • For app runtime, prefer the Neon pooled endpoint. For migrations, use direct URL. Using the pooler for migrations can cause odd migration/lock behavior.
  • After enabling Static Outbound IP, redeploy the service so the running replica is actually using that networking config.

If it still fails after moving all DB access to runtime/static egress, then I would open Railway support with: service ID, deployment ID, region, static outbound IP enabled, exact timestamp of a P1001, and confirmation that the same command succeeds when static IP is disabled. That narrows it to Railway static egress/NAT routing rather than Prisma or Neon config.


Status changed to Awaiting Railway Response Railway 10 days ago


Status changed to Awaiting User Response Railway 10 days ago


Railway
BOT

3 days ago

This thread has been marked as solved automatically due to a lack of recent activity. Please re-open this thread or create a new one if you require further assistance. Thank you!

Status changed to Solved Railway 3 days ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...