Skip to content

Prisma Installation — Set Up Prisma ORM in Your Project

DodaTech Updated 2026-06-28 5 min read

In this tutorial, you will learn about Prisma Installation. We cover key concepts, practical examples, and best practices to help you master this topic.

Setting up Prisma involves installing the CLI, initializing Prisma in your project, configuring the database connection, creating the first Migration, and generating the type-safe client.

What You'll Learn

By the end of this lesson you will install Prisma CLI, initialize a Prisma project, configure a PostgreSQL datasource, run your first migration, generate the Prisma Client, and verify the connection.

Why It Matters

Correct installation is the foundation of every Prisma project. Misconfigured datasources, wrong database URLs, or missing client generation will block all database operations.

Real-World Use

DodaZIP's Prisma setup uses a standardized init script that reads the DATABASE_URL from environment variables, runs migrations on deployment, and generates the client for both development and production.

flowchart LR
    A[npm install @prisma/client] --> B[npx prisma init]
    B --> C[Configure .env]
    C --> D[Define Schema]
    D --> E[npx prisma migrate dev]
    E --> F[npx prisma generate]
    F --> G[Use Client in Code]
    style B fill:#2d3748,color:#fff

Installing the Prisma CLI

Install Prisma as a development dependency.

# Create a new Node.js project
mkdir my-app && cd my-app
npm init -y

# Install Prisma CLI as dev dependency
npm install prisma --save-dev

# Install Prisma Client as dependency
npm install @prisma/client

# Verify installation
npx prisma --version
# install_check.py
# Verify Prisma installation

def check_prisma():
    try:
        import subprocess
        result = subprocess.run(["npx", "prisma", "--version"], capture_output=True, text=True)
        if result.returncode == 0:
            print("Prisma CLI is installed.")
            print(result.stdout[:200])
        else:
            print("Prisma CLI not found.")
    except FileNotFoundError:
        print("npx not available. Ensure Node.js is installed.")

check_prisma()

Initializing Prisma

Create the initial Prisma configuration.

# Initialize Prisma in your project
npx prisma init

# This creates:
# - prisma/schema.prisma (schema file)
# - .env (database connection string)

# To specify a datasource provider:
npx prisma init --datasource-provider postgresql
# or: mysql, sqlite, mongodb
# init_structure.py
# Project structure after init

def project_structure():
    print("Project Structure After prisma init:")
    print()
    print("my-app/")
    print("  prisma/")
    print("    schema.prisma    # Prisma schema definition")
    print("    migrations/      # Migration files (created later)")
    print("  node_modules/")
    print("  .env               # Database connection string")
    print("  package.json")
    print()
    print("Initial schema.prisma:")
    print("  generator client {")
    print('    provider = "prisma-client-js"')
    print("  }")
    print("  datasource db {")
    print('    provider = "postgresql"')
    print('    url      = env("DATABASE_URL")')
    print("  }")

project_structure()

Configuring the Database Connection

Set up the database connection string.

# .env file
DATABASE_URL="postgresql://user:password@localhost:5432/mydb?schema=public"

# Format:
# postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA
# mysql://USER:PASSWORD@HOST:PORT/DATABASE
# sqlite:file:./dev.db
# connection_config.py
# Database URL configuration

def url_examples():
    examples = {
        "PostgreSQL (local)": "postgresql://postgres:pass@localhost:5432/mydb",
        "PostgreSQL (Supabase)": "postgresql://postgres:pass@db.project.supabase.co:5432/postgres",
        "MySQL": "mysql://root:pass@localhost:3306/mydb",
        "SQLite": "file:./dev.db",
        "PostgreSQL (Docker)": "postgresql://postgres:pass@host.docker.internal:5432/mydb",
    }
    
    print("Database URL Examples:")
    for name, url in examples.items():
        print(f"  {name:30s} {url}")
    print()
    print("Security note:")
    print("  Never commit .env to version control")
    print("  Add .env to .gitignore")

url_examples()

First Migration and Client Generation

Create the first migration and generate the client.

# Create a model in schema.prisma first, then:

# Run the initial migration
npx prisma migrate dev --name init

# This:
# 1. Creates the migration SQL file
# 2. Applies it to the database
# 3. Generates the Prisma Client

# Manually generate client (if needed)
npx prisma generate
# first_migration.py
# Migration workflow

def migration_flow():
    print("First Migration Workflow:")
    print()
    print("1. Define models in prisma/schema.prisma")
    print("   model User {")
    print("     id    Int    @id @default(autoincrement())")
    print("     email String @unique")
    print("     name  String?")
    print("   }")
    print()
    print("2. Run: npx prisma migrate dev --name init")
    print("   - Creates migration file")
    print("   - Applies to database")
    print("   - Generates Prisma Client")
    print()
    print("3. Result:")
    print("   - prisma/migrations/20260628_init/migration.sql")
    print("   - node_modules/.prisma/client/ (generated)")

migration_flow()

Common Mistakes

  1. Forgetting prisma generate: After every schema change, the client must be regenerated. The client code is not automatically updated.

  2. Using the wrong database URL format: Each database type has a specific URL format. Check the Prisma documentation for your database.

  3. Not adding .env to .gitignore: The .env file contains database credentials. Expose it, and your database becomes accessible to anyone with repo access.

  4. Running migrations on the wrong database: Always check your DATABASE_URL before running migrations. A typo can run migrations on production.

  5. Installing prisma globally: Always install Prisma as a dev dependency per-project. Global installations cause version conflicts between projects.

Practice Questions

  1. What command installs the Prisma CLI? npm install prisma --save-dev.

  2. What does npx prisma init create? prisma/schema.prisma and a .env file with a placeholder database URL.

  3. What is the difference between prisma and @prisma/client? prisma (CLI) is a dev dependency for migrations and generation. @prisma/client is a runtime dependency.

  4. What does npx prisma migrate dev do? It creates a migration file from schema changes, applies it to the database, and generates the client.

  5. Challenge: Set up Prisma in a new project with PostgreSQL, create a User model, run the initial migration, and verify the database tables were created.

FAQ

Can I install Prisma globally?

You can, but it is best practice to install it per-project to avoid version conflicts.

Do I need Docker for Prisma?

No. Prisma connects to any accessible database. Docker is optional.

What is the difference between migrate dev and migrate deploy?

migrate dev is for development (creates + applies). migrate deploy is for production (applies existing migrations).

Can I use Prisma with TypeScript?

Yes. Prisma is designed for TypeScript and provides full type safety.

How do I point Prisma to a different database?

Change the DATABASE_URL in .env file. Run prisma generate to update the client.

Mini Project

Create a new Node.js project, install Prisma, configure a PostgreSQL connection, define a simple schema with a Product model (id, name, price, createdAt), run the migration, and generate the client.

def project_setup():
    print("Prisma Setup Project Steps:")
    print()
    print("1. mkdir prisma-demo && cd prisma-demo")
    print("2. npm init -y")
    print("3. npm install prisma --save-dev")
    print("4. npm install @prisma/client")
    print("5. npx prisma init --datasource-provider postgresql")
    print("6. Edit .env with DATABASE_URL")
    print("7. Add model to schema.prisma:")
    print("   model Product {")
    print("     id        Int      @id @default(autoincrement())")
    print("     name      String")
    print("     price     Float")
    print("     createdAt DateTime @default(now())")
    print("   }")
    print("8. npx prisma migrate dev --name init")
    print("9. Verify database has products table")

project_setup()

What's Next

Next: Prisma Schema Basics for writing schema definitions.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro