This guide explains how to set up and use Supabase with the NestJS backend.
The backend is configured to work with Supabase PostgreSQL database using MikroORM as the ORM. You can use either:
- Local Supabase for development (recommended)
- Hosted Supabase for production
Ensure you have the following installed:
- Supabase CLI
- Docker Desktop (required for local Supabase)
# From the project root
cd go-train-group-pass
# Start Supabase (this will start Docker containers)
supabase startThis will start the local Supabase stack on these ports (as configured in supabase/config.toml):
- PostgreSQL Database:
localhost:54322 - Studio (Web UI):
http://localhost:54323 - API:
http://localhost:54321 - Inbucket (Email testing):
http://localhost:54324
Create a .env file in the backend directory:
cd backend
cp .env.example .envFor local development, the default values should work:
DATABASE_URL=postgresql://postgres:postgres@localhost:54322/postgres
DB_HOST=localhost
DB_PORT=54322
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=postgres
NODE_ENV=developmentOnce Supabase is running and your backend is configured:
cd backend
# Install dependencies if not already done
npm install
# Run migrations to set up the database schema
npm run migrate:up
# Optional: Run seeders to populate initial data (not implemented yet)
npm run seednpm run start:devThe NestJS backend should now connect to your local Supabase database!
Use this when deploying against Supabase's official hosted infrastructure.
- Go to supabase.com and sign in.
- Create a free tier account and an organization (if you don't have one).
- Create a new project:
- Select your organization and region.
- Set an initial database password (you can reset it later).
From your project:
- Go to Settings → API.
- Capture:
SUPABASE_URLSUPABASE_ANON_KEYSUPABASE_SERVICE_ROLE_KEY(keep this secret, backend-only).
- Go to Settings → Database → Reset Database Password.
- Use the new value as
DB_PASSWORD.
- From the project dashboard, click the Connect button.
- Choose:
- Type:
PSQL - Source:
Primary database - Method:
Session pooler
- Type:
- Click View parameters and map:
host→DB_HOSTport→DB_PORTuser→DB_USERdatabase→DB_NAME
- Additionally set:
POOL_MODE=session
Example configuration:
# Supabase API
SUPABASE_URL=https://your-project-ref.supabase.co
SUPABASE_ANON_KEY=your-production-anon-key
SUPABASE_SERVICE_ROLE_KEY=your-production-service-role-key
# Managed Supabase database (session pooler)
DB_HOST=aws-0-xxx.pooler.supabase.com
DB_PORT=6543
DB_USER=postgres.your-project-ref
DB_PASSWORD=your-db-password
DB_NAME=postgres
POOL_MODE=session
# Application DB URL (MikroORM)
DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=require&pool_mode=${POOL_MODE}
NODE_ENV=productionnpm run migrate:up# Start local Supabase
supabase start
# Stop local Supabase
supabase stop
# View Supabase status
supabase status
# Access Studio (web UI)
# Open http://localhost:54323 in your browser
# Generate TypeScript types from database
supabase gen types typescript --local > types/supabase.ts# Create a new migration
npm run migration:create
# Run pending migrations
npm run migration:up
# Rollback last migration
npm run migration:down
# Check migration status
npm run migration:pendingYou can access the database using:
- Supabase Studio: http://localhost:54323 (local)
- SQL Editor: Available in Studio
- psql CLI:
psql postgresql://postgres:postgres@localhost:54322/postgres
The MikroORM configuration (backend/src/mikro-orm.config.ts) supports:
- Environment-based connection parameters
- Automatic migration management
- Entity discovery
- Debug logging (disabled in production)
The Supabase configuration (supabase/config.toml) defines:
- Local port mappings (avoid conflicts)
- Database version (PostgreSQL 17)
- API settings
- Auth configuration
- Storage settings
If you get port conflict errors, check if other services are using:
54321(API)54322(Database)54323(Studio)
You can modify these in supabase/config.toml if needed.
-
Verify Supabase is running:
supabase status
-
Check Docker containers:
docker ps
-
Test database connection:
psql postgresql://postgres:postgres@localhost:54322/postgres
If migrations fail:
- Check that Supabase is running
- Verify environment variables are correct
- Check migration files in
backend/src/database/migrations/ - Review logs for specific error messages