Skip to main content
This guide walks you through setting up the PostgreSQL database for F1 PitLane Predict using Prisma ORM.

Prerequisites

Ensure you have PostgreSQL installed and running on your system before proceeding.
You’ll need:
  • PostgreSQL 12 or higher
  • Node.js 18 or higher
  • Prisma CLI (installed as dev dependency)

Database configuration

Environment variables

The application uses the DATABASE_URL environment variable to connect to PostgreSQL. Create a .env file in your project root:
.env
DATABASE_URL="postgresql://username:password@localhost:5432/f1_pitlane_predict?schema=public"
The PostgreSQL connection string follows this format:
postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA
Parameters:
  • USER - PostgreSQL username
  • PASSWORD - User password
  • HOST - Database server hostname (e.g., localhost)
  • PORT - PostgreSQL port (default: 5432)
  • DATABASE - Database name
  • schema - PostgreSQL schema (usually public)

Connection string examples

DATABASE_URL="postgresql://postgres:mypassword@localhost:5432/f1_pitlane_predict?schema=public"
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:
psql -U postgres
CREATE DATABASE f1_pitlane_predict;

Using Docker

Alternatively, run PostgreSQL in a Docker container:
docker run --name f1-postgres \
  -e POSTGRES_PASSWORD=mypassword \
  -e POSTGRES_DB=f1_pitlane_predict \
  -p 5432:5432 \
  -d postgres:16

Installing Prisma

Prisma is included in the project dependencies. Install all dependencies:
npm install
This installs:
  • @prisma/client (v5.7.0) - Prisma Client for database queries
  • prisma (v5.7.0) - Prisma CLI for migrations and schema management

Running migrations

Generate Prisma Client

First, generate the Prisma Client based on your schema:
npx prisma generate
This creates the type-safe Prisma Client in node_modules/@prisma/client.

Create and apply migrations

During development, use prisma migrate dev to create and apply migrations:
npx prisma migrate dev --name init
This command:
  1. Creates a new migration in prisma/migrations/
  2. Applies the migration to your database
  3. Generates Prisma Client
  4. Prompts you to reset the database if needed
In production, use prisma migrate deploy to apply existing migrations:
npx prisma migrate deploy
This applies all pending migrations without creating new ones or resetting the database.

Migration commands

npx prisma migrate dev --name your_migration_name
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

Create prisma/seed.ts in your project:
prisma/seed.ts
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

async function main() {
  // Seed Teams
  const redBull = await prisma.teams.create({
    data: {
      teamId: 'red_bull',
      name: 'Red Bull Racing',
      nationality: 'Austrian',
      url: 'http://www.redbullracing.com/',
      teamLogo: '/logos/red_bull.png'
    }
  })

  // Seed Drivers
  await prisma.drivers.create({
    data: {
      driverId: 'max_verstappen',
      code: 'VER',
      permanentNumber: 1,
      givenName: 'Max',
      familyName: 'Verstappen',
      dateOfBirth: new Date('1997-09-30'),
      nationality: 'Dutch',
      url: 'http://en.wikipedia.org/wiki/Max_Verstappen',
      driverImage: '/drivers/verstappen.png',
      teamId: redBull.teamId
    }
  })

  // Add more seed data as needed
  console.log('Database seeded successfully')
}

main()
  .catch((e) => {
    console.error(e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  })

Configure package.json

Add the seed command to your package.json:
package.json
{
  "prisma": {
    "seed": "ts-node prisma/seed.ts"
  }
}

Run seed

npx prisma db 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:
npx prisma studio
This opens a browser window at 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:
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

// Query users
const users = await prisma.users.findMany()

// Create a bet
const bet = await prisma.bets.create({
  data: {
    userId: 1,
    raceId: 1,
    driverId: 'max_verstappen',
    amount: 10.00,
    odds: 1.85,
    betStatus: 'pending'
  }
})

// Get race with results
const race = await prisma.races.findUnique({
  where: { raceId: 1 },
  include: {
    Results: {
      include: {
        Drivers: true
      }
    }
  }
})

Troubleshooting

If you see “connection refused” errors:
  1. Verify PostgreSQL is running: sudo service postgresql status
  2. Check the connection string in .env
  3. Ensure PostgreSQL is listening on the correct port
  4. Verify firewall settings aren’t blocking connections
If authentication fails:
  1. Verify username and password in DATABASE_URL
  2. Check PostgreSQL user permissions
  3. Ensure the database exists: psql -l
  4. Try connecting manually: psql -U username -d database_name
If migrations conflict or fail:
  1. Check migration status: npx prisma migrate status
  2. Resolve conflicts manually or reset: npx prisma migrate reset
  3. Review prisma/migrations/ directory for issues
  4. In production, never use migrate reset - always use migrate deploy
If Prisma Client doesn’t match your schema:
  1. Regenerate the client: npx prisma generate
  2. Restart your development server
  3. 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 push for 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:
  1. Review the database schema to understand all models and relationships
  2. Implement API endpoints using Prisma Client
  3. Add data validation and error handling
  4. Set up database backups and monitoring

Build docs developers (and LLMs) love