Unable to connect internal database
eee
PROOP

5 months ago

Loaded Prisma config from prisma.config.ts.

Prisma schema loaded from prisma/schema.

Generated Prisma Client (v7.2.0) to ./node_modules/.pnpm/@prisma+client@7.2.0_prisma@7.2.0_@types+react@18.3.18_react-dom@19.2.3_react@19.2.3__react@1_mjdrmvbq545cdg4sflwm6veu7u/node_modules/@prisma/client in 269ms

Start by importing your Prisma Client (See: https://pris.ly/d/importing-client)

Tip: Need your database queries to be 1000x faster? Accelerate offers you that and more: https://pris.ly/tip-2-accelerate

> kun-nestjs@0.0.1 migration:run:prod /app

> prisma migrate deploy

Loaded Prisma config from prisma.config.ts.

Prisma schema loaded from prisma/schema.

Datasource "db": PostgreSQL database "railway", schema "public" at "postgres.railway.internal:5432"

$20 Bounty

7 Replies

5 months 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 brody 5 months ago


ilyassbreth
FREE

5 months ago

the issue is that postgres.railway.internal isnt available during build phase

you need to run your migrations in the start command instead of build command so they run when private networking is available

change your start command to:

npx prisma migrate deploy && npm start

or if you have a different start script:

npx prisma migrate deploy && node dist/main.js

this runs migrations at deploy time when the private network is accessible. dont put prisma migrate in your build command anymore, you can also run this as part of pre-deploy command feature


ilyassbreth
FREE

5 months ago

hope this help you


ilyassbreth

the issue is that postgres.railway.internal isnt available during build phase you need to run your migrations in the start command instead of build command so they run when private networking is available change your start command to: `npx prisma migrate deploy && npm start` or if you have a different start script: `npx prisma migrate deploy && node dist/main.js` this runs migrations at deploy time when the private network is accessible. dont put prisma migrate in your build command anymore, you can also run this as part of pre-deploy command feature

5 months ago

This is likely the cause, but I would highly advise not running this as part of the start command (especially if you have replicas), and instead running it as a Pre-Deploy Command. It is designed specifically for this kind of operation.


ilyassbreth
FREE

5 months ago

yes ofc , best solution is to use railway's pre-deploy command feature. it runs between build and deploy when private networking is available, and its designed specifically for migrations.

in your railway service settings:

- find "pre-deploy command" section

- add: npx prisma migrate deploy

this is better than putting it in start command especially if you scale to multiple replicas later since pre-deploy runs once before deployment while start command would run on every replica


aymenkani
FREE

5 months ago

The issue might actually be deeper than just a missing env var or a database delay. It is often related to how Prisma binaries are packaged during the build process.

I have a Node.js + Prisma + Redis template that solves this specific issue. It’s pre-configured to handle the packaging correctly and runs migrations safely in the pre-deploy stage. You can check it out here: https://railway.com/deploy/nodejs-multimodal-rag-starter?referralCode=2psx%5Ft&utm%5Fmedium=integration&utm%5Fsource=template&utm%5Fcampaign=generic


ilyassbreth
FREE

5 months ago

hy @eee , did solution end up fixing everything on your end?


2 months ago

1. Restart PostgreSQL First (Recommended First Step)

  • Go to your PostgreSQL service in Railway.
  • Click Restart.
  • Wait 15–20 seconds until it's fully up.
  • Then trigger a new deployment for your app.

2. Add Connection Timeout to DATABASE_URL

Update your DATABASE_URL environment variable (add at the end):

text

?connect_timeout=60&socket_timeout=60&pool_timeout=30

Example:

text

postgresql://user:pass@postgres.railway.internal:5432/railway?connect_timeout=60&socket_timeout=60&pool_timeout=30

3. Add Sleep Before Migration (Most Effective Fix)

Change your Start Command in Railway to:

text

/bin/sh -c "sleep 15 && npx prisma migrate deploy && npm run start:prod"

(You can increase sleep to 20 if needed.)

4. Use directUrl in schema.prisma (Strongly Recommended)

In prisma/schema.prisma, update your datasource:

prisma

datasource db {
  provider  = "postgresql"
  url       = env("DATABASE_URL")
  directUrl = env("DIRECT_URL")     // ← Add this
}

Then add a new variable in Railway:

  • DIRECT_URL → same as your internal PostgreSQL URL (without extra parameters).

5. (Optional) Increase Postgres Resources Temporarily

Go to PostgreSQL service → Resources → Increase RAM/CPU for testing.

Try in this order: 1 → 2 → 3

After applying, redeploy and check the build logs. greet super grok 🥸🤝


Welcome!

Sign in to your Railway account to join the conversation.

Loading...