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:Option 1: Using the menu
Option 1: Using the menu
Option 2: Using keyboard shortcut
Option 2: Using keyboard shortcut
- macOS: `Ctrl + “ (backtick)
- Windows/Linux: `Ctrl + “ (backtick)
Option 3: Using the command palette
Option 3: Using the command palette
- Press
Cmd/Ctrl + Shift + P
to open the command palette - Type “Terminal: Create New Terminal”
- Press Enter
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
- Go to Vercel.com and make sure you’re logged in
- Navigate to your team homepage
- Click on the Integrations tab
2
Browse the Marketplace
- Click Browse Marketplace
- Search for
Neon
in the search bar - Click on Neon and click Install
3
Create Database
- 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
- Turn off Auth (for this tutorial)
- Click Continue
- Name your database (you can use the default name or rename it to something like “RagTutorial”)
- Click Create in the bottom right corner
4
Get Connection String
- After seeing “Database created successfully”, click Done
- You’ll be redirected to your database instance
- In the Quick Start section, click Show secrets in
.env.local
- 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 Option 2: Using Cursor’s file manager
.env.example
file and rename it to .env
:Option 1: Using the command lineTerminal
- In the file explorer (left sidebar), right-click on
.env.example
- Select
Copy
from the context menu - Right-click in the file explorer area
- Select
Paste
from the context menu - Rename the copied file from
.env.example (copy)
to.env
2
Configure Database URL
- Open the new
.env
file - You should see an item called
DATABASE_URL
- Copy in your database connection string after the equals sign
.env
3
Add OpenAI API Key
- Go to platform.openai.com and sign in to your account
- Click on the Settings gear icon in the top right corner
- Select “API Keys” on the left side bar
- Click “Create new secret key”
- Give your API key a name (e.g., “RAG Tutorial”) and select the Default project
- Click “Create secret key”
- Important: Copy the API key immediately - you won’t be able to see it again
- Once you have your API key, paste it into your
.env
file:
.env
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:You should see the following output:
Terminal
2
What Happens
This command will:
- First add the pgvector extension to your database
- Then create a new table for your resources schema that is defined in
lib/db/schema/resources.ts
- This schema has four columns:
id
,content
,createdAt
, andupdatedAt
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
- Look for success messages in your terminal
- 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:You should see the following at the end of the console output:Open the link in your browser to see a web interface where you can see your database tables.Option 2: Vercel Dashboard (Cloud)
Terminal
- Go to your Vercel Dashboard
- Navigate to the Storage tab
- Find your Neon database and click on it
- Click on the Open in Neon button to view your database in Neon console
- Click Tables on the left sidebar. You should see a Resources table with 4 columns:
id
,content
,createdAt
, andupdatedAt
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
Migration fails with connection errors
Migration fails with connection errors
Possible causes and solutions:
-
Check your DATABASE_URL format - Ensure it follows the pattern:
postgresql://username:password@host:port/database
- Verify your database is running - Check your Vercel dashboard to ensure the database is active
- Check network connectivity - Ensure your local machine can reach the database host
- Verify credentials - Double-check the username and password in your connection string
pgvector extension fails to install
pgvector extension fails to install
This is usually resolved by:
- Using Neon database - Neon comes with pgvector pre-installed
- Checking database permissions - Ensure your database user has CREATE EXTENSION privileges
- Contacting support - If using a different provider, check their pgvector support