PayloadCMS requires database access at build
gabsong
FREEOP

3 months ago

Hi,

My app is based on PayloadCMS and the build process requires DB access. My deploy is failing with the following error logs, meaning the DATABASE_URL is not reachable by the app at deploy time. How can I resolve this? (error log redacted due to length)

pnpm --filter @namespace/my-app build

45s

apps/my-app |  WARN  The field "pnpm.onlyBuiltDependencies" was found in /app/apps/my-app/package.json. This will not take effect. You should configure "pnpm.onlyBuiltDependencies" at the root of the workspace instead.

packages/section-headline-plugin |  WARN  The field "pnpm.onlyBuiltDependencies" was found in /app/packages/section-headline-plugin/package.json. This will not take effect. You should configure "pnpm.onlyBuiltDependencies" at the root of the workspace instead.

> @namespace/my-app@0.1.0 build /app/apps/my-app

> cross-env NODE_OPTIONS=--no-deprecation next build

Warning: Found multiple lockfiles. Selecting /app/pnpm-lock.yaml.

Consider removing the lockfiles at:

* /app/apps/my-app/pnpm-lock.yaml

Attention: Next.js now collects completely anonymous telemetry regarding usage.

This information is used to shape Next.js' roadmap and prioritize features.

You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:

https://nextjs.org/telemetry

▲ Next.js 15.4.4

Creating an optimized production build ...

✓ Compiled successfully in 34.0s

Collecting page data ...

[22:28:42] ERROR: Error: cannot connect to Postgres. Details: getaddrinfo ENOTFOUND postgres.railway.internal

err: {

"type": "Error",

"message": "getaddrinfo ENOTFOUND postgres.railway.internal",

"stack":

Error: getaddrinfo ENOTFOUND postgres.railway.internal

at /app/apps/my-app/.next/server/chunks/1403.js:6:15208

at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

at async hM (/app/apps/my-app/.next/server/chunks/1403.js:346:7018)

at async Object.hN [as connect] (/app/apps/my-app/.next/server/chunks/1403.js:346:7262)

at async bE.init (/app/apps/my-app/.next/server/chunks/1403.js:198:7519)

at async bH (/app/apps/my-app/.next/server/chunks/1403.js:198:10684)

at async Object.o [as generateStaticParams] (/app/apps/my-app/.next/server/app/(frontend)/posts/page/[pageNumber]/page.js:2:9219)

at async builtRouteParams (/app/node_modules/.pnpm/next@15.4.4_@babel+core@7.28.4_@playwright+test@1.54.1_react-dom@19.1.0_react@19.1.0__react@19.1.0_sass@1.77.4/node_modules/next/dist/build/static-paths/app.js:396:36)

at async buildAppStaticPaths (/app/node_modules/.pnpm/next@15.4.4_@babel+core@7.28.4_@playwright+test@1.54.1_react-dom@19.1.0_react@19.1.0__react@19.1.0_sass@1.77.4/node_modules/next/dist/build/static-paths/app.js:366:25)

at async /app/node_modules/.pnpm/next@15.4.4_@babel+core@7.28.4_@playwright+test@1.54.1_react-dom@19.1.0_react@19.1.0__react@19.1.0_sass@1.77.4/node_modules/next/dist/build/utils.js:925:79

"errno": -3008,

"code": "ENOTFOUND",

"syscall": "getaddrinfo",

"hostname": "postgres.railway.internal"

}

[22:28:42] ERROR: Error: cannot connect to Postgres. Details: getaddrinfo ENOTFOUND postgres.railway.internal

err: {

"type": "Error",

"message": "getaddrinfo ENOTFOUND postgres.railway.internal",

"stack":

Error: getaddrinfo ENOTFOUND postgres.railway.internal

at /app/apps/my-app/.next/server/chunks/1403.js:6:15208

at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

at async /app/node_modules/.pnpm/next@15.4.4_@babel+core@7.28.4_@playwright+test@1.54.1_react-dom@19.1.0_react@19.1.0__react@19.1.0_sass@1.77.4/node_modules/next/dist/build/utils.js:925:79

"errno": -3008,

"code": "ENOTFOUND",

"syscall": "getaddrinfo",

"hostname": "postgres.railway.internal"

}

⨯ Next.js build worker exited with code: 1 and signal: null

/app/apps/my-app:

 ERR_PNPM_RECURSIVE_RUN_FIRST_FAIL  @namespace/my-app@0.1.0 build: cross-env NODE_OPTIONS=--no-deprecation next build

Exit status 1

ERROR: failed to build: failed to solve: process "sh -c pnpm --filter @namespace/my-app build" did not complete successfully: exit code: 1

Solved$10 Bounty

4 Replies

Railway
BOT

3 months 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!


brody
EMPLOYEE

3 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 3 months ago


irazvan2745
PRO

3 months ago

move the build command to the predeploy command


gabsong
FREEOP

2 months ago

Hint from: https://payloadcms.com/docs/production/building-without-a-db-connection

This is because Next.js' static site generation requires a DB connection. The above changes disable SSG at build time. I was actually able to solve this by doing the following:

  • Change the build command (in package.json) from cross-env NODE_OPTIONS=--no-deprecation next build to cross-env NODE_OPTIONS=--no-deprecation next build --experimental-build-mode compile

  • Change the start command (in package.json) from cross-env NODE_OPTIONS=--no-deprecation next start to cross-env NODE_OPTIONS=--no-deprecation next build --experimental-build-mode generate && cross-env NODE_OPTIONS=--no-deprecation next start

  • Optionally, you can add payload migrate to the start command if your app requires it and want to migrate the DB on deploy

Do I get the $10 bounty?


brody
EMPLOYEE

2 months ago

You unfortunately do not get a bounty for answering your own question.


Status changed to Awaiting User Response Railway 3 months ago


Status changed to Solved brody 3 months ago


Loading...