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.
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.
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.
A couple of quick checks tell you the extension is live:
SELECT * FROM pg_extension WHERE extname = 'vector'; — returns one row if enabledSELECT extversion FROM pg_extension WHERE extname = 'vector'; — shows the installed versionVersion 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.
CREATE EXTENSION vector installs into a schema (public by default). It registers:
vector data type (and on recent versions halfvec / sparsevec)<-> (L2 / Euclidean), <=> (cosine), <#> (negative inner product)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.