Sub topic 3
Enabling pgvector in a database
This chapter covers getting the vector type into a database. It assumes you can connect with psql, know basic SQL, and have a rough idea of what an embedding is. The later chapters rely on a working extension, so the chapter also shows how to check that it works.
Two separate steps: install, then create
PostgreSQL extensions need two steps, and mixing them up causes most setup errors.
- Install the files on the server. The extension's shared library and SQL scripts must be present on the machine that runs PostgreSQL. A package manager, a container image, or a managed-service provider does this.
- Create the extension in each database.
CREATE EXTENSIONruns the install scripts inside one database and adds thevectortype, the distance operators, and the index access methods to it.
Installing the files makes the extension available. Creating it makes it usable. The second step applies per database. Creating it in postgres does not enable it in mydb.
Installing the files
The method depends on where PostgreSQL runs.
| Environment | Typical method |
|---|---|
| Docker | Use the `pgvector/pgvector` image, which includes the extension |
| Debian or Ubuntu | Install the package that matches your server version, for example `postgresql-16-pgvector` |
| macOS with Homebrew | `brew install pgvector` |
| Managed services (Amazon RDS and Aurora, Google Cloud SQL, Azure, Supabase and others) | Nothing to install. Run `CREATE EXTENSION`, after checking that your service version offers it |
| Any Linux or macOS host | Build from source |
The fastest way to get a disposable test server is Docker. This starts PostgreSQL 16 with pgvector already installed:
docker run --name pgvector-demo \
-e POSTGRES_PASSWORD=postgres \
-p 5432:5432 \
-d pgvector/pgvector:pg16
docker exec -it pgvector-demo psql -U postgresTo build from source, you need the PostgreSQL development headers (on Debian, the postgresql-server-dev-16 package) and a C compiler. Make sure pg_config on your path belongs to the server you want to extend:
git clone --branch v0.8.0 https://github.com/pgvector/pgvector.git
cd pgvector
make
sudo make installmake install copies files into the directories reported by pg_config. If you run several PostgreSQL versions on one machine, a mismatched pg_config installs the extension for the wrong one. Check with pg_config --version before you build.
Checking that the server can see it
Before creating the extension, ask the server whether it found the files:
SELECT name, default_version, installed_version
FROM pg_available_extensions
WHERE name = 'vector';The extension is named vector, not pgvector. The project is called pgvector, but the name you pass to SQL is vector. If this query returns no rows, step 1 did not work for this server, and no SQL command will fix that.
Creating the extension
Connect to the database where your tables live, then run:
CREATE EXTENSION IF NOT EXISTS vector;IF NOT EXISTS makes the statement safe to repeat, which matters in migration scripts and setup code that may run more than once. Your role needs permission to create extensions. On a self-managed server that usually means a superuser. On managed services, the main admin role is typically allowed. If you get a permission error, ask whoever administers the server rather than working around it.
By default the extension's objects go into the first schema on your search_path, usually public. You can choose a schema:
CREATE SCHEMA IF NOT EXISTS extensions;
CREATE EXTENSION IF NOT EXISTS vector SCHEMA extensions;If you do this, every role that uses vectors must have extensions on its search_path, or you must write extensions.vector(3) and OPERATOR(extensions.<->) explicitly. Unless your platform requires a separate schema, the default is simpler.
Verifying the installation
Check three things: the extension is registered, its version, and that the type and operators work.
\dx vector
SELECT extname, extversion
FROM pg_extension
WHERE extname = 'vector';
SELECT '[1,2,3]'::vector <-> '[4,5,6]'::vector AS l2_distance;The last query measures the Euclidean (L2) distance between two three-dimensional vectors. The result is the square root of 27, about 5.196. If you see that number, the type, the text input format, and the <-> operator all work.
Note the version. Features arrived in different releases, and the later chapters use some of them:
| Feature | First version |
|---|---|
| `ivfflat` index | 0.1.0 |
| `hnsw` index | 0.5.0 |
| `halfvec`, `sparsevec`, and the `<+>` L1 operator | 0.7.0 |
If your version is older than a chapter needs, upgrade before continuing.
Upgrading
Upgrading has the same two steps. Updating the package or image replaces the files on the server. The database keeps running the old SQL objects until you tell it to switch:
ALTER EXTENSION vector UPDATE;Run this in every database that uses the extension. Until you do, pg_extension.extversion shows the old version, even though pg_available_extensions.default_version shows the new one. Comparing those two columns is a quick way to find databases that need an update.
Common errors
| Error message | Cause | Fix |
|---|---|---|
| `could not open extension control file ".../vector.control": No such file or directory` | The files are not installed for this server, or were installed for a different PostgreSQL version | Install the files, or check that `pg_config` matches the running server |
| `extension "vector" is not available` | Same cause, reported by the server's catalog | Confirm with `pg_available_extensions` |
| `permission denied to create extension "vector"` | Your role lacks the needed privilege | Use an administrative role |
| `type "vector" does not exist` | The extension exists in another database or schema | Run `CREATE EXTENSION` in the current database, or fix `search_path` |
| `operator does not exist: vector <-> unknown` or similar | The extension is in a schema that is not on your `search_path` | Add the schema to `search_path` or qualify the operator |
The fourth error is the most common after a successful install. Connecting to the wrong database with psql is easy to do, so check SELECT current_database(); first.
Creating the extension in new databases automatically
If you create many databases and want vectors in each one, install the extension in template1. PostgreSQL copies template1 into every new database:
\c template1
CREATE EXTENSION IF NOT EXISTS vector;This only affects databases created afterward. Existing databases need their own CREATE EXTENSION.
Putting it to use
You now have a database with the vector type. The next chapter builds on it by declaring a column with a fixed number of dimensions and storing real rows. Keep the Docker container from earlier or an equivalent setup, since you will run the examples against it.
Exercise
You installed the pgvector package on your server and created the extension in the database app_dev. A colleague connects to the database app_test on the same server and runs CREATE TABLE t (v vector(3));, which fails with type "vector" does not exist. What is wrong, and what single statement fixes it?
Show answer
The files are installed for the whole server, but the extension must be created in each database. app_test does not have it yet. Connect to app_test and run CREATE EXTENSION IF NOT EXISTS vector;. Then verify with SELECT extversion FROM pg_extension WHERE extname = 'vector';.
Exercise
After upgrading the pgvector package, pg_available_extensions shows default_version 0.8.0 for vector, but pg_extension in your database still shows extversion 0.5.1, and halfvec is missing. What do you run?
Show answer
Run ALTER EXTENSION vector UPDATE; in that database. The package upgrade replaced the files, but the database keeps its old objects until the update script runs.