Edit

Integrate Neon Serverless Postgres with Service Connector

This article describes the supported authentication methods and clients, and provides sample code for connecting Neon Serverless Postgres from Azure compute services by using Service Connector. You can still connect to Neon Serverless Postgres in other programming languages without using Service Connector. This article also lists default environment variable names and values (or Spring Boot configuration) that you receive when you create the service connection.

Supported compute services

Service Connector can be used to connect the following compute services to Neon Serverless Postgres:

  • Azure App Service
  • Azure Functions
  • Azure Kubernetes Service (AKS)
  • Azure Spring Apps

Supported authentication types and client types

The table below shows which combinations of authentication methods and clients are supported for connecting your compute service to Neon Serverless Postgres using Service Connector. A “Yes” indicates that the combination is supported, while a “No” indicates that it is not supported.

Client type System-assigned managed identity User-assigned managed identity Secret/connection string Service principal
.NET No No Yes No
Go (pg) No No Yes No
Java (JDBC) No No Yes No
Java - Spring Boot (JDBC) No No Yes No
Node.js (pg) No No Yes No
PHP (native) No No Yes No
Python (psycopg2) No No Yes No
Python-Django No No Yes No
Ruby (ruby-pg) No No Yes No
None No No Yes No

This table shows that Service Connector for Neon Serverless Postgres supports only the connection string authentication method. Managed identity and service principal aren't supported for this target service.

Default environment variable names or application properties and sample code

Reference the connection details and sample code in the following tables, according to your connection's authentication type and client type, to connect compute services to Neon Serverless Postgres. For more information about naming conventions, check the Service Connector internals article.

Connection string

Warning

Microsoft recommends that you use the most secure authentication flow available. The authentication flow described in this procedure requires a very high degree of trust in the application, and carries risks that are not present in other flows. You should only use this flow when other more secure flows, such as managed identities, aren't viable.

Default environment variable name Description Example value
NEON_POSTGRESQL_CONNECTIONSTRING .NET PostgreSQL connection string Server=ep-still-mud-a12aa123.eastus2.azure.neon.tech;Database=<database-name>;Port=5432;Ssl Mode=Require;User Id=<username>;

Sample code

To connect to Neon Serverless Postgres using a connection string:

  1. Install dependencies following the Npgsql guidance
  2. In code, get the PostgreSQL connection string from environment variables added by Service Connector.
    using System;
    using Npgsql;
    
    string connectionString = Environment.GetEnvironmentVariable("NEON_POSTGRESQL_CONNECTIONSTRING");
    using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
    {
        connection.Open();
    }
    

Next steps

Follow the tutorials listed below to learn more about Service Connector.