๐Ÿ” How to Enable pgvector on Azure PostgreSQL Flexible Server

๐Ÿ” How to Enable pgvector on Azure PostgreSQL Flexible Server

As AI-driven applications grow in popularity, so does the need to store and query vector embeddings. PostgreSQL, a powerful open-source database, supports this with the pgvector extension. But what if you’re using Azure Database for PostgreSQL โ€“ Flexible Server?

In this post, weโ€™ll walk through how to enable the pgvector extension on Azure and start using it for vector similarity searches.


๐Ÿง  What is pgvector?

pgvector is a PostgreSQL extension that allows you to store vector data (VECTOR(n) types) and perform similarity searches using:

  • Euclidean distance (<->)
  • Cosine distance (<=>)
  • Inner product (<#>)

Itโ€™s a foundational tool for AI applications using text or image embeddings from models like OpenAI, Cohere, or Hugging Face.


โœ… Prerequisites

  • An existing Azure Database for PostgreSQL โ€“ Flexible Server (version 14 or higher)
  • Admin access to the server (not just database-level access)
  • Familiarity with Azure Portal or Azure CLI

โš™๏ธ Step 1: Add pgvector to the Allowed Extensions

By default, Azure restricts which extensions can be enabled. You’ll need to explicitly allow vector (the name used in PostgreSQL for pgvector).

Option A: Azure Portal

  1. Go to your PostgreSQL Flexible Server in the Azure Portal.
  2. Navigate to Settings โ†’ Server parameters.
  3. Search for the parameter: azure.extensions.
  4. Add vector to the list (comma-separated values).
  5. Click Save.

Option B: Azure CLI

If you prefer using the CLI:

bashCopyEditaz postgres flexible-server parameter set \
  --resource-group <your-resource-group> \
  --server-name <your-server-name> \
  --name azure.extensions \
  --value vector

โœ… Note: If azure.extensions already includes values, append vector with a comma.


๐Ÿงช Step 2: Create the pgvector Extension

Now that vector is allowed, connect to your database using a tool like psql or Azure Data Studio and run:

sqlCopyEditCREATE EXTENSION vector;

To verify:

sqlCopyEditSELECT * FROM pg_extension WHERE extname = 'vector';

๐Ÿงฑ Step 3: Create a Table with Vector Embeddings

Now you can create tables using the vector type.

sqlCopyEditCREATE TABLE documents (
id SERIAL PRIMARY KEY,
content TEXT,
embedding VECTOR(1536) -- Match the dimensionality of your model
);