Skip to content

Issues accessing PostgreSQL connection string using Configuration.GetConnectionString() #21129

@leighghunt

Description

@leighghunt

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="<my real connection string>"

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
<my real connection string>

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="<my real connection string>"
<my real connection string>
<my real connection string>
[null]

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions