Skip to main content

langchain-azure-postgresql

langchain-azure-postgresql is a Python package that implements both asynchronous and synchronous VectorStore support for Azure Database for PostgreSQL. Specifically, this package adds support for

  1. Microsoft Entra ID (formerly Azure AD) authentication when connecting to your Azure Database for PostgreSQL instances, and,
  2. DiskANN indexing algorithm when indexing your (semantic) vectors.

This way, you can leverage your Azure Database for PostgreSQL instances as secure and fast vector stores for your LangChain workflows.

Installation

To install langchain-azure-postgresql, you need to install the necessary Python packages:

$ python3 -m pip install langchain langchain-azure-postgresql langchain-openai
# logs stripped for brevity

Usage

Once the packages are installed, you can use Azure Database for PostgreSQL instances as vector stores:

import os

from langchain_core.documents import Document
from langchain_openai import OpenAIEmbeddings

from langchain_azure_postgresql.common import AzurePGConnectionPool, ConnectionInfo
from langchain_azure_postgresql.langchain import AzurePGVectorStore

documents = [
    Document(
        page_content="Dogs are great companions, known for their loyalty and friendliness.",
        metadata={"source": "mammal-pets-doc"},
    ),
    Document(
        page_content="Cats are independent pets that often enjoy their own space.",
        metadata={"source": "mammal-pets-doc"},
    ),
]

host = os.getenv("PGHOST", "localhost")
connection_pool = AzurePGConnectionPool(azure_conn_info=ConnectionInfo(host=host))

embedding = OpenAIEmbeddings(model="text-embedding-3-small")

vector_store = AzurePGVectorStore(connection_pool=connection_pool, embedding=embedding)

vector_store.add_documents(documents)

The code snippet above will try to connect to your PostgreSQL host, as defined by the environment variable PGHOST, and fall back to connecting to localhost if the environment variable is not defined. By default, ConnectionInfo objects try to use Microsoft Entra ID to login to the PostgreSQL instances/hosts.

Please see the documentation for more details on configuring various classes provided by langchain-azure-postgresql.

Development

The development environment for this package is managed by uv, an up-and-coming and versatile Python package and project manager.

To create the development environment, you first need to install uv in your development environment (unless you are using the development container setup as provided by this repository). Once uv is properly installed and set up, you can run the following commands to synchronize and activate the development environment:

$ uv sync --all-extras # synchronize the development environment from uv.lock
# logs stripped for brevity
$ source .venv/bin/activate # or, as appropriate for your shell, e.g., fish

Once the development environment is synchronized and activated, you can start contributing changes to the package. For test automation, the package leverages tox, a test automation and standardization framework in Python.

There are some pre-defined test environments and labels managed by tox:

$ tox list
default environments:
lint    -> Run lint checks on the code base
package -> Run packaging checks on the code base
type    -> Run type checks on the code base
3.11    -> Run tests under Python 3.11
3.12    -> Run tests under Python 3.12
3.13    -> Run tests under Python 3.13
3.14    -> Run tests under Python 3.14

The default environments are for running lint checks, packaging checks, type checks, and end-to-end tests for different Python versions, respectively. You can selectively run an environment via, e.g., tox run -e lint, or, otherwise, run the full suite of tests via tox. There is a special label called test, which will run only the end-to-end tests for all the supported Python versions and can be run via the following:

$ PGHOST=<host>.postgres.database.azure.com PGPASSWORD='your_password' tox run -m test
# logs stripped for brevity

For more information on the supported test flags and environment variables, please check the output of pytest --help.

Changelog

  • 1.0.2:

    • We raised the minimum supported Python version from 3.10 to 3.11 in accordance with the repository's Python support policy. Users running Python 3.10 must upgrade their runtime to install this release. #1021
    • We added Python 3.14 to the supported runtime and test matrix. #1021

Release files for langchain-azure-postgresql 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-azure-postgresql 1.0.2
File Size Uploaded
langchain_azure_postgresql-1.0.2.tar.gz 36.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-azure-postgresql 1.0.2
File Interpreter ABI Platform
langchain_azure_postgresql-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 78.6 kB

Release files / langchain_azure_postgresql-1.0.2.tar.gz

Download URL langchain_azure_postgresql-1.0.2.tar.gz
Size 36.9 kB
Tags Source
SHA-256 checksum
How to use checksums
5e2233938fca1cbd979af49f2f155e3955151b74d2a78707ef7820d84709eb13
BLAKE2b-256 checksum
How to use checksums
3bc08dfc74b9d7e68c73c8ec575ac089f311de084af6cd7f61b809e26325f183
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / langchain_azure_postgresql-1.0.2-py3-none-any.whl

Download URL langchain_azure_postgresql-1.0.2-py3-none-any.whl
Size 41.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ac2e735b18547d5368154f71ad5d3abbee890be0748c1ceff17ea354f4c14f52
BLAKE2b-256 checksum
How to use checksums
47d733362fd778f55ce9bdac94faf280dc920e73900aa8308e768a8c76ae25b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page