pgvector guide: speed, installing on Postgres and Alpine, index storage, errors, tutorial

What pgvector is, whether it is fast, installing and enabling it, Alpine images, where indexes live, fixing operator errors, Lantern and tutorials.

Presenting to a room
Presenting to a room

Key takeaways

  • pgvector adds vector types, distance operators and HNSW or IVFFlat indexes to PostgreSQL.
  • With in-memory HNSW indexes, pgvector serves millions of vectors in milliseconds.
  • It is a separate extension: install the package, then run CREATE EXTENSION vector per database.
  • Its indexes are normal Postgres indexes, stored, logged and replicated like any other.
  • Operator errors usually mean a missing extension, a schema outside search_path or a missing ::vector cast.

pgvector is a PostgreSQL extension that adds vector columns, distance operators and HNSW and IVFFlat indexes, letting you run similarity search for RAG and recommendations inside Postgres. With HNSW indexes it is fast enough for most applications with millions of vectors. I build RAG systems with Python, FastAPI and PostgreSQL, so this page covers what pgvector is, its speed, installing and enabling it, Alpine images, where indexes are stored, fixing operator errors, comparisons with Lantern, and a tutorial.

What is the pgvector extension?

pgvector is an open-source extension for PostgreSQL that adds a vector data type, distance operators for L2, inner product, cosine and more, and approximate nearest neighbour indexes, so Postgres can store embeddings and run similarity search.

Also asked as: what is pgvector extension

Newer versions add half-precision vectors to save memory, binary vectors for compact storage and Hamming distance, sparse vectors for keyword-like embeddings, and iterative index scans for better filtered queries. Because it is an extension, everything works with standard SQL: joins, WHERE filters, transactions, backups and replication. It is maintained on GitHub under the PostgreSQL licence and available on most managed Postgres services, which makes it a popular default vector store for RAG applications.

Is pgvector fast?

Yes, for most applications. With an HNSW index that fits in memory, pgvector typically answers similarity queries over millions of vectors in milliseconds with high recall. Exact search without an index is slower, scanning every row.

Performance depends on index type, parameters, dimensions, memory and filters. HNSW offers better speed and recall than IVFFlat but builds slower and uses more memory. Raise hnsw.ef_search for recall, use half-precision vectors to cut memory, and give the server enough RAM and maintenance_work_mem for builds. Heavily filtered queries were a weak spot, improved by iterative index scans in version 0.8. Benchmark with your real data and queries before deciding you need a dedicated vector database.

Does pgvector come with Postgres?

No. pgvector is not part of core PostgreSQL; it is a separate extension you install. Many managed services, such as Amazon RDS and Aurora, Supabase, Neon, Azure Database for PostgreSQL and Google Cloud SQL, include it ready to enable.

Also asked as: benchmarking postgres vector search approaches: pgvector vs. lantern

On those services, enabling it is a single SQL command, since the binaries are already installed. For self-managed servers, you install the package for your PostgreSQL version first. Some Docker images, such as pgvector/pgvector, bundle it on top of the official Postgres image. Check your PostgreSQL version compatibility, since pgvector supports recent major versions, and check which pgvector version your managed provider offers, since features like halfvec and iterative scans need newer releases.

How do you install pgvector in Postgres?

Use a package where possible: on Debian or Ubuntu with the PostgreSQL apt repository, install postgresql-16-pgvector, matching your Postgres version; on macOS, run brew install pgvector; with Docker, use the pgvector/pgvector image.

Also asked as: how to install pgvector in postgres

To build from source, install the PostgreSQL development headers and a compiler, clone the pgvector repository, then run make and make install, which uses pg_config to place files in the right directories. On Windows, build with the Visual Studio tools following the repository's instructions. After installation, a restart is not usually required; you enable the extension per database with CREATE EXTENSION. Verify with SELECT extversion FROM pg_extension WHERE extname = 'vector'.

How do you add the pgvector extension to a PostgreSQL database?

Connect to the database with sufficient privileges and run CREATE EXTENSION vector; the extension name is vector, not pgvector. Each database needs it enabled separately, and the binaries must already be installed on the server.

Also asked as: how to add pgvector extension to postgresql

Then create a table with a vector column, such as embedding vector(768), insert embeddings, and add an index with CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops). On managed services, you may need an admin role or must enable it through the dashboard, as on Supabase, which places extensions in an extensions schema. To upgrade later, install the new binaries and run ALTER EXTENSION vector UPDATE. Record the version in your migrations so environments stay consistent.

Can you add pgvector to a postgres Alpine image?

Yes. In a Dockerfile based on postgres:15-alpine, install build tools, clone pgvector at a release tag, run make and make install, then remove the build tools to keep the image small. Then enable the extension with CREATE EXTENSION vector.

Also asked as: is it possible to add pgvector extension on top of postgres:15.3-alpine images

