This guide will walk you through setting up the database for your NextSaaS application. Follow these steps every time you clone the repository or set up a new instance.
- Node.js 18+ installed
- npm or yarn package manager
- Git installed
- A Supabase account (free tier works great for development)
-
Go to https://supabase.com and sign in
-
Click "New project"
-
Fill in the project details:
- Name: Your project name (e.g., "nextsaas-dev")
- Database Password: Generate a strong password (save this!)
- Region: Choose the closest to you
- Pricing Plan: Free tier is perfect for development
-
Wait for the project to be created (takes about 2 minutes)
Once your project is ready:
-
Go to Settings → API in your Supabase dashboard
-
Copy these values:
- Project URL (looks like
https://xxxxx.supabase.co) - Anon/Public key (a long string starting with
eyJ...) - Service role key (
⚠️ Keep this secret! Only for server-side)
- Project URL (looks like
-
Go to Settings → Database
-
Copy the Connection string (URI format)
- Copy the example environment file:
cp .env.example .env.local- Update
.env.localwith your Supabase credentials:
# Supabase Configuration
NEXT_PUBLIC_SUPABASE_URL=https://xxxxx.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJ...your-anon-key...
SUPABASE_SERVICE_ROLE_KEY=eyJ...your-service-key...
# Direct database connection (for migrations)
DATABASE_URL=postgresql://postgres:[YOUR-PASSWORD]@db.xxxxx.supabase.co:5432/postgres# From the root of the project
npm installWe've created a one-command setup that will:
- Create all tables
- Set up Row Level Security
- Configure triggers and functions
- Seed initial data
# Run the complete database setup
npm run db:setupThis command will:
- ✅ Create all database tables
- ✅ Set up Row Level Security policies
- ✅ Create triggers and functions
- ✅ Insert seed data for development
- ✅ Set up the initial admin user
If you prefer to run the setup manually or need more control:
cd packages/database
npm run db:migrate up# Development data
npm run db:seed run development
# Or reset and seed
npm run db:seed resetGo to your Supabase dashboard:
- Navigate to Table Editor
- You should see all tables:
- Core:
users,organizations,memberships - Auth:
sessions,oauth_accounts,password_resets,email_verifications - Billing:
plans,subscriptions,invoices,payments,usage_tracking - Content:
projects,items,categories,attachments,custom_fields - System:
audit_logs,activities,notifications,feature_flags,api_keys
- Core:
Important: RLS should be automatically enabled by the migrations, but verify:
- Go to Authentication → Policies in Supabase
- Check that all tables show "RLS enabled" ✅
- You should see policies for each table
- Go to Authentication → Users in Supabase
- Click "Invite user"
- Enter your email
- Check your email for the confirmation link
- Start the development server:
npm run dev- Navigate to http://localhost:3002/register
- Create your account
- Check the
userstable in Supabase to see your user
After creating a user:
-- Run this in the Supabase SQL editor
-- Replace the user_id with your actual user ID from the users table
INSERT INTO organizations (name, slug, created_by)
VALUES ('My Company', 'my-company', 'your-user-id-here');
-- Add yourself as the owner
INSERT INTO memberships (user_id, organization_id, role, accepted_at)
VALUES (
'your-user-id-here',
(SELECT id FROM organizations WHERE slug = 'my-company'),
'owner',
NOW()
);Run this SQL query in the Supabase SQL editor to verify everything is working:
-- Check tables exist
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;
-- Check RLS is enabled
SELECT tablename, rowsecurity
FROM pg_tables
WHERE schemaname = 'public';
-- Check your user and organization
SELECT
u.email,
u.name,
o.name as org_name,
m.role
FROM users u
JOIN memberships m ON m.user_id = u.id
JOIN organizations o ON o.id = m.organization_id
WHERE u.email = 'your-email@example.com';-
"Permission denied" errors
- Make sure RLS is enabled on all tables
- Check that you're using the correct API keys
- Verify your user has the proper role in the organization
-
"Relation does not exist" errors
- Run migrations:
npm run db:migrate up - Check you're connected to the right database
- Run migrations:
-
Can't see data in the application
- Verify RLS policies are correct
- Make sure you're authenticated
- Check that your user is a member of an organization
If you need to start fresh:
# ⚠️ This will delete ALL data
cd packages/database
npm run db:seed reset- Explore the Admin Panel: Visit
/adminin your application - Check the API: Test endpoints at
/api/health - Review Security: Check RLS policies in Supabase dashboard
- Set up Backups: Enable point-in-time recovery in Supabase
When pulling new changes from the repository:
# Pull latest changes
git pull origin main
# Install any new dependencies
npm install
# Run any new migrations
cd packages/database
npm run db:migrate up- Use the Supabase Dashboard: Great for exploring data and testing queries
- Enable Realtime: Go to Database → Replication to enable realtime for tables
- SQL Editor: Use the SQL editor in Supabase for quick queries
- Logs: Check Database → Logs for query performance
For production:
- Create a separate Supabase project for production
- Use different API keys and connection strings
- Enable additional security features:
- SSL enforcement
- Connection pooling
- Daily backups
- Set up monitoring and alerts
# One-command setup
npm run db:setup
# Individual commands
npm run db:migrate up # Run migrations
npm run db:seed run # Seed data
npm run db:seed reset # Reset and seed
npm run db:migrate status # Check migration status
npm run db:generate # Generate TypeScript typesNeed help? Check the troubleshooting guide or open an issue on GitHub.