Installing pgvector & Your First Embeddings

How to enable the pgvector extension, declare a correctly-sized vector(n) column, match the dimension to your embedding model, generate embeddings from an API and store them with proper upserts — plus a preview of the halfvec and sparsevec types for scale.

Day 2 Progress0%

Installing the pgvector Extension

pgvector is an extension for PostgreSQL — it isn't part of the core database, but once installed it turns ordinary Postgres into a working vector store. You enable it once per database with a single SQL statement.

CREATE EXTENSION vector

After the extension's files are present on the server, you turn it on inside a specific database:

CREATE EXTENSION IF NOT EXISTS vector;

The IF NOT EXISTS guard makes the statement safe to run repeatedly — in migrations, in seed scripts, in CI — without erroring if the extension is already enabled. Note the extension is named vector, not "pgvector"; the project is pgvector but the SQL object is vector.

This is a per-database operation. If you have separate databases for staging and production, you run it in each. It also requires sufficient privileges — typically a superuser or a role granted CREATE on the database. On managed Postgres (Supabase, Neon, RDS, Cloud SQL) pgvector is pre-shipped; you just run CREATE EXTENSION and it's available. On a self-managed box you first install the OS package or build from source so Postgres can find the extension files.

Confirming It Worked

A couple of quick checks tell you the extension is live:

  • SELECT * FROM pg_extension WHERE extname = 'vector'; — returns one row if enabled
  • SELECT extversion FROM pg_extension WHERE extname = 'vector'; — shows the installed version

Version matters: newer pgvector releases add types (halfvec, sparsevec, bit) and index types (HNSW landed in 0.5.0). If a feature in this lesson errors, check your version first.

Where the Extension Lives

CREATE EXTENSION vector installs into a schema (public by default). It registers:

  • the vector data type (and on recent versions halfvec / sparsevec)
  • distance operators: <-> (L2 / Euclidean), <=> (cosine), <#> (negative inner product)
  • distance functions and the index access methods (ivfflat, hnsw)

You don't import anything in your application — once the extension is enabled, these types and operators are just part of SQL in that database.

Key Takeaways
  • Enable pgvector once per database with CREATE EXTENSION IF NOT EXISTS vector — the SQL object is named 'vector', not 'pgvector'
  • Managed Postgres (Supabase, Neon, RDS, Cloud SQL) ships pgvector; self-managed servers need the OS package or a source build first
  • Verify with SELECT extversion FROM pg_extension WHERE extname='vector' — feature availability (HNSW, halfvec) depends on the version

AI Learning Assistant

Powered by advanced LLM

Get personalized help with concepts, code examples, and explanations tailored to your learning pace.

Course Stats

Estimated Time
50 min
Lessons
6 sections