A typical Alpine build installs git, build-base, clang and llvm, since PostgreSQL on Alpine may expect LLVM for JIT bitcode, then cleans them up in the same layer. Pin the pgvector version for reproducibility. Alternatively, switch to the official pgvector/pgvector images, which are Debian-based and maintained with each release, avoiding custom builds. Test that the extension loads by connecting and running CREATE EXTENSION in a fresh container.

Where are pgvector HNSW and IVFFlat indexes stored?

They are ordinary PostgreSQL indexes, stored as relation files in the database's data directory alongside tables, written to the write-ahead log, cached in shared buffers, and replicated and backed up like any other index.

Also asked as: where are hnsw and ivfflat indexes for pgvector extension of postgres created and stored

During index builds, pgvector uses maintenance_work_mem; if the graph does not fit, HNSW builds slow down significantly, so raise this setting for large builds. Queries are fastest when the index fits in memory. You can check index size with pg_relation_size or pg_indexes_size and list indexes through pg_indexes. Because they are standard indexes, they work with transactions, survive crashes and appear in query plans, which you can inspect with EXPLAIN ANALYZE.

How do you fix the operator does not exist error with pgvector?

This error usually means PostgreSQL cannot find a vector operator such as <=> for your types. Common causes: the extension is not enabled in this database, it lives in a schema not on your search_path, or one side is not a vector.

Also asked as: how to fix postgres error "operator does not exist" using pgvector

Fixes: run CREATE EXTENSION vector in the database; if it is installed in another schema, such as extensions on Supabase, add that schema to search_path or qualify the operator; cast query parameters explicitly with ::vector, since a text or float array parameter will not match; and ensure your column type is vector, not real[]. ORMs and drivers sometimes send parameters as text, so use pgvector's client libraries for Python or Node.js, which register the type correctly.

pgvector vs Lantern: which is faster?

Lantern is a PostgreSQL extension that implemented HNSW using the usearch library and claimed faster index builds than early pgvector versions. Since then, pgvector has added parallel HNSW builds and other optimisations, narrowing the differences.

Benchmarks published by vendors often favour their own product and depend heavily on dataset, dimensions, parameters and hardware, so treat them as hints. Consider ecosystem factors: pgvector is far more widely supported by managed providers, ORMs and frameworks, which matters more for most teams than small speed differences. If you evaluate alternatives, including pgvectorscale or VectorChord, run your own benchmark measuring recall, latency, build time and memory on your real workload.

What is a good Postgres pgvector tutorial?

Run Postgres with pgvector using the pgvector Docker image, enable the extension, create a table with text and an embedding column, insert embeddings from a model, then query nearest neighbours ordered by cosine distance with a limit.

Next steps: add an HNSW index and compare query times with and without it; add metadata columns and filter with WHERE; combine with PostgreSQL full-text search for hybrid retrieval; and connect from Python with psycopg and the pgvector package, or through LangChain's PGVector integration. Finish by building a small RAG API with FastAPI that embeds questions, retrieves top chunks and sends them to a model, measuring retrieval quality on test questions.

Sources

  1. pgvector, repositorygithub.com
  2. PostgreSQL, CREATE EXTENSIONpostgresql.org
  3. Docker Hub, pgvector/pgvector imagehub.docker.com
  4. Supabase, pgvector extensionsupabase.com
  5. pgvector-python, repositorygithub.com
  6. Lantern, repositorygithub.com
Pranjul Rathour
Pranjul Rathour
GenAI Engineer · India

GenAI engineer and AI product builder with 2+ years shipping production-grade AI systems: RAG pipelines, fine-tuned LLMs, hybrid retrieval and multi-modal apps across vision, speech and OCR, architected end to end from ingestion to deployment. Leads engineering for SCULT INDIA's 14-member team, founded the 500+ member TechVerse Enclave community and has mentored 200+ students. Three hackathon first prizes: Changethon 2025 (IIT Roorkee), Product Genesis at Vividhotsava 2025 (CSJMU) and BYTEBATTLE (MeetKats).

Open to GenAI roles, hackathon judging, mentorship sessions and guest talks at colleges. For campus sessions the inviting institution covers the visit end to end: travel (a First AC train ticket for journeys up to five hours, a flight for anything longer), accommodation, meals, and pickup and drop; the honorarium and workshop fees are negotiable on WhatsApp or on the call, and requests are welcome from institutions that can cover the visit this way. Email pranjulrathour41@gmail.com.

Keep reading

More on RAG & retrieval

All RAG & retrieval guides →
Campus talks · hackathon judging · mentoring

Want this as a live session at your college?

Open to GenAI roles, hackathon judging, mentorship sessions and guest talks at colleges. For campus sessions the inviting institution covers the visit end to end: travel (a First AC train ticket for journeys up to five hours, a flight for anything longer), accommodation, meals, and pickup and drop; the honorarium and workshop fees are negotiable on WhatsApp or on the call, and requests are welcome from institutions that can cover the visit this way.

Pranjul Rathour, GenAI engineer in India