'Connect Django with Azure PostgreSQL using managed identity in azure

How can I configure Azure PostgreSQL database in Django settings.py using Managed Identity in Azure?



Solution 1:[1]

How to configure Azure PostgreSQL database in Django settings.py using Managed Identity In Azure?

As per the documentation:

1. Create a PostgreSQL user for Managed Identity:

SET aad_validate_oids_in_tenant = off;
CREATE ROLE myuser WITH LOGIN PASSWORD 'CLIENT_ID' IN ROLE azure_ad_user;

2. Settings.py:

DATABASES={
   'default':{
      'ENGINE':'django.db.backends.postgresql_psycopg2',
      'NAME':os.getenv('DATABASE_NAME'),
      'USER':os.getenv('DATABASE_USER'),
      'PASSWORD':os.getenv('DATABASE_PASSWORD'),
      'HOST':os.getenv('DATABASE_HOST'),
      'PORT':'5432',
      'OPTIONS': {'sslmode': 'require'}
   }
}

3. requirements.txt:

Django==2.2.17
postgres==3.0.0
psycopg2-binary==2.8.6
psycopg2-pool==1.1
pytz==2020.4

You can refer to Connect with Managed Identity to Azure Database for PostgreSQL and Tutorial: Deploy Django app on AKS with Azure Database for PostgreSQL - Flexible Server

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1