Deploy keeps failing at database error

binojdaniel
PROOP

11 days ago

I keep getting this database connection error when i try a deploy

stage-0

RUN npm run build:railway

2s

npm warn config production Use --omit=dev instead.

> roccos@0.1.0 build:railway

> prisma generate && prisma migrate deploy && next build

Prisma schema loaded from prisma/schema.prisma

Generated Prisma Client (v6.17.1) to ./node_modules/@prisma/client in 158ms

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

Tip: Want to turn off tips and other hints? https://pris.ly/tip-4-nohints

Prisma schema loaded from prisma/schema.prisma

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

Error: P1001: Can't reach database server at postgres.railway.internal:5432

Solved$10 Bounty

42 Replies

Railway
BOT

11 days ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


binojdaniel
PROOP

11 days ago

i am using Postgres on Railway and using the DATABASE_URL variable value in the nexjs app environment variable that my app uses.


11 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 brody 11 days ago


The private network is not available during build. You do it with a pre-deploy command, more details at https://docs.railway.com/guides/pre-deploy-command

You can find it in Settings -> Deploy


binojdaniel
PROOP

11 days ago

So the same command but in pre-deploy? And it should not be part of my package.json?


binojdaniel

So the same command but in pre-deploy? And it should not be part of my package.json?

It should be part of the package.json, the screenshot I sent was wrong.

If your npm script is called “db:migrate” then run that as a Predeploy command


binojdaniel
PROOP

11 days ago

i have these 2 commands in my json, so am i adding the "build:railway" under Settings --> Deploy as pre-build command?

    "build": "prisma generate && next build",
    "build:railway": "prisma generate && prisma migrate deploy && next build",

binojdaniel

i have these 2 commands in my json, so am i adding the "build:railway" under Settings --> Deploy as pre-build command?"build": "prisma generate && next build", "build:railway": "prisma generate && prisma migrate deploy && next build",

It’d be like this (you don’t need the build:railway one):

"build": "prisma generate && next build",
"db:migrate": "prisma generate && prisma migrate deploy",

binojdaniel
PROOP

11 days ago

i am still getting database error from the last deploy but it seems to have little more progress. But same database related error.
prisma:error

Invalid prisma.settings.findFirst() invocation:

Can't reach database server at postgres.railway.internal:5432

Please make sure your database server is running at postgres.railway.internal:5432.


If you click your database service, then go to Settings -> Network, does it look like this?

Attachments


binojdaniel
PROOP

11 days ago

yes,

Attachments


binojdaniel
PROOP

11 days ago

so based on my research it seems like this is what is happening.

The Next.js build is still trying to connect to the Railway database during the build phase, which is not allowed—the private network is only available during pre-deploy and runtime, not build.

What’s happening?

  • Your build process (npm run build:railway) runs next build, which tries to prerender pages (like /sitemap.xml, /test-schema/page) that use Prisma to fetch data from the database.

  • Since the database is not accessible during build, all Prisma queries fail, causing the build to error out.

How to fix:

  1. Switch to Dynamic Rendering for DB Pages

    • Any page or API route that uses Prisma (database access) must be rendered at runtime (on-demand), not at build time.

    • In Next.js, use export const dynamic = "force-dynamic" at the top of those files (e.g., in /sitemap.xml/route.ts, /test-schema/page.tsx, etc.).

    • Remove any getStaticProps, getStaticPaths, or static export logic that fetches from the database.

  2. Do NOT prerender database-dependent pages

    • Only statically generate pages that do not require database access.

    • All others should use SSR (Server-Side Rendering) or API routes.

  3. Update your build script

    • Your build script should only run static build steps:

      This is quite a lot of rewrite for me and it defeats the SSG (Static Site Generation) logic i had for my content pages which is need for performance and effective SEO. This is basically telling me to implement SSR (Which is just Server Side Rendering)

      Let me know if there is any workaround.


binojdaniel

