Sub topic 3
Enabling pgvector in a database
This chapter assumes that you know basic SQL and PostgreSQL administration. You should be able to connect with psql or another SQL client and run commands such as CREATE TABLE and SELECT. You should also know what an embedding is: a list of numbers that represents the meaning or features of an item.
What pgvector provides
pgvector is a PostgreSQL extension. An extension adds database features that are not part of PostgreSQL's core installation. After you enable pgvector, PostgreSQL can store vectors and compare them with distance operators.
Installing pgvector and enabling pgvector are separate steps:
- Installing makes the extension files available to the PostgreSQL server.
- Enabling creates the extension objects in one database.
- Each database that uses vectors must enable the extension separately.
The server package must already be installed by your operating system, container image, or managed database provider. The following SQL command cannot install missing server files.
Enable the extension
Connect to the target database, then run:
CREATE EXTENSION IF NOT EXISTS vector;The IF NOT EXISTS clause makes the command safe to run more than once. PostgreSQL creates the vector type, operators, functions, and index support provided by pgvector.
You can verify that the extension is enabled with:
SELECT extname, extversion
FROM pg_extension
WHERE extname = 'vector';A successful result contains one row. The extversion column shows the installed pgvector version.
You can also list available extensions before enabling one:
SELECT name, default_version, installed_version
FROM pg_available_extensions
WHERE name = 'vector';If installed_version is empty, pgvector is available on the server but is not enabled in the current database.
Installation and permissions
You usually need a database owner or a suitably privileged administrator to create an extension. On a managed PostgreSQL service, the provider may enable pgvector for you or restrict which roles can run CREATE EXTENSION.
If PostgreSQL reports that it cannot open the extension control file, the server installation does not include pgvector. Install a package or choose a database image that includes it. If PostgreSQL reports a permission error, connect with the database owner or ask an administrator to enable the extension.
The extension belongs to the database where you run the command. Enabling it in one database does not enable it in another.
| Situation | What to do |
|---|---|
| pgvector is installed but not enabled | Run CREATE EXTENSION IF NOT EXISTS vector |
| The extension control file is missing | Install pgvector on the PostgreSQL server |
| Permission is denied | Use an authorized database role |
| The extension is enabled in another database | Connect to the correct database and enable it there |
How the setup fits together
Once the extension is enabled, later chapters can create columns with the vector type and run similarity queries. Enabling the extension alone does not create any tables or embeddings.
Exercise
You ran CREATE EXTENSION IF NOT EXISTS vector successfully in the analytics database. Why might a query using the vector type still fail in the reporting database?
Show answer
Extensions are enabled per database. Run the command in the reporting database too, assuming pgvector is installed on the server and your role has permission.