Concept Beginner to Intermediate · 3 min read

What is pgvector for PostgreSQL

Quick answer
pgvector is an open-source PostgreSQL extension that adds vector data types and similarity search functions to the database. It enables efficient storage and querying of high-dimensional vectors, which are essential for AI tasks like semantic search and retrieval-augmented generation (RAG).
pgvector is a PostgreSQL extension that adds vector data types and similarity search capabilities to enable AI-powered semantic search and retrieval within the database.

How it works

pgvector extends PostgreSQL by introducing a new vector data type to store fixed-length arrays of floating-point numbers, representing embeddings generated by AI models. It provides similarity functions like cosine distance, Euclidean distance, and inner product to compare vectors efficiently. Think of it as adding a new column type specialized for AI embeddings, allowing you to run similarity queries directly inside your relational database without needing a separate vector search engine.

Analogy: Imagine your database is a library, and pgvector adds a new kind of book index that organizes books by their meaning (embedding vectors) rather than just titles or authors, so you can find related content by concept, not just keywords.

Concrete example

This example shows how to create a table with a vector column, insert embeddings, and query for the closest vectors using cosine similarity.

python
CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE documents (
  id SERIAL PRIMARY KEY,
  content TEXT,
  embedding vector(3) -- 3-dimensional vector for demo
);

INSERT INTO documents (content, embedding) VALUES
('AI is transforming software development', '[0.1, 0.2, 0.3]'),
('PostgreSQL supports vector search', '[0.2, 0.1, 0.4]'),
('Vector databases enable semantic search', '[0.4, 0.4, 0.5]');

-- Query for the closest document to a given vector
SELECT id, content, embedding <=> '[0.1, 0.2, 0.25]' AS distance
FROM documents
ORDER BY distance
LIMIT 1;
output
 id |               content                |   distance   
----+------------------------------------+--------------
  1 | AI is transforming software development | 0.0433
(1 row)

When to use it

Use pgvector when you want to integrate vector similarity search directly into your PostgreSQL database without deploying a separate vector search system. It's ideal for AI applications like retrieval-augmented generation (RAG), semantic search, recommendation engines, and natural language understanding where embeddings are stored and queried alongside relational data.

Do not use pgvector if you require extremely large-scale vector search with billions of vectors or advanced indexing structures like HNSW; specialized vector databases may be better suited in those cases.

Key terms

TermDefinition
pgvectorPostgreSQL extension adding vector data types and similarity search functions.
VectorA fixed-length array of floats representing embeddings from AI models.
EmbeddingNumerical representation of text or data capturing semantic meaning.
Cosine similarityA metric measuring the cosine of the angle between two vectors.
Retrieval-Augmented Generation (RAG)An AI architecture combining retrieval of documents with language model generation.

Key Takeaways

  • pgvector enables vector similarity search inside PostgreSQL by adding a vector data type and distance functions.
  • Store AI embeddings directly in your database and query them efficiently for semantic search and RAG workflows.
  • Ideal for moderate-scale vector search integrated with relational data; not for massive-scale vector databases.
  • Supports common similarity metrics like cosine distance, Euclidean distance, and inner product.
  • Simplifies AI app architecture by avoiding separate vector search infrastructure.
Verified 2026-04
Verify ↗