Prerequisites

Recommended: Use Cursor’s integrated terminal for all command-line operations in the course. This provides a seamless development experience with your code editor.

Opening Cursor’s Terminal

You can open the terminal in Cursor in several ways:
Terminal Location: The terminal will appear at the bottom of your Cursor window. You can resize it by dragging the top edge.

Learning Objectives

By the end of this section, you will be able to:
  • Create a free Postgres database using Vercel’s Neon integration
  • Configure environment variables for database connection
  • Set up OpenAI API key for embeddings and chat functionality
  • Run database migrations to create the initial schema

Create Database

You will need a Postgres database to complete this tutorial. We’ll use Vercel’s Neon integration to create a free Postgres database.
Why Vercel + Neon?: This combination provides a free, production-ready PostgreSQL database with pgvector support, perfect for RAG applications.

Setting up Postgres with Vercel

Need a Vercel account? If you don’t have a Vercel account yet, follow the Getting Started with Vercel guide to create one first.
1

Navigate to Vercel

  1. Go to Vercel.com and make sure you’re logged in
  2. Navigate to your team homepage
  3. Click on the Integrations tab
2

Browse the Marketplace

  1. Click Browse Marketplace
  2. Search for Neon in the search bar
  3. Click on Neon and click Install
3

Create Database

  1. On the “Get Started with Neon” page, select your region (e.g., Washington, D.C., U.S. East) - select a region close to where the majority of your users are located
  2. Turn off Auth (for this tutorial)
  3. Click Continue
  4. Name your database (you can use the default name or rename it to something like “RagTutorial”)
  5. Click Create in the bottom right corner
4

Get Connection String

  1. After seeing “Database created successfully”, click Done
  2. You’ll be redirected to your database instance
  3. In the Quick Start section, click Show secrets in .env.local
  4. Copy the full DATABASE_URL environment variable
Save this URL: You’ll need this connection string for your environment variables in the next step.

Configure Environment Variables

Now that you have your database, you need to add the connection string as an environment variable.
1

Create Environment File

Make a copy of the .env.example file and rename it to .env:Option 1: Using the command line
Terminal
cp .env.example .env
Option 2: Using Cursor’s file manager
  1. In the file explorer (left sidebar), right-click on .env.example
  2. Select Copy from the context menu
  3. Right-click in the file explorer area
  4. Select Paste from the context menu
  5. Rename the copied file from .env.example (copy) to .env
2

Configure Database URL

  1. Open the new .env file
  2. You should see an item called DATABASE_URL
  3. Copy in your database connection string after the equals sign
.env
DATABASE_URL=postgresql://username:password@host:port/database
3

Add OpenAI API Key

  1. Go to platform.openai.com and sign in to your account
  2. Click on the Settings gear icon in the top right corner
  3. Select “API Keys” on the left side bar
  4. Click “Create new secret key”
  5. Give your API key a name (e.g., “RAG Tutorial”) and select the Default project
  6. Click “Create secret key”
  7. Important: Copy the API key immediately - you won’t be able to see it again
  8. Once you have your API key, paste it into your .env file:
.env
OPENAI_API_KEY=sk-your-openai-api-key-here
DATABASE_URL=postgresql://username:password@host:port/database
Security: Never commit your .env file to version control. It’s already in .gitignore for safety.

Migrate Database

Once you have your Postgres database and environment variables set up, you can run your first database migration.
1

Run Migration

Run the following command to set up your database schema in your terminal:
npm run db:migrate
You should see the following output:
Terminal
 Running migrations...
 Migrations completed in 1986 ms

2

What Happens

This command will:
  1. First add the pgvector extension to your database
  2. Then create a new table for your resources schema that is defined in lib/db/schema/resources.ts
  3. This schema has four columns: id, content, createdAt, and updatedAt
pgvector Extension: This extension enables PostgreSQL to store and query vector embeddings, which is essential for RAG functionality.

Verify Database Setup

1

Check Migration Success

  1. Look for success messages in your terminal
  2. You should see output indicating that the pgvector extension was added and tables were created
2

Optional: View Database

You have two options to view your database:Option 1: Drizzle Studio (Local)Run this command in your terminal:
npm run db:studio
You should see the following at the end of the console output:
Terminal
Drizzle Studio is up and running on https://local.drizzle.studio

Open the link in your browser to see a web interface where you can see your database tables.Option 2: Vercel Dashboard (Cloud)
  1. Go to your Vercel Dashboard
  2. Navigate to the Storage tab
  3. Find your Neon database and click on it
  4. Click on the Open in Neon button to view your database in Neon console
  5. Click Tables on the left sidebar. You should see a Resources table with 4 columns: id, content, createdAt, and updatedAt with no data
Both options work: Drizzle Studio is great for local development, while the Vercel dashboard is convenient for viewing your cloud database directly.

Troubleshooting


Reflection Questions

Take a moment to reflect on what you’ve learned. Think about it first and then use AI to refine your answers.