name | description | languages | products | page_type | urlFragment | |||||
---|---|---|---|---|---|---|---|---|---|---|
PostgreSQL + pgvector |
Deploy a PostgreSQL Flexible Server to Azure with the pgvector extension enabled. |
|
|
sample |
azure-postgres-pgvector-python |
This repository makes it easy to deploy a PostgreSQL Flexible Server to Azure with the pgvector extension installed. The pgvector extension provides a vector similarity search engine for PostgreSQL, allowing you to perform similarity searches on your data using vector embeddings.
The repository contains infrastructure-as-code (Bicep) to deploy an Azure PostgreSQL Flexible Server with pgvector extension enabled, password authentication disabled, and Entra (Active Directory) authentication enabled. The repository also contains example Python scripts to demonstrate how to use pgvector.
Table of contents:
You have a few options for setting up this project. The easiest way to get started is GitHub Codespaces, since it will setup all the tools for you, but you can also set it up locally if desired.
You can run this repo virtually by using GitHub Codespaces, which will open a web-based VS Code in your browser:
Once the codespace opens (this may take several minutes), open a terminal window.
A related option is VS Code Dev Containers, which will open the project in your local VS Code using the Dev Containers extension:
- Start Docker Desktop (install it if not already installed).
- Open the project: .
- In the VS Code window that opens, once the project files show up (this may take several minutes), open a terminal window.
-
Install the required tools:
- Azure Developer CLI
- Python 3.9, 3.10, or 3.11 (Only necessary if you want to run the Python scripts)
-
Create a new folder and switch to it in the terminal.
-
Run this command to download the project code:
azd init -t azure-postgres-pgvector-python
Note that this command will initialize a git repository, so you do not need to clone this repository.
-
Create a Python virtual environment and install the required packages:
python -m pip install -r requirements.txt
-
Open a terminal window inside the project folder.
Follow these steps to deploy a PostgreSQL Flexible Server to Azure with the pgvector extension enabled:
-
Login to your Azure account:
azd auth login
-
Create a new azd environment:
azd env new
Enter a name that will be used for the resource group. This will create a new folder in the
.azure
folder, and set it as the active environment for any calls toazd
going forward. -
Run this command to provision all the resources:
azd provision
This will create a new resource group, and create the PostgreSQL Flexible server inside that group.
-
The example Python scripts look for configuration variables from a
.env
file located in the directory from where you invoke the scripts. You can easily create a file with the correct variables for your PostgreSQL server by running this script that copies the necessaryazd
environment variables into your local.env
:./write_azure_env.sh
-
Now you may run the Python scripts in order to interact with the PostgreSQL server.
python examples/sqlalchemy_async.py
Note that each of the script starts off with a
CREATE EXTENSION vector;
command, which will install the pgvector extension into the database. Once you run that once in a given database, you do not need to run it again for that particular database.
The examples
folder contains example Python scripts that demonstrate how to use pgvector, based on the pgvector sample code.
Script | Dependencies | Description |
---|---|---|
sqlalchemy_async.py |
asyncpg , sqlalchemy , pgvector |
Uses pgvector with SQLAlchemy and asyncpg for a simple 3-dimension vector. |
sqlalchemy_items.py |
psycopg2 , sqlalchemy , pgvector |
Uses pgvector with SQLAlchemy and psycopg2 for a simple 3-dimension vector. |
sqlalchemy_movies.py |
psycopg2 , sqlalchemy , pgvector |
Uses pgvector with SQLAlchemy and psycopg2 for 1536-dimension vectors calculated previously with OpenAI's text-embedding-ada-002 model. |
sqlmodel_items.py |
sqlmodel , pgvector |
Uses pgvector with SQLModel for a simple 3-dimension vector. |
asyncpg_items.py |
asyncpg , pgvector |
Uses pgvector with asyncpg for a simple 3-dimension vector. |
psycopg_items.py |
psycopg2 , pgvector |
Uses pgvector with psycopg2 for a simple 3-dimension vector. |
Let us know if there are any other examples you would like to see!