Loading lesson…
Store embeddings, search by similarity. The foundation of every RAG system. Postgres plus pgvector gets you there.
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.
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 vector type plus an HNSW index is all pgvector needs. Works inside regular Postgres.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;
}The <=> operator is cosine distance. Lower = more similar. 1 - distance = a nice 0..1 score.The big idea: embed once, search many. Postgres plus pgvector handles production-grade semantic search without a separate vector service.
15 questions · take it digitally for instant feedback at tendril.neural-forge.io/learn/quiz/end-progx-vector-db-creators
What transformation does an embedding perform on text data?
In vector space, what does it mean for two texts to be 'near' each other?
What is the primary function of a vector database?
Why must all embeddings in a table share the same dimension?
What happens if you store embeddings from different models in the same table without transformation?
What type of nearest neighbors does HNSW return?
For which use case is HNSW's approximate search NOT ideal?
What does increasing the ef_search parameter in HNSW typically affect?
In vector databases, cosine distance measures what?
What is the efficiency advantage of 'embed once, search many'?
What capability does pgvector add to standard PostgreSQL?
What is a key benefit of using pgvector over a separate vector database service?
What is pgvector's primary use case in AI applications?
Why might approximate results be acceptable for RAG applications?
What would happen to search results if you queried with a vector of the wrong dimension?