Cannot Connect To Database

Used in this guide:
Next.js 15.3.4
Vercel

Error:

ERROR: Error: cannot connect to Postgres Details: connect ENETUNREACH


This error means that the Vercel build environment attempted to establish a network connection to a specified destination (in this case, a PostgreSQL database). But the network itself could not reach the destination. The connection attempt failed at the network level.

This is common in Next.js when using features like getStaticProps or server-side rendering (SSR) if the code connects to a database to fetch data required for rendering pages.

The error occurs during the "Generating static pages" step of the Next.js build:

[09:05:16.955]   Generating static pages (0/29) ...
[09:05:18.959]   Generating static pages (7/29) 
...
[09:05:18.968] [02:05:18] ERROR: Error: cannot connect to Postgres. 

Next.js is trying to fetch data for certain pages - likely mentioned in the error message which requires a connection to your PostgreSQL database.

The process fails because the build environment on Vercel cannot establish a connection to your PostgreSQL database.


What Causes the Network Unreachable Error on Vercel?

When building on Vercel, the build environment has limited network access for security and stability reasons. This ENETUNREACH error usually points to one of the following:

  • Vercel's build environment struggles with IPv6 connections
  • Database Firewall or Access Restrictions
  • Incorrect Database Host/Address
  • DNS Resolution Issues

How to Fix It

  • Verify Environment Variables
  • Check your Database Provider's Firewall/Whitelisting
  • Locate your Connection Pooler String in your database



Connection Pooler String in Supabase

  • Go to your Supabase Dashboard.
  • Navigate to Project and click the connection icon at the top menu.
  • Copy the string from Transaction pooler section and use it instead of the original direct connection the your .env.local

JKT

Stay focused, and the rest will follow

©Jakkrit Turner. All rights reserved