Build of next.js fails on prerendering route with Postgres

Hello! I’m just starting with fly.io. My stack:

  • next.js app on app folder with route that does simple SELECT * FROM test using pg package (any other package would be same)
  • using fly launch create an app and postgres app. After that, local build wouldn’t work because DATABASE_URL isn’t available
  • wasn’t able to figure out how to pull secret from fly.io for local builds, so I just ssh’d into app and copied the DATABASE_URL. The fly proxy work fine too
  • fly secrets list displays DATABASE_URL just fine, so apps are attached.

When I do fly deploy I receive this:

Generating static pages (0/7) ...
12.16 
12.16 Error occurred prerendering page "/api/my-api-route". Read more: https://nextjs.org/docs/messages/prerender-error
12.16 
12.16 AggregateError [ECONNREFUSED]: 
12.16     at internalConnectMultiple (node:net:1114:18)
12.16     at afterConnectMultiple (node:net:1667:5)
12.16     at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17)

I tried:

  • there was a suggestion somewhere to put ENV DATABASE_URL “postgres://:memory:”, didn’t help
  • put actual DATABASE_URL into Dockerfile, didn’t help
  • tried flyctl deploy --build-arg DATABASE_URL=$DATABASE_URL, didn’t help. Same with --build-secret.
  • I tried this and it works, but since there are few more steps in Dockerfile to copy and move files, this is not ideal.

I don’t think it should take that much time to deploy a simple next.js app, so I feel like I am missing something obvious.

Nope, you are not. By design, secrets are not available at build time. For those that need it, there is a documented workaround: Build Secrets · Fly Docs; the last line can be added to your package.json as a script so that you can run something like the following to do a deploy:

npm run deploy

(substitute yarn, pnpm, or bun to taste)

I am getting closer! Turns out I was able to send DATABASE_URL before, but “npm run build” from within the Docker cannot connect to the postgres app, localhost:5432.

I was able to connect using host “.internal:5432”.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.