This page shows supported authentication methods and clients, and shows sample code you can use to connect Neon Serverless Postgres from Azure compute services using Service Connector. You might still be able to connect to Neon Serverless Postgres in other programming languages without using Service Connector. This page also shows default environment variable names and values (or Spring Boot configuration) you get 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 Container Apps
- 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 indicates that all combinations of client types and authentication methods in the table are supported. All client types can use any of the authentication methods to connect to Neon Serverless Postgres using Service Connector.
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>; |
Default environment variable name |
Description |
Example value |
NEON_POSTGRESQL_CONNECTIONSTRING |
JDBC PostgreSQL connection string |
jdbc:postgresql://ep-still-mud-a12aa123.eastus2.azure.neon.tech:5432/<database-name>?sslmode=require&user=<username>&password=<password> |
Application properties |
Description |
Example value |
spring.datasource.url |
Database URL |
jdbc:postgresql://ep-still-mud-a12aa123.eastus2.azure.neon.tech:5432/<database-name>?sslmode=require |
spring.datasource.username |
Database username |
<username> |
spring.datasource.password |
Database password |
<password> |
Default environment variable name |
Description |
Example value |
NEON_POSTGRESQL_CONNECTIONSTRING |
psycopg2 connection string |
dbname=<database-name> host=ep-still-mud-a12aa123.eastus2.azure.neon.tech port=5432 sslmode=require user=<username> password=<password> |
Default environment variable name |
Description |
Example value |
NEON_POSTGRESQL_NAME |
Database name |
<database-name> |
NEON_POSTGRESQL_HOST |
Database host URL |
ep-still-mud-a12aa123.eastus2.azure.neon.tech |
NEON_POSTGRESQL_USER |
Database username |
<username> |
NEON_POSTGRESQL_PASSWORD |
Database password |
<database-password> |
Default environment variable name |
Description |
Example value |
NEON_POSTGRESQL_CONNECTIONSTRING |
Go PostgreSQL connection string |
host=ep-still-mud-a12aa123.eastus2.azure.neon.tech dbname=<database-name> sslmode=require user=<username> password=<password> |
Default environment variable name |
Description |
Example value |
NEON_POSTGRESQL_HOST |
Database host URL |
ep-still-mud-a12aa123.eastus2.azure.neon.tech |
NEON_POSTGRESQL_USER |
Database username |
<username> |
NEON_POSTGRESQL_PASSWORD |
Database password |
<password> |
NEON_POSTGRESQL_DATABASE |
Database name |
<database-name> |
NEON_POSTGRESQL_PORT |
Port number |
5432 |
NEON_POSTGRESQL_SSL |
SSL option |
true |
Default environment variable name |
Description |
Example value |
NEON_POSTGRESQL_CONNECTIONSTRING |
PHP native PostgreSQL connection string |
host=ep-still-mud-a12aa123.eastus2.azure.neon.tech port=5432 dbname=<database-name> sslmode=require user=<username> password=<password> |
Default environment variable name |
Description |
Example value |
NEON_POSTGRESQL_CONNECTIONSTRING |
Ruby PostgreSQL connection string |
host=<your-postgres-server-name>.postgres.database.azure.com port=5432 dbname=<database-name> sslmode=require user=<username> password=<password> |
Default environment variable name |
Description |
Example value |
NEON_POSTGRESQL_HOST |
Database host URL |
ep-still-mud-a12aa123.eastus2.azure.neon.tech |
NEON_POSTGRESQL_USERNAME |
Database username |
<username> |
NEON_POSTGRESQL_DATABASE |
Database name |
<database-name> |
NEON_POSTGRESQL_PORT |
Port number |
5432 |
NEON_POSTGRESQL_SSL |
SSL option |
true |
NEON_POSTGRESQL_PASSWORD |
Database password |
<password> |
Sample code
Refer to the steps and code below to connect to Neon Serverless Postgres using a connection string.
- Install dependencies following the Npgsql guidance
- 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();
}
- Install dependencies following the pgJDBC guidance.
- In code, get the PostgreSQL connection string from environment variables added by Service Connector.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
String connectionString = System.getenv("NEON_POSTGRESQL_CONNECTIONSTRING");
Connection connection = null;
try {
connection = DriverManager.getConnection(connectionString);
System.out.println("Connection successful!");
} catch (SQLException e){
System.out.println(e.getMessage());
}
- Install the Spring Cloud Azure Starter JDBC PostgreSQL module by adding the following dependencies to your
pom.xml
file. Find the version of Spring Cloud Azure here.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-dependencies</artifactId>
<version>4.11.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter-jdbc-postgresql</artifactId>
</dependency>
</dependencies>
</dependencyManagement>
- Set up a Spring Boot application, more details in this section.
- Install dependencies following the psycopg2 guidance.
- In code, get the PostgreSQL connection information from environment variables added by Service Connector.
import os
import psycopg2
connection_string = os.getenv('NEON_POSTGRESQL_CONNECTIONSTRING')
connection = psycopg2.connect(connection_string)
print("Connection established")
connection.close()
- Install dependencies following the Django guidance and psycopg2 guidance.
pip install django
pip install psycopg2
- In the setting file, get the PostgreSQL database information from environment variables added by Service Connector.
# in your setting file, eg. settings.py
host = os.getenv('NEON_POSTGRESQL_HOST')
user = os.getenv('NEON_POSTGRESQL_USER')
password = os.getenv('NEON_POSTGRESQL_PASSWORD')
database = os.getenv('NEON_POSTGRESQL_NAME')
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': database,
'USER': user,
'PASSWORD': password,
'HOST': host,
'PORT': '5432', # Port is 5432 by default
'OPTIONS': {'sslmode': 'require'},
}
}
- Install dependencies.
go get github.com/lib/pq
- In code, get the PostgreSQL connection string from environment variables added by Service Connector.
import (
"database/sql"
"fmt"
"os"
_ "github.com/lib/pq"
)
connectionString := os.Getenv("NEON_POSTGRESQL_CONNECTIONSTRING")
conn, err := sql.Open("postgres", connectionString)
if err != nil {
panic(err)
}
conn.Close()
- Install dependencies.
npm install pg dotenv
- In code, get the PostgreSQL connection information from environment variables added by Service Connector.
import { Client } from 'pg';
(async () => {
const client = new Client({
host: process.env.NEON_POSTGRESQL_HOST,
user: process.env.NEON_POSTGRESQL_USER,
password: process.env.NEON_POSTGRESQL_PASSWORD,
database: process.env.NEON_POSTGRESQL_DATABASE,
port: Number(process.env.NEON_POSTGRESQL_PORT) ,
ssl: process.env.NEON_POSTGRESQL_SSL
});
await client.connect();
await client.end();
})();
- In code, get the PostgreSQL connection information from environment variables added by Service Connector.
<?php
$conn_string = getenv('NEON_POSTGRESQL_CONNECTIONSTRING');
$dbconn = pg_connect($conn_string);
?>
- Install dependencies.
gem install pg
- In code, get the PostgreSQL connection information from environment variables added by Service Connector.
require 'pg'
require 'dotenv/load'
begin
conn = PG::Connection.new(
connection_string: ENV['NEON_POSTGRESQL_CONNECTIONSTRING'],
)
rescue PG::Error => e
puts e.message
ensure
connection.close if connection
end
Next steps
Follow the tutorials listed below to learn more about Service Connector.