Issues accessing PostgreSQL connection string using Configuration.GetConnectionString()
- Dominant language
- Python
- Stars
- 4.6k
- Forks
- 3.5k
- Avg merge
- 3d 2h
- Merged PRs (30d)
- 60
Description
Not sure if this is the best place to record this, but I found accessing Postgres connection strings in an Azure webapp confusing, and I think a note on this page could be really useful.
I have a .Net6 web app, and when I set the connection string in my Azure App Settings page, a Postgres connection string doesn't come through in ```Configuration.GetConnectionString()``` in the same was as a SQL connection string does - instead, I just get my default string from my ```appsettings.json``` file.
E.g if I set the following settings in code:
**```appsettings.json```**
```
{
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Port=5432;User ID=postgres;Password=<***>;Database=timelapse
"
},
...
...
```
and the following in application settings:
```
az webapp config connection-string set --connection-string-type PostgreSQL --name $AZURE_APP_NAME --resource-group $AZURE_RESOURCE_GROUP --settings DefaultConnection=""
```
The following code:
```
_logger.LogInformation(_configuration["ConnectionStrings:DefaultConnection"]);
_logger.LogInformation(_configuration.GetConnectionString("DefaultConnection"));
_logger.LogInformation(_configuration["POSTGRESQLCONNSTR_DefaultConnection"]);
```
gives the following results:
```
Host=localhost;Port=5432;User ID=postgres;Password=<***>;=timelapse
Host=localhost;Port=5432;User ID=postgres;Password=<***>;=timelapse
```
If however I set the connection string in Azure as type **AzureSQL** rather than **PostgreSQL**, it gives the following results, with the azure application setting connection string correctly taking precedence over `appsettings.json`.
```
az webapp config connection-string set --connection-string-type SQLAzure --name $AZURE_APP_NAME --resource-group $AZURE_RESOURCE_GROUP --settings DefaultConnection=""
```
```
[null]
```
---
#### Document Details
⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*
* ID: f2b885be-54f8-23f0-9fff-ec4feb157553
* Version Independent ID: 2ad01dad-e05d-e129-13b6-d1ed09db7086
* Content: [az webapp config connection-string](https://docs.microsoft.com/en-us/cli/azure/webapp/config/connection-string?view=azure-cli-latest)
* Content Source: [latest/docs-ref-autogen/webapp/config/connection-string.yml](https://github.com/MicrosoftDocs/azure-docs-cli/blob/main/latest/docs-ref-autogen/webapp/config/connection-string.yml)
* Service: **app-service-web**
* GitHub Login: @rloutlaw
* Microsoft Alias: **routlaw**
Contributor guide
Assessment
This issue has not been assessed yet.