Run This Ai
EN DE

Getting Started with pgvector: Vector Search in PostgreSQL

Add high-performance vector similarity search to PostgreSQL with pgvector — the most popular open-source vector extension for AI applications.

pgvector - Vector Similarity Search for PostgreSQL

What Is pgvector?

pgvector is an open-source extension that brings high-performance vector similarity search directly into PostgreSQL. Instead of running a separate vector database alongside your main database, pgvector lets you store, index, and query vectors — embeddings generated by LLMs or other AI models — right alongside your relational data. With over 22,000 GitHub stars, it has become the most widely adopted PostgreSQL vector extension in the AI ecosystem.

Why Use pgvector for AI Applications?

Modern AI applications rely heavily on vector embeddings — numeric representations of text, images, or audio that capture semantic meaning. When you need to find similar items (semantic search), implement Retrieval-Augmented Generation (RAG), or power recommendation systems, pgvector offers several compelling advantages:

  • No extra infrastructure: Add vectors to your existing PostgreSQL — no new service to deploy, no separate API to manage.
  • Transactional consistency: Vector data benefits from ACID guarantees, foreign keys, and PostgreSQL’s full SQL power.
  • Mature ecosystem: Works with everything that connects to PostgreSQL — Prisma, Drizzle, psycopg2, SQLAlchemy, and more.
  • Multiple index types: IVFFlat for fast approximate search, HNSW for higher recall at the cost of slower build time.
pgvector GitHub Organization

Quick Start with Docker

docker pull pgvector/pgvector:pg16
docker run --name pgvector-db -e POSTGRES_PASSWORD=*** -d pgvector/pgvector:pg16

Once running, connect and enable the extension:

docker exec -it pgvector-db psql -U postgres -c "CREATE EXTENSION vector;"

Creating Vector Embeddings

Store embeddings as a vector(n) column where n is the dimension count (e.g., 768 for BERT, 1536 for OpenAI text-embedding-3-small):

CREATE TABLE documents (
  id SERIAL PRIMARY KEY,
  content TEXT,
  embedding vector(768)
);

Similarity Search

Query for the most similar items using distances like L2, cosine, or inner product:

SELECT content, embedding '<->' '[0.1,0.2]' AS distance
FROM documents
ORDER BY embedding '<->' '[0.1,0.2]'
LIMIT 5;

Indexing for Performance

For production workloads, create an index. IVFFlat is great for balancing speed and accuracy:

CREATE INDEX ON documents USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100);

For higher recall, use HNSW:

CREATE INDEX ON documents USING hnsw (embedding vector_cosine_ops);

Conclusion

pgvector is the simplest path to adding vector search to your AI stack. No separate database, no new query language — just PostgreSQL with superpowers. Whether you are building a RAG pipeline, a semantic search engine, or an AI-powered recommendation system, pgvector gives you production-grade vector search in minutes.

#pgvector #postgresql #vector-search #rag #embeddings