PostgreSQL Database
Complete PostgreSQL setup guide for NEXTDEVKIT including cloud providers and self-hosting options.
PostgreSQL is the default database for NEXTDEVKIT's standard Next.js deployment, offering robust features, excellent performance, and wide hosting support.
🚀 Why PostgreSQL?
PostgreSQL is chosen as the default database for NEXTDEVKIT because:
- 🔧 Full-featured: Support for complex queries, transactions, and relationships
- 📊 ACID compliance: Ensures data integrity and consistency
- 🌐 Wide support: Available on most hosting platforms
- 🎯 Developer-friendly: Excellent tooling and documentation
- ⚡ Performance: Optimized for both read and write operations
- 🔒 Security: Enterprise-grade security features
Environment Variables
Configure your PostgreSQL connection in .env.local
:
# PostgreSQL Connection
DATABASE_URL="postgresql://username:password@localhost:5432/dbname"
🌐 Cloud PostgreSQL Providers
1. Neon (Recommended)
Neon is a modern PostgreSQL platform designed for serverless applications:
Features:
- 🚀 Serverless PostgreSQL
- 🔄 Auto-scaling
- 🎯 Generous free tier
- ⚡ Instant provisioning
- 🔀 Branching for development
Setup Steps:
- Sign up at neon.tech
- Create a new project
- Copy the connection string
- Add to your
.env.local
or.env
:
2. Supabase
Supabase provides a complete backend-as-a-service with PostgreSQL:
Features:
- 🗄️ PostgreSQL database
- 🔐 Built-in authentication
- 📡 Real-time subscriptions
- 🎨 Admin dashboard
- 🔒 Row-level security
Setup Steps:
- Create account at supabase.com
- Create a new project
- Navigate to Settings → Database
- Copy the connection string
- Add to your
.env.local
or.env
:
🔄 Schema Management & Migrations
Updating Database Schema
- Modify Schema File:
Edit
src/database/schema.ts
to add/modify tables:
// Add new table
export const post = pgTable("post", {
id: text("id").primaryKey(),
title: text("title").notNull(),
content: text("content"),
authorId: text("authorId").references(() => user.id),
published: boolean("published").default(false),
createdAt: timestamp("createdAt").defaultNow(),
updatedAt: timestamp("updatedAt").defaultNow(),
});
// Add relation
export const postRelations = relations(post, ({ one }) => ({
author: one(user, { fields: [post.authorId], references: [user.id] }),
}));
-
Generate Migration:
pnpm db:generate
-
Review Migration: Check the generated SQL file in
drizzle/
directory -
Apply Migration:
pnpm db:push
Migration Files
Generated migration files are stored in drizzle/
directory:
🔍 Database Studio
Using Drizzle Studio
Launch the visual database management interface:
pnpm db:studio
Features:
- 📊 Browse table data
- ✏️ Edit records in-place
- 🔍 Run custom SQL queries
- 🎯 View table relationships
- 📈 Monitor query performance
🛠️ Troubleshooting
Common Issues
Connection Errors:
- Verify
DATABASE_URL
is correct - Check firewall settings
- Ensure PostgreSQL is running
- Verify SSL requirements
Migration Failures:
- Check for syntax errors in schema
- Verify foreign key constraints
- Ensure database permissions
- Review migration logs
Performance Issues:
- Add database indexes
- Optimize query structure
- Monitor connection pool usage
- Check for N+1 query problems
🔗 Related Resources
- PostgreSQL Documentation
- Drizzle ORM Documentation
- Neon PostgreSQL
- Supabase Documentation
- Dokploy Self-Hosting
🎯 Next Steps
Now that you understand the database architecture, dive into the specific setup for your chosen platform: