PostgreSQL

pgvector: Semantic Search and AI Embeddings in PostgreSQL

Add vector similarity search to PostgreSQL with pgvector. Covers HNSW and IVFFlat indexes, cosine similarity queries, OpenAI embedding integration, and hybrid search.

JusDB Team
October 3, 2025
Updated June 20, 2026
5 min read

pgvector adds vector similarity search to PostgreSQL, making it a viable vector database for AI embeddings, semantic search, and recommendation systems — without adding a separate service.

Install pgvector

bash
# Ubuntu/Debian
apt-get install postgresql-15-pgvector

# Enable in database
CREATE EXTENSION vector;

Store Embeddings

sql
CREATE TABLE documents (
  id        BIGSERIAL PRIMARY KEY,
  content   TEXT,
  embedding VECTOR(1536)  -- OpenAI text-embedding-3-small dimension
);

-- Insert with embedding
INSERT INTO documents (content, embedding)
VALUES ('PostgreSQL is a powerful database', '[0.1, 0.2, ...]');
sql
-- Cosine similarity (best for normalized embeddings)
SELECT id, content,
       1 - (embedding <=> '[0.1, 0.2, ...]'::vector) AS similarity
FROM documents
ORDER BY embedding <=> '[0.1, 0.2, ...]'::vector
LIMIT 10;

-- Operators:
-- <=>  cosine distance
-- <->  L2 (Euclidean) distance
-- <#>  negative inner product

HNSW Index (pgvector 0.5+)

sql
-- HNSW: faster queries, higher memory, best for most use cases
CREATE INDEX idx_docs_embedding_hnsw ON documents
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);

-- IVFFlat: lower memory, good for large datasets
CREATE INDEX idx_docs_embedding_ivf ON documents
USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 100);

Python Integration

python
import openai, psycopg2
from pgvector.psycopg2 import register_vector

conn = psycopg2.connect('postgresql://localhost/mydb')
register_vector(conn)

# Generate embedding
resp = openai.embeddings.create(model='text-embedding-3-small', input='search query')
query_vec = resp.data[0].embedding

# Semantic search
cur = conn.cursor()
cur.execute(
    'SELECT id, content FROM documents ORDER BY embedding <=> %s LIMIT 5',
    (query_vec,)
)
results = cur.fetchall()

Hybrid Search: Vector + Full-Text

sql
-- Combine vector similarity with keyword filtering
SELECT id, content,
       embedding <=> $1 AS vec_dist
FROM documents
WHERE search_vector @@ to_tsquery('english', 'postgres & performance')
ORDER BY embedding <=> $1
LIMIT 10;

Key Takeaways

  • HNSW index is faster at query time; IVFFlat uses less memory — use HNSW for most cases
  • Index must be created after inserting data — empty-table indexes are inaccurate for IVFFlat
  • Use vector_cosine_ops for normalized embeddings (OpenAI, Cohere); vector_l2_ops for unnormalized
  • Hybrid search combining pgvector with pg_tsvector gives better results than either alone

JusDB Can Help

pgvector lets you add AI-powered search to your existing PostgreSQL database. JusDB can design your vector search schema and tune index parameters.

Share this article

JusDB Team

Official JusDB content team

Keep reading

PostgreSQL 19 Beta: Every New Feature That Matters to DBAs

PostgreSQL 19 Beta 1 (June 4, 2026) brings parallel autovacuum, the native REPACK command for online table rebuilds, 2x faster inserts under foreign-key load, online logical replication without a restart, WAIT FOR LSN for read-your-writes consistency, and default changes (JIT off, lz4 TOAST, RADIUS removed). A DBA-focused walkthrough of what changed and what to test before GA.

PostgreSQL14 minJun 15, 2026
Read

PostgreSQL Performance Tuning Playbook: A Top-Down Method for Faster Queries

A repeatable, top-down method for tuning PostgreSQL: measure with pg_stat_statements, read plans with EXPLAIN (ANALYZE, BUFFERS), fix queries and indexes before parameters, then tune memory, I/O, WAL, connection pooling, and autovacuum — with a ready-to-adapt postgresql.conf baseline.

PostgreSQL16 minMay 31, 2026
Read

PostgreSQL Architecture Deep Dive: Process Model, MVCC, WAL & Replication Explained

Walk through PostgreSQL's multi-process architecture, shared/local memory layout, page-organized storage, MVCC tuple versioning, the WAL write path, the query execution pipeline, and physical + logical replication — all with ASCII flow diagrams that show how data and control actually move through the system.

PostgreSQL18 minMay 31, 2026
Read