Skip to main content
Azure Database for PostgreSQL - Flexible Server is a relational database service based on the open-source Postgres database engine. It’s a fully managed database-as-a-service that can handle mission-critical workloads with predictable performance, security, high availability, and dynamic scalability. This guide shows you how to leverage this integrated vector database to store documents in collections, create indices and perform vector search queries using approximate nearest neighbor algorithms such as Cosine Distance, L2 (Euclidean distance), and IP (inner product) to locate documents close to the query vectors.

Vector support

Azure Database for PostgreSQL - Flexible Server enables you to efficiently store and query millions of vector embeddings in PostgreSQL. As well as scale your AI use cases from POC to production:
  • Provides a familiar SQL interface for querying vector embeddings and relational data.
  • Boosts pgvector with a faster and more precise similarity search across 100M+ vectors using DiskANN indexing algorithm.
  • Simplifies operations by integrating relational metadata, vector embeddings, and time-series data into a single database.
  • Leverages the power of the robust PostgreSQL ecosystem and Azure Cloud for enterprise-grade features including replication, and high availability.

Authentication

Azure Database for PostgreSQL - Flexible Server supports password-based as well as Microsoft Entra (formerly Azure Active Directory) authentication. Entra authentication allows you to use Entra identity to authenticate to your PostgreSQL server. This eliminates the need to manage separate usernames and passwords for your database users, and allows you to leverage the same security mechanisms that you use for other Azure services. This guide is set up to use either authentication method. You can configure whether or not to use Entra authentication later in the notebook.

Setup

Azure Database for PostgreSQL is based on open-source Postgres. This integration uses the dedicated langchain-azure-postgresql package, which provides optimized support including DiskANN indexing and Microsoft Entra authentication. First download the partner packages:

Enable pgvector

See enablement instructions for Azure Database for PostgreSQL.

Set up credentials

You will need your Azure Database for PostgreSQL connection details and add them as environment variables to run this notebook. Set the USE_ENTRA_AUTH flag to True if you want to use Microsoft Entra authentication. If using Entra authentication, you will only need to supply the host and database name. If using password authentication, you’ll also need to set the username and password.

Setup AzureOpenAIEmbeddings

Initialization

Use Microsoft entra authentication

The following sections demonstrate how to set up LangChain to use Microsoft Entra authentication. The class AzurePGConnectionPool in the LangChain Azure Postgres package retrieves tokens for the Azure Database for PostgreSQL service by using DefaultAzureCredential from the azure.identity library. The connection can be passed into the connection parameter of the AzurePGVectorStore LangChain vector store.

Sign in to Azure

To log into Azure, ensure you have the Azure CLI installed. You will need to run the following command in your terminal:
Once you have logged in, the below code will be able to fetch the token.

Password authentication

If you’re not using Microsoft Entra authentication, the BasicAuth class allows the use of username and password:

Creating the vector store

Configuring vector store parameters

You can override the default parameters for metadata type, embedding dimension, index type, and more when initializing AzurePGVectorStore. This allows you to tailor the vector store to your specific use case and data. Key configuration options:
  • metadata_column_type: The type of the metadata column (default: 'jsonb'). Set to 'jsonb', 'text', etc.
  • embedding_column_type: The type of the embedding column (default: 'vector').
  • embedding_dimension: The dimension of your embedding vectors (default: 1536).
  • embedding_index_type: The index type for vector search (default: 'DiskANN'). Other options may include 'ivfflat', 'hnsw', etc.
  • embedding_index_opclass: The operator class for the index (default: 'vector_cosine_ops').
Example:
DiskANN is a scalable approximate nearest neighbor search algorithm for efficient vector search at any scale. It offers high recall, high queries per second, and low query latency, even for billion-point datasets. Those characteristics make it a powerful tool for handling large volumes of data.

Manage vector store

Add items

Note that adding documents by ID will over-write any existing documents that match that ID.

Update items

Retrieve items

Delete items

Query vector store

After you create your vector store and add the relevant documents, you can query the vector store in your chain or agent.

Filtering

The vector store supports a set of filters that can be applied against the metadata fields of the documents via the FilterCondition, OrFilter, and AndFilter in the LangChain Azure PostgreSQL package:

Query directly

Performing a simple similarity search can be done as follows:
If you want to use logical AND filters, here is an example:
If you want to execute a similarity search and receive the corresponding scores you can run:

Query by turning into retriever

You can also transform the vector store into a retriever for easier usage in your chains.
If you want to use max marginal relevance search on your vector store:
For a full list of the different searches you can execute on a AzurePGVectorStore vector store, please refer to the documentation.

Usage for retrieval-augmented generation

For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:

API reference

For detailed documentation of all AzurePGVectorStore features and configurations head to the API reference: https://github.com/langchain-ai/langchain-azure/tree/main/libs/azure-postgresql/src/langchain_azure_postgresql/langchain