HerokuValerie WoolardTutorial
How to Use pgvector for Similarity Search on Heroku Postgres
Summary
Heroku Postgres now supports pgvector on all Standard-tier or higher databases running Postgres 15, adding vector storage and functions for similarity search and machine-learning applications. Older Postgres versions require an upgrade to Postgres 15. The workflow generates embeddings, adds a VECTOR(100) column, and stores values using SQL or Python with psycopg, pgvector-python, and Wikipedia2Vec’s pretrained 100-dimensional model. Queries can rank vectors with Euclidean, cosine, or inner-product distance; the example uses <-> to return five animals nearest to shark, producing crocodile, dolphin, whale, turtle, and alligator. Results depend on the embedding model and corpus, while indexes introduce approximate nearest-neighbor search; pgvector supports IVFFlat and HNSW, with IVFFlat requiring table data for better recall.
Context
Heroku Postgres added pgvector to support vector data associated with large language models and other machine-learning applications. Embeddings are often produced in vector format, enabling similarity searches alongside existing relational data. The feature is available on Standard-tier or higher databases running Postgres 15.
Approach / What changed
Enable the vector extension with CREATE EXTENSION vector, create a dimensioned vector column, and populate it with embeddings generated by Wikipedia2Vec or another embedding model. The example uses SQL or Python with psycopg and pgvector-python, then queries distance operators for similarity. It also describes IVFFlat and HNSW indexing considerations.
Takeaways
The example stores Wikipedia2Vec word embeddings in a VECTOR(100) column and uses Python with psycopg and pgvector-python to insert generated vectors.
pgvector provides Euclidean distance (<->), cosine distance (<=>), and inner product (<#>) functions; the appropriate choice depends on the data and use case.
Adding a vector index changes searches from exact to approximate nearest-neighbor search, which can affect results; IVFFlat and HNSW are the supported index types.
Source and freshness
- Source
- heroku.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.