so based on my research it seems like this is what is happening.The Next.js build is still trying to connect to the Railway database during the build phase, which is not allowed—the private network is only available during pre-deploy and runtime, not build.What’s happening?Your build process (npm run build:railway) runs next build, which tries to prerender pages (like /sitemap.xml, /test-schema/page) that use Prisma to fetch data from the database.Since the database is not accessible during build, all Prisma queries fail, causing the build to error out.How to fix:Switch to Dynamic Rendering for DB PagesAny page or API route that uses Prisma (database access) must be rendered at runtime (on-demand), not at build time.In Next.js, use export const dynamic = "force-dynamic" at the top of those files (e.g., in /sitemap.xml/route.ts, /test-schema/page.tsx, etc.).Remove any getStaticProps, getStaticPaths, or static export logic that fetches from the database.Do NOT prerender database-dependent pagesOnly statically generate pages that do not require database access.All others should use SSR (Server-Side Rendering) or API routes.Update your build scriptYour build script should only run static build steps:This is quite a lot of rewrite for me and it defeats the SSG (Static Site Generation) logic i had for my content pages which is need for performance and effective SEO. This is basically telling me to implement SSR (Which is just Server Side Rendering)Let me know if there is any workaround.

You can try using the DATABASE_PUBLIC_URL as your DATABASE_URL during build


binojdaniel
PROOP

11 days ago

where do i mention that?


binojdaniel
PROOP

11 days ago

i tried this but still not working. It still keeps looking for internal

  "build:railway": "DATABASE_URL=$DATABASE_PUBLIC_URL prisma generate && next build",

binojdaniel
PROOP

11 days ago

i can see that the command gets executed with Public URL parameter, but logs shows its still looking for other url
> roccos@0.1.0 build:railway

> DATABASE_URL=$DATABASE_PUBLIC_URL prisma generate && next build

Prisma schema loaded from prisma/schema.prisma

Generated Prisma Client (v6.17.1) to ./node_modules/@prisma/client in 157ms

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

Tip: Want to turn off tips and other hints? https://pris.ly/tip-4-nohints

Invalid next.config.ts options detected:

Unrecognized key(s) in object: 'swcMinify'

See more info here: https://nextjs.org/docs/messages/invalid-next-config

▲ Next.js 15.5.6

- Experiments (use with caution):

· optimizePackageImports

Creating an optimized production build ...

<w> [webpack.cache.PackFileCacheStrategy] Serializing big strings (128kiB) impacts deserialization performance (consider using Buffer instead and decode when needed)

✓ Compiled successfully in 10.3s

Skipping linting

Checking validity of types ...

Collecting page data ...

Generating static pages (0/59) ...

prisma:error

Invalid prisma.settings.findFirst() invocation:

Can't reach database server at postgres.railway.internal:5432

Please make sure your database server is running at postgres.railway.internal:5432.


binojdaniel

i can see that the command gets executed with Public URL parameter, but logs shows its still looking for other url> roccos@0.1.0 build:railway> DATABASE_URL=$DATABASE_PUBLIC_URL prisma generate && next buildPrisma schema loaded from prisma/schema.prismaGenerated Prisma Client (v6.17.1) to ./node_modules/@prisma/client in 157msStart by importing your Prisma Client (See: https://pris.ly/d/importing-client)Tip: Want to turn off tips and other hints? https://pris.ly/tip-4-nohintsInvalid next.config.ts options detected:Unrecognized key(s) in object: 'swcMinify'See more info here: https://nextjs.org/docs/messages/invalid-next-config▲ Next.js 15.5.6- Experiments (use with caution):· optimizePackageImportsCreating an optimized production build ...<w> [webpack.cache.PackFileCacheStrategy] Serializing big strings (128kiB) impacts deserialization performance (consider using Buffer instead and decode when needed)✓ Compiled successfully in 10.3sSkipping lintingChecking validity of types ...Collecting page data ...Generating static pages (0/59) ...prisma:errorInvalid prisma.settings.findFirst() invocation:Can't reach database server at postgres.railway.internal:5432Please make sure your database server is running at postgres.railway.internal:5432.

Are you using a Dockerfile? If so, can you share it?


binojdaniel
PROOP

11 days ago

i dont think im using docker


binojdaniel
PROOP

11 days ago

i just use docker for my local development


binojdaniel
PROOP

11 days ago

Any updates?


binojdaniel

Any updates?

Can you try updating your npm script to this?

```

"build:railway": "prisma generate && DATABASE_URL=$DATABASE_PUBLIC_URL next build"
```

In the version you shared, the DATABASE_URL replacement was done for the prisma generate command, not next build.


binojdaniel
PROOP

11 days ago

now i get prisma:error

Invalid prisma.settings.findFirst() invocation:

error: Error validating datasource db: You must provide a nonempty URL. The environment variable DATABASE_URL resolved to an empty string.


binojdaniel

now i get prisma:errorInvalid prisma.settings.findFirst() invocation:error: Error validating datasource db: You must provide a nonempty URL. The environment variable DATABASE_URL resolved to an empty string.

Make sure your service has DATABASE_PUBLIC_URL env variable defined. There's a chance you only have DATABASE_URL referencing the private network URL and since DATABASE_PUBLIC_URL is empty, it errors out.


binojdaniel
PROOP

11 days ago

i have this under the service variables

Attachments


binojdaniel

i have this under the service variables

It looks like those changes haven't been applied yet. Apply the changes (and it will redeploy and hopefully succeed)


binojdaniel
PROOP

11 days ago

it was already applied, i came to this screen to show that it was already there. i dont know where as to click apply.


binojdaniel

it was already applied, i came to this screen to show that it was already there. i dont know where as to click apply.

Can you see something like this on your dashboard?

If not, try refreshing just to make sure. After the refresh, check if the variable is there. It shouldn't be purple


binojdaniel
PROOP

11 days ago

ok trying now


binojdaniel
PROOP

11 days ago

ok that worked


binojdaniel

ok that worked

Glad to hear! If you run into any other issues let me know :)


