pgvector: Vectors in Postgres

Our recommended starting vector store: keep embeddings next to your relational data with the pgvector extension for Postgres.

Intermediate · about 5 hours · difficulty 3/5

Learn first: SQL and Postgres Basics, Similarity Metrics and ANN Indexes

pgvector adds a vector column type, distance operators and ANN indexes (HNSW and IVFFlat) to Postgres. For most applications it is the pragmatic choice: one database for users, documents, permissions and embeddings.

Why we recommend it first

  • One system to run, back up and secure; available on most managed Postgres services.
  • SQL joins and filters with the vector search: "nearest chunks, but only in documents this user can access, updated this year" is one query.
  • Transactions: document rows and their chunks stay consistent.
  • Scales comfortably to millions of vectors with proper indexes.

What to learn

  • Enabling the extension and defining embedding vector(N) columns with the right dimension.
  • Distance operators: <=> for cosine distance, <-> for L2, <#> for negative inner product.
  • Creating an HNSW index with the matching operator class, and tuning ef_search.
  • Combining with full-text search (tsvector) for hybrid retrieval later.
  • Using it from Python (psycopg plus pgvector helpers) or an ORM.

Limits

At very large scale (hundreds of millions of vectors) or with extreme query rates, a dedicated vector database may be simpler to operate. Index builds on big tables need memory and time.

Good enough to move on

You can store chunks and embeddings in Postgres, build an HNSW index, and run a filtered top-k similarity query from your application.

Key concepts

  • vector column type and dimensions
  • Distance operators
  • HNSW and IVFFlat indexes
  • Filtered similarity queries
  • Full-text search with tsvector
  • Managed Postgres options: Supabase, Neon, cloud RDS

Other options for this step

  • Dedicated Vector Databases: Pinecone, Qdrant, Weaviate, Milvus and Chroma are purpose-built alternatives when scale, features or a managed service outweigh keeping one database.

Mini project

Run Postgres with pgvector in Docker, load chunks and embeddings for a document set, create an HNSW index, and expose a search endpoint that filters by document type and returns the top 5 chunks with scores.

Check yourself

Why is permission filtering easier to get right when embeddings live in the same database as your access control data?

Free resources