Skip to main content

pgvector

pgvector is a Postgres extension that adds a vector column type and nearest-neighbour operators, so embeddings live next to the relational data they describe instead of in a separate system.

pip install -qU langchain-postgres
from langchain_postgres import PGVector
from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(model="text-embedding-3-small")

vector_store = PGVector(
embeddings=embeddings,
collection_name="my_docs",
connection="postgresql+psycopg://user:password@localhost:5432/mydb",
)

vector_store.add_documents(documents)

Why put vectors in Postgres

If your application already stores its relational data in Postgres, pgvector avoids running and operating a second database, keeps backups and transactions unified, and lets you join a vector search against ordinary SQL filters instead of maintaining metadata filtering logic in two places.

Index types

IndexBuild timeQuery speedRecallUse it when
None (exact)noneslow at scaleexactsmall dataset, correctness matters most
IVFFlatfastgoodapproximate, tunablemoderate dataset size, simpler tuning
HNSWslowerfastestapproximate, high recalllarger dataset, query latency matters
tip

Start with no index (exact search) while the collection is small — an approximate index is an optimization for scale, not a default you need on day one.

See also

  • Overview — the shared VectorStore interface.
  • Comparison — pgvector against the alternatives.