Easily deploy a pgvector-enabled PostgreSQL server to Azure
The pgvector extension was first released in 2021 but has shot up in popularity in the last year, as developers discover what is possible with vector embeddings, vector similarity, and vector search. Once we start storing embedding vectors in database rows, we can make queries like "which movies are more similar to each other, based on their synopsis?" and "which retail item's descriptions most closely match this user's query?"
What is pgvector?
The pgvector extension allows PostgreSQL users to store columns of a vector type, create an index (HNSW or IVF) to efficiently index the vector fields, and query using vector distance operators (cosine, Euclidean, or inner product). Or, to put that in SQL form:
Note: While the extension is typically referred to as "pgvector", the actual extension name is "vector", so that is what's used in the CREATE EXTENSION statement above.
Using pgvector on Azure PaaS
Azure now has multiple PaaS offerings for PostgreSQL that support the pgvector extension: PostgreSQL Flexible Server and Azure Cosmos DB PostgreSQL server.
If you already have an existing Azure PostgreSQL server, you can enable the extension manually in the Portal, as shown below and described in the linked tutorials.
A PostgreSQL flexible server with pgvector extension enabled.
An easy template for pgvector deployment
To make it even easier to get started with pgvector on PostgreSQL Flexible Server, we've created a template project that contains infrastructure-as-code (Bicep files) and support for the Azure Developer CLI (azd). Clone or download the project here:
https://github.com/Azure-Samples/azure-postgres-pgvector-python
With a few commands, you'll have a pgvector-enabled PostgreSQL server provisioned in your Azure account. We've also added keyless authentication to the template, so you can authenticate with your Azure credential instead of a secret.
The template project includes multiple Python scripts showing you how to connect to your PostgreSQL server and use the pgvector extension in the most common SQL packages: psycopg2, asyncpg, SQLAlchemy, and SQLModel.
For example, here's a selection of the code from a SQLAlchemy example for storing movie titles and their embeddings:
See the full code in the repository.
There are so many ways that you can use the pgvector extension once you've gotten started with it, both as a tool in building generative AI applications (especially RAG apps), but also in any situation where similarity is a heuristic, like recommendations, fraud detection, and more. Start bringing vectors into your apps today and let us know what you build!
Published on:
Learn moreRelated posts
This Month in Azure Static Web Apps | 09/2024
We are back with another edition of the Azure Static Web Apps Community! :party_popper: September was yet another month ...
GitHub Copilot for Azure: 6 Must-Try Features
As developers, we are constantly seeking tools that streamline our workflows and boost productivity. … Enter GitHub Copilot for Azure, now in ...
Responsible AI Mitigation Layers
Generative AI is increasingly being used in various kinds of systems to augment humans and infuse intelligent behavior into existing and new a...
Streamline Your Azure Workflow: Introducing GitHub Copilot for Azure in VS Code
I'm excited to announce the public preview of GitHub Copilot for Azure - a new addition to your toolkit that seamlessly integrates with G...
Build Intelligent Apps Code-First with Prompty and Azure AI
Building Generative AI applications can feel daunting for traditional app developers. What does the end-to-end applicati...
Certificación AI-900 (Fundamentos de IA) con Chicas en IA
La inteligencia artificial ha llegado para quedarse, ¡y más aún con la revolucionaria IA generativa! Para ayudar a los profesionales a especia...
Get certified with Learn Live GitHub series!
GitHub Universe is coming, and Microsoft and GitHub are partnering to offer a new special Learn Live series in Brazilian Portuguese, English a...
Certifícate con Learn Live GitHub en Español
Microsoft y GitHub se han unido para ofrecer una nueva serie especial de Learn Live en inglés y español: GitHub 2024. Del 10 al 24 de Octubre,...
Evaluating generative AI: Best practices for developers
As a developer working with generative AI, you've likely marveled at the impressive outputs your models can produce. But how do you ensure the...
Introducing Azure Product Retirement Livestreams
The Azure Retirements team, in collaboration with key partner groups, is excited t...