Database
Learn about database options and how to configure your database for the project
This guide covers the database options available for your project, how to set up and connect to your database, and how to work with Drizzle ORM.
Setup
The project uses PostgreSQL as the database system and Drizzle ORM for database access and management. The setup is designed to be flexible, allowing you to use various PostgreSQL hosting options.
Hosted PostgreSQL Services
Hosted PostgreSQL services are the recommended approach for both development and production environments. They provide easy setup, management, and often include free tiers sufficient for getting started.
Neon
Neon is a serverless PostgreSQL service with an excellent developer experience and is the default choice for this project.
Key Features:
- Serverless architecture
- Database branching for development/staging
- Automatic scaling
- Free tier with 500MB storage
Setup Steps:
- Create an account at neon.tech
- Create a new project
- Create a database
- Get your connection string from the dashboard
- Add the connection string to your
.env
file asDATABASE_URL
Supabase
Supabase provides PostgreSQL databases with additional features like authentication and storage.
Key Features:
- Full PostgreSQL support
- Additional services (auth, storage, etc.)
- Free tier with 500MB storage and 5GB data transfer
- Simple dashboard
Setup Steps:
- Create an account at supabase.com
- Create a new project
- Navigate to the SQL editor or Database settings
- Get your connection string
- Add the connection string to your
.env
file asDATABASE_URL
Other Options
- AWS RDS (PostgreSQL)
- Google Cloud SQL (PostgreSQL)
- Azure Database for PostgreSQL
Self-Hosted Options
Docker
You can run PostgreSQL in a Docker container for local development:
Then use this connection string:
Local Installation
Install PostgreSQL directly on your machine:
- Download from postgres.org or using package managers
- Install and set up a password
- Create a database
Then use this connection string:
If you are setting up the environment, now you can go back to the Environment Setup guide and continue. The rest of this guide can be read later.
Environment Setup
Set up environment variables
Using Drizzle ORM
This project uses Drizzle ORM, a TypeScript ORM with a focus on type safety and developer experience. Drizzle is compatible with various database systems and providers.
Current Configuration
Currently, the project is configured to use Neon's HTTP driver for better compatibility with Next.js and serverless environments:
Database Schema
The database schema is defined in src/db/schema.ts
using Drizzle's schema definition syntax:
Using Other Database Providers
Drizzle ORM supports multiple database systems. If you want to use a different provider:
- Install the appropriate Drizzle adapter package
- Update
src/db/index.ts
to use the correct driver - Adjust the schema in
src/db/schema.ts
if necessary
Example: Using MySQL
Example: Using SQLite
For more information about Drizzle ORM and its adapters, refer to the Drizzle ORM documentation.
Troubleshooting
Connection Issues
If you're having trouble connecting to your database:
- Check that your
DATABASE_URL
is correctly formatted - Ensure your IP is allowed in the database's firewall settings
- Verify that the database user has the correct permissions
- Check for any network restrictions
Schema Migration Issues
If you're experiencing issues with database migrations:
- Check your schema definitions for errors
- Ensure your migration scripts are correctly formatted
- Try running migrations manually to see detailed errors
For more detailed help, refer to the Drizzle ORM documentation.
Next Steps
Now that you understand how to set up a database in MkSaaS, you might want to explore these related features: