---
title: "How to Use pgvector for Similarity Search on Heroku Postgres"
description: "Heroku Postgres now supports the pgvector extension on Standard-tier and higher databases running Postgres 15, enabled with `CREATE EXTENSION vector;`."
url: "https://swe.quest/articles/how-to-use-pgvector-for-similarity-search-on-heroku-postgres-c4879269328732cd"
canonical: "https://www.heroku.com/blog/pgvector-for-similarity-search-on-heroku-postgres/"
source: "Heroku Engineering"
published: "2023-11-16"
difficulty: "medium"
kind: "How-to"
reading_minutes: 5
tags: ["vector-search","postgres","python"]
---

# How to Use pgvector for Similarity Search on Heroku Postgres

A post on [Heroku Engineering](https://swe.quest/sources/heroku.md), published 2023-11-16, 5 min read, How-to. Original: [https://www.heroku.com/blog/pgvector-for-similarity-search-on-heroku-postgres/](https://www.heroku.com/blog/pgvector-for-similarity-search-on-heroku-postgres/)

## Summary

Heroku Postgres now supports the pgvector extension on Standard-tier and higher databases running Postgres 15, enabled with \`CREATE EXTENSION vector;\`. A worked example loads 100-dimensional Wikipedia2Vec word embeddings for a list of animals into a VECTOR(100) column using Python, psycopg and pgvector-python. It then runs nearest-neighbour queries with the Euclidean (\`\<-\>\`), cosine (\`\<=\>\`) and inner product (\`\<#\>\`) operators. On indexing, IVFFlat and HNSW indexes switch queries to approximate search, should match the distance function you query with most, and IVFFlat should be built after data is loaded for better recall.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Vector search](https://swe.quest/tags/vector-search.md) (Data & databases)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- PostgreSQL basics ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- vector embeddings ([easy posts on RAG & retrieval](https://swe.quest/tags/rag.md?difficulty=easy))

## Keywords

pgvector, Heroku Postgres, Wikipedia2Vec, psycopg, HNSW, IVFFlat, Word2vec

## Related posts

- [20x faster than pgvector: introducing pg\_embedding extension for vector search in Postgres and LangChain](https://swe.quest/articles/20x-faster-than-pgvector-introducing-pg-embedding-extension-for-vector-search-058737fc9388f495.md): Neon · 2023-07-11 · Medium · 7 min read · Vector search, PostgreSQL, Performance engineering
  pg\_embedding brought HNSW graph-based approximate nearest neighbor indexing to Postgres.
- [pgvector v0.5.0: Faster semantic search with HNSW indexes](https://swe.quest/articles/pgvector-v0-5-0-faster-semantic-search-with-hnsw-indexes-d1e5c38ac44f551e.md): Supabase · 2023-09-06 · Medium · 6 min read · Vector search, PostgreSQL, Performance engineering, Algorithms
  pgvector v0.5.0 adds HNSW indexes, which combine skip-list-style layering with navigable small world proximity graphs to do approximate nearest-neighbor search over embeddings.
- [Understanding vector search and HNSW index with pgvector](https://swe.quest/articles/understanding-vector-search-and-hnsw-index-with-pgvector-db3498f2e1e5486e.md): Neon · 2023-09-20 · Medium · 10 min read · Vector search, PostgreSQL, LLMs
  pgvector 0.5.0 adds an HNSW (Hierarchical Navigable Small Worlds) index for approximate nearest neighbor search in Postgres, replacing O(N) sequential exact scans with a multi-layer graph traversal that trades a little accuracy for much faster queries.
- [On-disk HNSW index for Postgres with pg\_embedding](https://swe.quest/articles/on-disk-hnsw-index-for-postgres-with-pg-embedding-aadc4bd81ebf65c4.md): Neon · 2023-08-03 · Medium · 5 min read · Vector search, PostgreSQL
  pg\_embedding's new version builds its HNSW vector index on disk rather than in memory and adds cosine, Manhattan and Euclidean (L2) distance support.
- [pgvector 0.4.0 performance](https://swe.quest/articles/pgvector-0-4-0-performance-559fc540f926a562.md): Supabase · 2023-07-13 · Medium · 8 min read · Vector search, PostgreSQL, Performance engineering, Inference & serving
  With tuned settings, pgvector 0.4.0's IVFFlat index handles 1M OpenAI embeddings (1536 dimensions, dbpedia dataset) at about 1800 QPS with 0.91 accuracy@10 on a 64-core, 256 GB instance, or about 670 QPS at 0.98 accuracy.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
