Introduction
Building a full-stack application with Next.js and PostgreSQL is a powerful choice for modern web development. But when it comes to deployment, costs can quickly add up. Traditional VPS hosting requires maintenance, and managed databases often come with a hefty price tag.
Fortunately, the modern "Serverless" ecosystem offers a solution. By combining Vercel (for the frontend and API) with Neon (for a serverless PostgreSQL database), you can deploy a scalable, production-ready full-stack application completely for free.
In this guide, we will walk through the exact process of deploying a Next.js 15 application with Prisma ORM and Auth.js (NextAuth), covering every step from database creation to handling common deployment errors.
The Tech Stack
- Framework: Next.js 15 (App Router)
- Database: PostgreSQL
- Infrastructure: Vercel (Frontend/API Hosting) + Neon (Serverless Database)
- ORM: Prisma
- Authentication: NextAuth.js (Google OAuth)
Step 1: Set Up Your Serverless Database with Neon
The first hurdle in full-stack deployment is the database. Neon separates storage from compute, allowing it to "scale to zero" when not in use, making it perfect for free-tier projects.
- Create a Project: Go to Neon.tech, sign up, and create a new project.
- Get Connection Details:
- Neon provides a Pooled Connection string (optimized for serverless apps like Next.js).
- It also provides a Direct Connection string (needed for database migrations).
- Keep both handy! You will need them significantly in Step 3.
Pro Tip: Using a pooled connection for your live app prevents "max connection" errors, which are common when serverless functions scale up.
Step 2: Prepare Your Next.js Application
Before deploying, ensure your project is configured to handle the production environment.
1. Prisma Schema Configuration
Serverless environments have a quirk: they don't play well with long-running connections during database migrations. To fix the common P1002: Timed out trying to acquire a postgres advisory lock error, you must separate your migration connection from your app connection.
Update your
prisma/schema.prisma:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL") // Pooled connection for the app
directUrl = env("DIRECT_URL") // Direct connection for migrations
}
2. The Build Script
Ensure your
package.json includes a build script that generates the Prisma client and runs migrations automatically during deployment:
"scripts": {
"build": "npx prisma generate && npx prisma migrate deploy && next build"
}
This ensures that every time you deploy, your database schema is up-to-date.
Step 3: Deploying to Vercel
Vercel is the creators of Next.js, offering the smoothest deployment experience.
Option A: The "No-Git" CLI Method (Fastest)
If you don't want to mess with creating a GitHub repository yet, you can deploy directly from your terminal.
- Install Vercel CLI:
npm i -g vercel - Login:
npx vercel login - Deploy: Run
npx vercel --prodinside your project folder. - Follow the prompts to link your project.
Option B: The GitHub Method (Recommended)
- Push your code to a GitHub repository.
- Log in to Vercel and click "Add New Project".
- Import your repository.
Step 4: Configuring Environment Variables
This is where most deployments fail. Your application needs the secrets to connect to the database and authentication providers.
In your Vercel Project Dashboard, go to Settings > Environment Variables and add the following:
| Variable Key | Value Source | Why? |
|---|---|---|
DATABASE_URL |
Neon Console (Pooled string) | For high-performance app queries. |
DIRECT_URL |
Neon Console (Direct string) | Crucial: Prevents migration timeouts. |
NEXTAUTH_URL |
Your Vercel Domain (e.g., https://myapp.vercel.app) |
Tells Auth.js where the site lives. |
NEXTAUTH_SECRET |
Generate with openssl rand -base64 32 |
Encrypts user sessions. |
Critical Note: Do NOT confuse
DATABASE_URLandDIRECT_URL. If you swap them,your app might work, but your deployments will fail randomly with timeout errors.
Step 5: Handling Authentication (Google OAuth)
If your app uses Google Login, it will break immediately after deployment because Google doesn't trust your new domain yet.
- Go to the Google Cloud Console.
- Navigate to APIs & Services > Credentials.
- Edit your OAuth 2.0 Client.
- Add your Vercel domain (e.g.,
https://myapp.vercel.app) to Authorized JavaScript origins. - Add the callback path (e.g.,
https://myapp.vercel.app/api/auth/callback/google) to Authorized redirect URIs.
Wait a few minutes for Google to propagate changes.
Troubleshooting Common Errors
1. P1002: Timed out trying to acquire a postgres advisory lock
Cause: You are running prisma migrate deploy using a pooled connection. Fix: Ensure you added the directUrl field in your Prisma schema (Step 2) and defined the DIRECT_URL environment variable pointing to the non-pooled Neon string.
2. 404 Not Found on Vercel
Cause: Vercel might be deploying the wrong directory (often happened if you imported a monorepo). Fix: Go to Settings > General > Root Directory and ensure it points to your Next.js app folder usually (./ or your-app-name). Alternatively, deploy using the CLI command npx vercel --prod from the correct folder.
3. Error: No Output Directory named "public"
Cause: Vercel thinks your project is a static site, not a Next.js app. Fix: Create a
vercel.json file in your root:
{
"framework": "nextjs",
"buildCommand": "npx prisma generate && npx prisma migrate deploy && next build"
}
Conclusion
You now have a professional-grade, full-stack application running for $0/month. This architecture scales effortlessly: Vercel handles global traffic distribution and caching, while Neon manages your database scaling.
By following this guide, you’ve not only deployed an app but built a robust foundation for any future SaaS or side project. Happy coding!
Discussion 0
No comments yet. Be the first to start the discussion!
Leave a Comment