Storing Embeddings in a Database

Ma Mahalakshmi V Updated 16 Sep 2026
8 min read ·Lesson 120 of 224

Storing Embeddings in a Database

The in-memory search engine from Lesson 5 keeps every document and vector in a Python list, which works for a demo but not for an application that needs to persist data across restarts, handle more documents than fit comfortably in memory, or let multiple processes query the same index. This lesson covers storing embeddings in an actual database — using PostgreSQL with the pgvector extension as the running example, since it is a widely used, realistic choice that lets a team keep vector search in the same database as the rest of their application data, without operating a second specialized system.

Why a Real Database Instead of an In-Memory List

Three concrete limitations of the in-memory approach motivate this move:

  1. Persistence. An in-memory list disappears when the process restarts. A database keeps the data durably on disk, with the same reliability guarantees (backups, replication) as any other application data.
  2. Scale beyond available RAM. A database can index and query far more vectors than would comfortably fit in one process's memory, and can page data in from disk as needed.
  3. Concurrent access. A web application typically has multiple worker processes handling requests simultaneously. A shared database lets all of them query the same up-to-date index; a Python list held in one process's memory cannot be shared that way without extra infrastructure.

PostgreSQL With pgvector: The Idea

pgvector is a PostgreSQL extension that adds a vector column type and similarity operators directly usable in SQL. The core idea: store each document's embedding as a native column value, and let the database compute similarity as part of an ordinary SELECT query.

