a month 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
42 Replies
a month 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!
a month ago
i am using Postgres on Railway and using the DATABASE_URL variable value in the nexjs app environment variable that my app uses.
a month 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 • about 1 month ago
a month 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
a month 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?
a month ago
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
a month 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",
a month ago
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",a month 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.
a month ago
If you click your database service, then go to Settings -> Network, does it look like this?
Attachments
a month 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) runsnext 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 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.
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.
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.
a month ago
You can try using the DATABASE_PUBLIC_URL as your DATABASE_URL during build
a month ago
where do i mention that?
a month 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",a month 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.
a month ago
Are you using a Dockerfile? If so, can you share it?
a month ago
Any updates?
binojdaniel
Any updates?
a month ago
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.
a month 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.
a month ago
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
i have this under the service variables
a month ago
It looks like those changes haven't been applied yet. Apply the changes (and it will redeploy and hopefully succeed)
a month 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.
a month ago
a month ago
ok trying now
binojdaniel
ok that worked
a month ago
Glad to hear! If you run into any other issues let me know :)
a month ago
but none of the api calls working
a month ago
GET https://roccositaliansausages.com/api/auth/error 500 (Internal Server Error)
a month ago
basically looks like all auth related errors.
binojdaniel
basically looks like all auth related errors.
a month ago
Try debugging your app deploy logs to see what's going on, but seems unrelated
a month ago
but everything works locally fine for me.
a month ago
and where can i check application logs on Railway?
a month 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)
```a month 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:
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) ```
a month 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?
a month ago
i did set AUTH_TRUST_HOST=true and but still errors
NEXTAUTH_URL="https://roccositaliansausages.com"a month 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
a month 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 • about 1 month ago
Status changed to Solved samgordon • about 1 month ago


