Prerequisites
Ensure you have PostgreSQL installed and running on your system before proceeding.
- PostgreSQL 12 or higher
- Node.js 18 or higher
- Prisma CLI (installed as dev dependency)
Database configuration
Environment variables
The application uses theDATABASE_URL environment variable to connect to PostgreSQL. Create a .env file in your project root:
.env
Connection string format
Connection string format
The PostgreSQL connection string follows this format:Parameters:
USER- PostgreSQL usernamePASSWORD- User passwordHOST- Database server hostname (e.g.,localhost)PORT- PostgreSQL port (default:5432)DATABASE- Database nameschema- PostgreSQL schema (usuallypublic)
Connection string examples
Never commit your
.env file to version control. Add it to your .gitignore file to keep credentials secure.Creating the database
Using PostgreSQL CLI
Connect to PostgreSQL and create the database:Using Docker
Alternatively, run PostgreSQL in a Docker container:Installing Prisma
Prisma is included in the project dependencies. Install all dependencies:@prisma/client(v5.7.0) - Prisma Client for database queriesprisma(v5.7.0) - Prisma CLI for migrations and schema management
Running migrations
Generate Prisma Client
First, generate the Prisma Client based on your schema:node_modules/@prisma/client.
Create and apply migrations
Development workflow
Development workflow
During development, use This command:
prisma migrate dev to create and apply migrations:- Creates a new migration in
prisma/migrations/ - Applies the migration to your database
- Generates Prisma Client
- Prompts you to reset the database if needed
Production deployment
Production deployment
In production, use This applies all pending migrations without creating new ones or resetting the database.
prisma migrate deploy to apply existing migrations:Migration commands
The Bets and Users models contain check constraints that require additional setup. Prisma will guide you through this during migration. See the Prisma check constraints documentation for details.
Database seeding
While the project doesn’t include a seed file by default, you can create one to populate initial data.Create seed file
Createprisma/seed.ts in your project:
prisma/seed.ts
Configure package.json
Add the seed command to yourpackage.json:
package.json
Run seed
Seeding runs automatically when you use
prisma migrate reset or prisma migrate dev.Prisma Studio
Prisma Studio provides a visual interface to view and edit your database:http://localhost:5555 where you can:
- Browse all tables and records
- Edit data directly
- Filter and search records
- View relationships between models
Using Prisma Client
After setup, use Prisma Client in your application:Troubleshooting
Connection refused
Connection refused
If you see “connection refused” errors:
- Verify PostgreSQL is running:
sudo service postgresql status - Check the connection string in
.env - Ensure PostgreSQL is listening on the correct port
- Verify firewall settings aren’t blocking connections
Authentication failed
Authentication failed
If authentication fails:
- Verify username and password in
DATABASE_URL - Check PostgreSQL user permissions
- Ensure the database exists:
psql -l - Try connecting manually:
psql -U username -d database_name
Migration conflicts
Migration conflicts
If migrations conflict or fail:
- Check migration status:
npx prisma migrate status - Resolve conflicts manually or reset:
npx prisma migrate reset - Review
prisma/migrations/directory for issues - In production, never use
migrate reset- always usemigrate deploy
Prisma Client out of sync
Prisma Client out of sync
If Prisma Client doesn’t match your schema:
- Regenerate the client:
npx prisma generate - Restart your development server
- Clear node_modules and reinstall if issues persist
Best practices
Follow these practices for a smooth database workflow:
- Always backup your database before running migrations in production
- Use migrations instead of
prisma db pushfor production environments - Version control your migration files in
prisma/migrations/ - Test migrations in a staging environment before production
- Use connection pooling for production deployments (e.g., PgBouncer)
- Monitor queries in production using Prisma’s query logging
- Set appropriate timeouts for database connections
Next steps
Now that your database is set up:- Review the database schema to understand all models and relationships
- Implement API endpoints using Prisma Client
- Add data validation and error handling
- Set up database backups and monitoring