Lesson 198 of 2116
Vector DB Basics With pgvector
Store embeddings, search by similarity. The foundation of every RAG system. Postgres plus pgvector gets you there.
Lesson map
What this lesson covers
Learning path
The main moves in order
- 1From Text to Math
- 2embedding
- 3pgvector
- 4cosine similarity
Concept cluster
Terms to connect while reading
Section 1
From Text to Math
An embedding turns text into a list of numbers — a vector. Similar meanings land near each other in that space. A vector database does one thing: find the nearest neighbors to a query vector, fast.
The vector type plus an HNSW index is all pgvector needs. Works inside regular Postgres.
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE docs (
id BIGSERIAL PRIMARY KEY,
body TEXT NOT NULL,
embedding vector(1536) NOT NULL -- 1536 for text-embedding-3-small
);
-- HNSW index for fast approximate nearest-neighbor search
CREATE INDEX ON docs
USING hnsw (embedding vector_cosine_ops);The <=> operator is cosine distance. Lower = more similar. 1 - distance = a nice 0..1 score.
import OpenAI from "openai";
import { Pool } from "pg";
const openai = new OpenAI();
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
async function embed(text: string): Promise<number[]> {
const r = await openai.embeddings.create({
model: "text-embedding-3-small",
input: text,
});
return r.data[0].embedding;
}
export async function insert(body: string) {
const vec = await embed(body);
await pool.query(
"INSERT INTO docs (body, embedding) VALUES ($1, $2)",
[body, `[${vec.join(",")}]`]
);
}
export async function search(query: string, k = 5) {
const vec = await embed(query);
const { rows } = await pool.query(
"SELECT id, body, 1 - (embedding <=> $1) AS score FROM docs ORDER BY embedding <=> $1 LIMIT $2",
[`[${vec.join(",")}]`, k]
);
return rows;
}Key terms in this lesson
The big idea: embed once, search many. Postgres plus pgvector handles production-grade semantic search without a separate vector service.
End-of-lesson quiz
Check what stuck
15 questions · Score saves to your progress.
Tutor
Curious about “Vector DB Basics With pgvector”?
Ask anything about this lesson. I’ll answer using just what you’re reading — short, friendly, grounded.
Progress saved locally in this browser. Sign in to sync across devices.
Related lessons
Keep going
Creators · 60 min
RAG From Scratch
Chunk, embed, store, retrieve, generate. Build retrieval-augmented generation in a single file.
Creators · 40 min
Agents vs. Autocomplete — the Mental Model Shift
Autocomplete is a suggestion. An agent is an actor. The mental model you bring to each is different, and conflating them is the number-one reason teams trip over AI coding.
Creators · 50 min
Test-Driven AI Development
TDD was already the gold standard. Paired with an agent, it becomes the tightest feedback loop in software. Here's the full workflow and the pitfalls.