Note: pgvector is a real, actively maintained open-source extension, but exact installation steps, operator names, and default index parameters can change between versions. Confirm the current syntax against the pgvector project documentation (and your hosting provider's docs, if using a managed Postgres service) before running this in production.

A representative schema:

CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE documents (
    id          BIGSERIAL PRIMARY KEY,
    external_id TEXT UNIQUE NOT NULL,
    content     TEXT NOT NULL,
    metadata    JSONB DEFAULT '{}',
    embedding   VECTOR(1536),
    model_name  TEXT NOT NULL,
    created_at  TIMESTAMPTZ DEFAULT now()
);

Why each column exists: external_id is a stable identifier from the source system (a ticket ID, a URL) used to update or delete a specific document later without depending on the database's internal auto-incrementing id. content keeps the original text so a search result can be displayed. metadata is a JSONB column for structured attributes (category, date, author) used for filtering — the subject of Lesson 7. embedding is the vector itself, declared with a fixed dimension (1536 here, matching a chosen embedding configuration) — pgvector requires the dimension to be fixed per column. model_name records which embedding model (and configuration) produced the vector.

Why store model_name at all? This directly addresses a mistake flagged in Lesson 2: vectors from different models, or the same model with different dimensions settings, are not comparable. Recording which model produced each stored vector makes it possible to detect and safely handle a migration — for example, re-embedding everything after switching models — instead of silently mixing incompatible vectors in the same similarity search.

Inserting Embeddings

INSERT INTO documents (external_id, content, metadata, embedding, model_name)
VALUES (
    'article-482',
    'Return policy for electronics: items can be returned within 30 days.',
    '{"category": "returns", "published_at": "2026-01-15"}',
    '[0.012, -0.034, 0.087, ...]',
    'text-embedding-4'
);

The vector is written as a bracketed list of numbers, which pgvector parses into its native vector type. In real code this string is generated from the Python list returned by the embeddings API — never typed by hand.

import json


def insert_document(conn, external_id: str, content: str, metadata: dict,
                     embedding: list[float], model_name: str) -> None:
    """Insert one document row. `conn` is a DB-API style connection
    (e.g. psycopg2 or psycopg3) with a `.cursor()` method.
    """
    vector_literal = "[" + ",".join(str(x) for x in embedding) + "]"
    with conn.cursor() as cur:
        cur.execute(
            """
            INSERT INTO documents (external_id, content, metadata, embedding, model_name)
            VALUES (%s, %s, %s, %s, %s)
            ON CONFLICT (external_id) DO UPDATE
                SET content = EXCLUDED.content,
                    metadata = EXCLUDED.metadata,
                    embedding = EXCLUDED.embedding,
                    model_name = EXCLUDED.model_name
            """,
            (external_id, content, json.dumps(metadata), vector_literal, model_name),
        )
    conn.commit()

ON CONFLICT (external_id) DO UPDATE makes this function an "upsert" — inserting a new row if external_id has not been seen, or updating the existing row if it has. Why does this matter? Real content changes over time: a document gets edited, re-embedded, or its metadata updated. Without an upsert, re-indexing the same source document would either fail (if external_id has a uniqueness constraint, which it does here) or create duplicate rows with stale copies alongside the new one. The upsert pattern makes re-indexing idempotent — running it twice with the same input produces the same end state as running it once.

Querying by Similarity

pgvector provides operators for common distance measures directly in SQL. The <=> operator computes cosine distance (1 minus cosine similarity, so smaller is more similar):

SELECT external_id, content, 1 - (embedding <=> %(query_vector)s) AS similarity
FROM documents
WHERE model_name = 'text-embedding-4'
ORDER BY embedding <=> %(query_vector)s
LIMIT 5;

Note: Operator names (<=> for cosine distance, <-> for Euclidean, <#> for negative inner product) and their exact semantics are specific to pgvector's current version — confirm against the extension's documentation before relying on them, as these details are exactly the kind of thing that can change between major versions.

Why ORDER BY ... LIMIT instead of fetching everything and sorting in Python? Doing the distance computation and sorting inside the database lets it use an index (covered next) to avoid scanning every row, and avoids pulling potentially millions of vectors across the network into the application process just to sort them there. This is the same principle as Lesson 4's point about vectorized batch comparison — push the numeric work to where it can be done most efficiently, which for a large, persisted collection is the database itself.

def search_documents(conn, query_embedding: list[float], model_name: str, limit: int = 5) -> list[dict]:
    vector_literal = "[" + ",".join(str(x) for x in query_embedding) + "]"
    with conn.cursor() as cur:
        cur.execute(
            """
            SELECT external_id, content, 1 - (embedding <=> %s) AS similarity
            FROM documents
            WHERE model_name = %s
            ORDER BY embedding <=> %s
            LIMIT %s
            """,
            (vector_literal, model_name, vector_literal, limit),
        )
        rows = cur.fetchall()
    return [{"id": r[0], "content": r[1], "similarity": float(r[2])} for r in rows]

Indexing for Speed at Scale

Without an index, pgvector computes the exact distance to every row for every query — correct, but slow once a table has hundreds of thousands or millions of rows. pgvector supports approximate-nearest-neighbor indexes (such as HNSW) that trade a small amount of accuracy for a large speed improvement:

CREATE INDEX ON documents
USING hnsw (embedding vector_cosine_ops);

Note: Index types available (HNSW, IVFFlat), their build/query parameters, and which one is recommended by default have changed across pgvector releases. Check current documentation for the recommended index type and parameters for your version and dataset size before creating one in production.

Why "approximate" is an acceptable trade here: An approximate index occasionally misses the single mathematically closest vector in favor of one that is nearly as close, in exchange for query times that stay fast as the table grows into the millions of rows. For semantic search, where the difference between the 1st and 3rd most similar result is rarely meaningful to the end user, this trade is almost always worth it — an exact index only makes sense for small tables or when perfect recall is a hard requirement, which Lesson 9's evaluation methodology can help confirm one way or the other for a specific application.

Dedicated Vector Databases: A Brief Comparison

pgvector is one option among several. Dedicated vector databases (such as Pinecone, Weaviate, Qdrant, or Milvus) are purpose-built for vector search and can offer more specialized indexing options and scaling characteristics at very large sizes.

ConsiderationPostgreSQL + pgvectorDedicated vector database
Operational complexityOne database to run, if you already use PostgresAn additional system to deploy, monitor, and secure
Combining vector search with relational dataNative — one SQL query with joins, filtersOften requires syncing data between two systems
Specialized scaling for huge vector countsGood up to large scale; managed by tuning indexesPurpose-built for very large-scale vector workloads
Ecosystem maturity for pure vector searchGrowing rapidlyMature, vector-search-specific tooling

When to choose which: If an application already stores its data in PostgreSQL and the vector collection is in the range of thousands to low millions of documents, pgvector avoids the operational cost of running and syncing a second database — this is the practical default for most applications built with this course's stack. A dedicated vector database becomes more attractive at very large scale (tens of millions of vectors and beyond) or when a team's primary datastore is not relational at all.

Common Mistakes

  • Storing vectors as plain text or JSON in a generic column instead of a native vector type. This works for storage but loses the ability to use similarity operators and specialized indexes directly in SQL, forcing similarity computation back into the application.
  • Not recording which embedding model produced each vector. As covered above, this makes it easy to silently mix incompatible vectors after a model change, corrupting every similarity comparison that spans the mix.
  • Building an index before understanding the accuracy/speed trade-off it makes. Approximate indexes are usually the right choice at scale, but blindly applying default parameters without reading current documentation can produce disappointing recall on a specific dataset.

Best Practices

  • Use upserts keyed by a stable external ID, not raw inserts, so re-indexing content is idempotent and safe to run repeatedly.
  • Store metadata alongside embeddings in the same row, not in a separate system, so filtering (Lesson 7) can be combined with similarity search in a single query.
  • Confirm operator names, index types, and default parameters against current documentation before deploying, since this is precisely the kind of database-specific syntax that changes between versions, as flagged throughout this lesson.

0 Comments

Reviewed before they appear

No comments yet.

OpenAI SDK
Introduction to the OpenAI SDK Setting Up Python Creating an API Key Your First Call — client.responses.create() and response.output_text Understanding Billing, Credits, and What a Request Costs Why Responses Replaced Chat Completions Anatomy of a Request: model, input, and instructions Anatomy of a Response: The Typed output Array, Not Just Text Roles: User, Assistant, and Developer/System Choosing a Model, and Reading the Models Page Instead of Memorizing Names Instructions vs. Input Writing Prompts That Get Consistent Results Few-Shot Examples Reasoning Models and the reasoning Parameter Debugging a Prompt That Misbehaves Why Streaming Matters for User Experience stream=True and Iterating Over Events Handling the Event Types You Actually Care About Background Mode for Long-Running Jobs Project — Add Live Streaming to Your Chatbot The Problem With Parsing Free Text JSON Schema and Strict Mode Pydantic Models With the SDK's Parse Helpers Handling Refusals and Validation Failures Project — A Resume-to-JSON Extractor Working With input_image input_file, PDFs, and the Files API Image Generation Speech-to-Text and Text-to-Speech Project: A PDF Question-Answering Script What Function Calling Is Defining a Tool Schema The Full Loop Multiple Tools Errors, Timeouts, and Untrusted Arguments Project: A Weather Assistant Web Search File Search and Vector Stores Code Interpreter Remote MCP Servers and Connectors Project: A Research Assistant What an Embedding Is, Without the Maths Generating and Storing Embeddings Similarity Search From Scratch Hosted Vector Stores vs. Rolling Your Own A Small RAG App Over a Folder of Notes Agents vs. a Single API Call — When You Need One pip install openai Giving Agents Tools Handoffs and Multi-Agent Triage Guardrails and Approvals Tracing and Observing What Your Agent Did A Multi-Agent Support Desk Error Codes and What Each One Means Retries, Timeouts, and Backoff Rate Limits and Spend Limits Prompt Caching and Cost Optimisation The Batch API for Bulk Work Async Clients and Concurrency Moderation and Safety Best Practices Designing the App Backend With FastAPI Streaming to a Simple Frontend Deploying and a Cost/Safety Checklist Why Web Search Is Useful for Current Information Using the Web Search Tool with the Responses API Configuring Search Behavior for Application Use Cases Understanding Citations and Source Attribution Where to Go Next Building a Research Assistant with Web Search Combining Web Search with Structured Outputs Handling Conflicting or Low-Quality Web Sources Reducing Unsupported Claims with Grounded Generation Testing Freshness-Sensitive AI Answers Production Considerations for Web-Grounded Applications Understanding File Search and Retrieval-Augmented Generation Creating and Organizing Vector Stores Uploading Documents for Retrieval Connecting Vector Stores to Responses API Requests Designing Document Metadata and Filtering Strategies Building a PDF Question-Answering Application Improving Retrieval Quality With Better Document Preparation Handling Missing Evidence and Retrieval Failures Combining File Search With Web Search Building a Production Knowledge-Base Assistant What the Code Interpreter Tool Is Designed For Running Python-Based Analysis Through the OpenAI SDK Uploading Datasets for Analysis Analyzing CSV and Spreadsheet Data Generating Charts and Data Summaries Handling Generated Files and Downloadable Artifacts Building a Data-Analysis Assistant Combining Code Execution with Structured Outputs Validating Generated Calculations and Results Security and Sandbox Considerations for Code Execution Understanding Multimodal Input with the OpenAI SDK Sending Images to a Model Image Analysis from URLs and Uploaded Files Extracting Text and Information from Screenshots Building an Image-Question-Answering Application Combining Image Input with Structured Output Analyzing Multiple Images in One Request Handling Image Quality and Input Limitations Designing Multimodal Prompts for Reliable Results Building a Practical Vision-Powered Python Application Understanding Speech-to-Text and Text-to-Speech Workflows Transcribing Audio with the OpenAI SDK Working with Uploaded Audio Files Handling Timestamps and Transcription Metadata Building a Meeting Transcription Workflow Generating Spoken Responses from Text Handling Long Audio and Processing Failures Combining Audio with Text and Tool Calling Building an End-to-End Python Voice Application What Embeddings Are and When to Use Them Generating Embeddings With the OpenAI API Preparing Text for Embedding Comparing Vectors With Cosine Similarity Building a Simple Semantic Search Engine in Python Storing Embeddings in a Database Metadata Filtering for Semantic Search Chunking Strategies for Better Retrieval Evaluating Semantic Search Quality Building a Document Similarity Application When Batch Processing Makes Sense Designing Large-Volume AI Processing Pipelines Using Asynchronous Python with the OpenAI SDK Running Concurrent Requests Safely Controlling Concurrency and Avoiding Rate Limits Tracking Batch Job Progress Handling Partial Failures in Bulk Workloads Retrying Failed Items Without Duplicating Successful Work Designing Resumable AI Processing Jobs Building a Production Batch-Processing Pipeline Batch Processing Makes Sense Large-Scale AI Processing Pipelines Async Python with OpenAI SDK Safe Concurrent Requests Concurrency & Rate Limits Batch Progress Tracking Partial Failure Handling Safe Retry Handling Resumable AI Jobs Production Batch Pipeline System–User Data Separation Reusable App Instructions Prompt Templates & Variables Extraction & Classification Prompts Summarization & Transformation Prompts Explicit Output Requirements Prompt Version Management Prompt Testing & Evaluation Reusable Python Prompt Library API Key Security Secure API Key Storage Secure Secret Management Prompt Injection Prevention Trusted vs. Untrusted Content Tool Argument Validation Sensitive Data Handling Secure Logging AI Action Authorization Production AI Security Checklist Why AI Applications Need Evaluation Beyond Unit Tests Unit Testing OpenAI SDK Integration Code Mocking API Responses in Python Tests Testing Structured Outputs Against Schemas Testing Tool-Calling Workflows Building a Small Evaluation Dataset Measuring Accuracy, Consistency, and Failure Rates Regression Testing Prompts and Model Changes Human Evaluation Versus Automated Evaluation Creating a Repeatable Evaluation Pipeline AI Request Monitoring Token Cost Management Usage Metrics Design Reducing Model Calls Prompt & Context Optimization Model Selection & Optimization AI Caching Strategies Interactive Latency Optimization Usage Dashboards & Budget Alerts Performance & Cost Checklist Every API Call Starts Fresh Fixing API Statelessness Server-Side Conversation Memory Limits of Response Chaining What We're Building Conversation Memory Challenges Preparing an OpenAI SDK Application for Deployment Environment-Specific Configuration for Development and Production Deploying a Python AI Service with Docker Container Health Checks and Startup Configuration Managing Secrets in Cloud Deployments Background Workers for Long-Running AI Tasks Queues and Asynchronous Job Architectures Scaling AI Workloads Horizontally Monitoring Production Incidents and Failures Production Deployment Checklist for OpenAI SDK Applications Reusable OpenAI Service Classes AI Client Dependency Injection Typed AI Responses Python Configuration Management AI Request Decorators Centralized AI Error Handling Clean SDK Abstractions Reusable OpenAI Utilities Internal AI Python Libraries SDK Integration Maintenance Production AI Chatbot Document Q&A System Web Research Assistant Customer Support Agent AI Data Analysis Assistant Image Analysis App Meeting Transcription & Summary Semantic Document Search Multi-Tool AI Agent Production OpenAI SDK App Why "It Looked Fine When I Tested It" Isn't Enough Timing Note Status Note Pre-Decision Status Note Current Availability Note
Ask about this post
AI Ask about this post

Ask questions about Storing Embeddings in a Database and get answers drawn from it.

Signed-in readers only.