binojdaniel
PROOP

11 days ago

but none of the api calls working


binojdaniel

but none of the api calls working

What are the errors?


binojdaniel
PROOP

11 days ago

GET https://roccositaliansausages.com/api/auth/error 500 (Internal Server Error)


binojdaniel
PROOP

11 days ago

basically looks like all auth related errors.


binojdaniel

basically looks like all auth related errors.

Try debugging your app deploy logs to see what's going on, but seems unrelated


binojdaniel
PROOP

11 days ago

but everything works locally fine for me.


binojdaniel
PROOP

11 days ago

and where can i check application logs on Railway?


binojdaniel
PROOP

11 days ago

i was seeing this in the deploy logs

[auth][error] UntrustedHost: Host must be trusted. URL was: https://roccositaliansausages.com/api/auth/session. Read more at https://errors.authjs.dev#untrustedhost
    at /app/.next/server/middleware.js:416:51219
    at hp (/app/.next/server/middleware.js:416:53914)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    ```

jimmyb
FREE

11 days ago

Hello!

You can find logs for your deployments and services in several places. In brief, if you click on the service and panel should pop out from the right, you can click on the View Logs button. This will pop open a new panel which shows you the services build and deploy logs.

You can also click on the Logs link in the top nav bar.

You can learn more about logs here:

https://docs.railway.com/guides/logs

Attachments


binojdaniel

i was seeing this in the deploy logs[auth][error] UntrustedHost: Host must be trusted. URL was: https://roccositaliansausages.com/api/auth/session. Read more at https://errors.authjs.dev#untrustedhost at /app/.next/server/middleware.js:416:51219 at hp (/app/.next/server/middleware.js:416:53914) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) ```

jimmyb
FREE

11 days ago

It looks like you need to set the AUTH_TRUST_HOST=true environment variable. What is the value of NEXTAUTH_URL? Is it https://roccositaliansausages.com?


binojdaniel
PROOP

11 days ago

i did set AUTH_TRUST_HOST=true and but still errors

NEXTAUTH_URL="https://roccositaliansausages.com"

binojdaniel
PROOP

10 days ago

i was able to fix it, turns out NextAuth 5 and Next.js 15 has compatibility issues big time. So i had to downgrade to NextAuth 5


binojdaniel

i was able to fix it, turns out NextAuth 5 and Next.js 15 has compatibility issues big time. So i had to downgrade to NextAuth 5

jimmyb
FREE

10 days ago

That's great, I'm glad to hear you were able to get the issue resolved!

I assume you meant you downgraded to NextAuth 4?


Status changed to Solved samgordon 10 days ago


Status changed to Solved samgordon 10 days ago