LogoNEXTDEVKIT Docs

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

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:

  1. Sign up at neon.tech
  2. Create a new project
  3. Copy the connection string
  4. 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:

  1. Create account at supabase.com
  2. Create a new project
  3. Navigate to Settings → Database
  4. Copy the connection string
  5. Add to your .env.local or .env:

🔄 Schema Management & Migrations

Updating Database Schema

  1. 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] }),
}));
  1. Generate Migration:

    pnpm db:generate
  2. Review Migration: Check the generated SQL file in drizzle/ directory

  3. 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

🎯 Next Steps

Now that you understand the database architecture, dive into the specific setup for your chosen platform: