Skip to content

Commit e078688

Browse files
authored
Merge pull request #295085 from xfz11/svc/neon
neon postgres code sample
2 parents efdc4d6 + a29f25a commit e078688

File tree

4 files changed

+349
-13
lines changed

4 files changed

+349
-13
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
title: Integrate Neon Serverless Postgres with Service Connector
3+
description: Integrate Neon Serverless Postgres into your application with Service Connector.
4+
author: maud-lv
5+
ms.author: malev
6+
ms.service: service-connector
7+
ms.topic: how-to
8+
ms.custom: engagement-fy23
9+
ms.date: 02/21/2025
10+
---
11+
12+
# Integrate Neon Serverless Postgres with Service Connector
13+
14+
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.
15+
16+
## Supported compute services
17+
18+
Service Connector can be used to connect the following compute services to Neon Serverless Postgres:
19+
20+
- Azure App Service
21+
- Azure Container Apps
22+
- Azure Functions
23+
- Azure Kubernetes Service (AKS)
24+
- Azure Spring Apps
25+
26+
## Supported authentication types and client types
27+
28+
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.
29+
30+
| Client type | System-assigned managed identity | User-assigned managed identity | Secret/connection string | Service principal |
31+
|---------------------------|:--------------------------------:|:------------------------------:|:------------------------:|:-----------------:|
32+
| .NET | No | No | Yes | No |
33+
| Go (pg) | No | No | Yes | No |
34+
| Java (JDBC) | No | No | Yes | No |
35+
| Java - Spring Boot (JDBC) | No | No | Yes | No |
36+
| Node.js (pg) | No | No | Yes | No |
37+
| PHP (native) | No | No | Yes | No |
38+
| Python (psycopg2) | No | No | Yes | No |
39+
| Python-Django | No | No | Yes | No |
40+
| Ruby (ruby-pg) | No | No | Yes | No |
41+
| None | No | No | Yes | No |
42+
43+
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.
44+
45+
## Default environment variable names or application properties and sample code
46+
47+
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](concept-service-connector-internals.md#configuration-naming-convention) article.
48+
49+
### Connection String
50+
51+
> [!WARNING]
52+
> 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.
53+
54+
#### [.NET](#tab/dotnet)
55+
56+
| Default environment variable name | Description | Example value |
57+
| ------------------------------------- | --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
58+
| `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>;` |
59+
60+
#### [Java](#tab/java)
61+
62+
| Default environment variable name | Description | Example value |
63+
| ------------------------------------- | --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
64+
| `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>` |
65+
66+
#### [SpringBoot](#tab/springBoot)
67+
68+
| Application properties | Description | Example value |
69+
| ------------------------------ | ----------------- | --------------------------------------------------------------------------------------------------------------- |
70+
| `spring.datasource.url` | Database URL | `jdbc:postgresql://ep-still-mud-a12aa123.eastus2.azure.neon.tech:5432/<database-name>?sslmode=require` |
71+
| `spring.datasource.username` | Database username | `<username>` |
72+
| `spring.datasource.password` | Database password | `<password>` |
73+
74+
#### [Python](#tab/python)
75+
76+
| Default environment variable name | Description | Example value |
77+
| ------------------------------------- | -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
78+
| `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>` |
79+
80+
#### [Django](#tab/django)
81+
82+
| Default environment variable name | Description | Example value |
83+
| --------------------------------- | ----------------- | -------------------------------------------------------- |
84+
| `NEON_POSTGRESQL_NAME` | Database name | `<database-name>` |
85+
| `NEON_POSTGRESQL_HOST` | Database host URL | `ep-still-mud-a12aa123.eastus2.azure.neon.tech` |
86+
| `NEON_POSTGRESQL_USER` | Database username | `<username>` |
87+
| `NEON_POSTGRESQL_PASSWORD` | Database password | `<database-password>` |
88+
89+
#### [Go](#tab/go)
90+
91+
| Default environment variable name | Description | Example value |
92+
|-------------------------------------|---------------------------------|---------------------------------------------------------------------------------------------------------------------------------|
93+
| `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>` |
94+
95+
#### [NodeJS](#tab/nodejs)
96+
97+
| Default environment variable name | Description | Example value |
98+
| --------------------------------- | ----------------- | -------------------------------------------------------- |
99+
| `NEON_POSTGRESQL_HOST` | Database host URL | `ep-still-mud-a12aa123.eastus2.azure.neon.tech` |
100+
| `NEON_POSTGRESQL_USER` | Database username | `<username>` |
101+
| `NEON_POSTGRESQL_PASSWORD` | Database password | `<password>` |
102+
| `NEON_POSTGRESQL_DATABASE` | Database name | `<database-name>` |
103+
| `NEON_POSTGRESQL_PORT` | Port number | `5432` |
104+
| `NEON_POSTGRESQL_SSL` | SSL option | `true` |
105+
106+
#### [PHP](#tab/php)
107+
108+
| Default environment variable name | Description | Example value |
109+
|-----------------------------------|--------------------------------------|---------------------------------------------------------------------------------------------------------------------------------|
110+
| `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>` |
111+
112+
#### [Ruby](#tab/ruby)
113+
114+
| Default environment variable name | Description | Example value |
115+
|-----------------------------------|---------------------------------|----------------------------------------------------------------------------------|
116+
| `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>` |
117+
118+
#### [Other](#tab/none)
119+
120+
| Default environment variable name | Description | Example value |
121+
| --------------------------------- | ----------------- | -------------------------------------------------------- |
122+
| `NEON_POSTGRESQL_HOST` | Database host URL | `ep-still-mud-a12aa123.eastus2.azure.neon.tech` |
123+
| `NEON_POSTGRESQL_USERNAME` | Database username | `<username>` |
124+
| `NEON_POSTGRESQL_DATABASE` | Database name | `<database-name>` |
125+
| `NEON_POSTGRESQL_PORT` | Port number | `5432` |
126+
| `NEON_POSTGRESQL_SSL` | SSL option | `true` |
127+
| `NEON_POSTGRESQL_PASSWORD` | Database password | `<password>`|
128+
129+
---
130+
131+
#### Sample code
132+
133+
Refer to the steps and code below to connect to Neon Serverless Postgres using a connection string.
134+
[!INCLUDE [code sample for postgresql secrets](./includes/code-neon-postgres-secret.md)]
135+
136+
137+
## Next steps
138+
139+
Follow the tutorials listed below to learn more about Service Connector.
140+
141+
> [!div class="nextstepaction"]
142+
> [Learn about Service Connector concepts](./concept-service-connector-internals.md)
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
---
2+
author: wchigit
3+
ms.service: service-connector
4+
ms.topic: include
5+
ms.date: 02/21/2025
6+
ms.author: wchi
7+
---
8+
9+
### [.NET](#tab/dotnet)
10+
11+
1. Install dependencies following [the Npgsql guidance](https://www.npgsql.org/doc/installation.html)
12+
1. In code, get the PostgreSQL connection string from environment variables added by Service Connector.
13+
```csharp
14+
using System;
15+
using Npgsql;
16+
17+
string connectionString = Environment.GetEnvironmentVariable("NEON_POSTGRESQL_CONNECTIONSTRING");
18+
using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
19+
{
20+
connection.Open();
21+
}
22+
```
23+
24+
### [Java](#tab/java)
25+
26+
1. Install dependencies following [the pgJDBC guidance](https://jdbc.postgresql.org/documentation/).
27+
1. In code, get the PostgreSQL connection string from environment variables added by Service Connector.
28+
```java
29+
import java.sql.Connection;
30+
import java.sql.DriverManager;
31+
import java.sql.SQLException;
32+
33+
String connectionString = System.getenv("NEON_POSTGRESQL_CONNECTIONSTRING");
34+
Connection connection = null;
35+
try {
36+
connection = DriverManager.getConnection(connectionString);
37+
System.out.println("Connection successful!");
38+
} catch (SQLException e){
39+
System.out.println(e.getMessage());
40+
}
41+
```
42+
43+
### [SpringBoot](#tab/springBoot)
44+
45+
1. 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](https://github.com/Azure/azure-sdk-for-java/wiki/Spring-Versions-Mapping#which-version-of-spring-cloud-azure-should-i-use).
46+
```xml
47+
<dependencyManagement>
48+
<dependencies>
49+
<dependency>
50+
<groupId>com.azure.spring</groupId>
51+
<artifactId>spring-cloud-azure-dependencies</artifactId>
52+
<version>4.11.0</version>
53+
<type>pom</type>
54+
<scope>import</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.azure.spring</groupId>
58+
<artifactId>spring-cloud-azure-starter-jdbc-postgresql</artifactId>
59+
</dependency>
60+
</dependencies>
61+
</dependencyManagement>
62+
```
63+
1. Set up a Spring Boot application, more details in this [section](/azure/developer/java/spring-framework/configure-spring-data-jpa-with-azure-postgresql?tabs=password%2Cservice-connector).
64+
65+
### [Python](#tab/python)
66+
67+
1. Install dependencies following [the psycopg2 guidance](https://pypi.org/project/psycopg2/).
68+
1. In code, get the PostgreSQL connection information from environment variables added by Service Connector.
69+
```python
70+
import os
71+
import psycopg2
72+
73+
connection_string = os.getenv('NEON_POSTGRESQL_CONNECTIONSTRING')
74+
connection = psycopg2.connect(connection_string)
75+
print("Connection established")
76+
77+
connection.close()
78+
```
79+
80+
### [Django](#tab/django)
81+
82+
1. Install dependencies following [the Django guidance](https://docs.djangoproject.com/en/4.2/topics/install/) and [psycopg2 guidance](https://pypi.org/project/psycopg2/).
83+
```bash
84+
pip install django
85+
pip install psycopg2
86+
```
87+
1. In the setting file, get the PostgreSQL database information from environment variables added by Service Connector.
88+
```python
89+
# in your setting file, eg. settings.py
90+
host = os.getenv('NEON_POSTGRESQL_HOST')
91+
user = os.getenv('NEON_POSTGRESQL_USER')
92+
password = os.getenv('NEON_POSTGRESQL_PASSWORD')
93+
database = os.getenv('NEON_POSTGRESQL_NAME')
94+
95+
DATABASES = {
96+
'default': {
97+
'ENGINE': 'django.db.backends.postgresql_psycopg2',
98+
'NAME': database,
99+
'USER': user,
100+
'PASSWORD': password,
101+
'HOST': host,
102+
'PORT': '5432', # Port is 5432 by default
103+
'OPTIONS': {'sslmode': 'require'},
104+
}
105+
}
106+
```
107+
108+
### [Go](#tab/go)
109+
110+
1. Install dependencies.
111+
```bash
112+
go get github.com/lib/pq
113+
```
114+
1. In code, get the PostgreSQL connection string from environment variables added by Service Connector.
115+
```go
116+
import (
117+
"database/sql"
118+
"fmt"
119+
"os"
120+
121+
_ "github.com/lib/pq"
122+
)
123+
124+
connectionString := os.Getenv("NEON_POSTGRESQL_CONNECTIONSTRING")
125+
conn, err := sql.Open("postgres", connectionString)
126+
if err != nil {
127+
panic(err)
128+
}
129+
130+
conn.Close()
131+
```
132+
133+
### [NodeJS](#tab/nodejs)
134+
135+
1. Install dependencies.
136+
```bash
137+
npm install pg dotenv
138+
```
139+
1. In code, get the PostgreSQL connection information from environment variables added by Service Connector.
140+
```javascript
141+
const { Client } = require('pg');
142+
143+
(async () => {
144+
const client = new Client({
145+
host: process.env.NEON_POSTGRESQL_HOST,
146+
user: process.env.NEON_POSTGRESQL_USER,
147+
password: process.env.NEON_POSTGRESQL_PASSWORD,
148+
database: process.env.NEON_POSTGRESQL_DATABASE,
149+
port: Number(process.env.NEON_POSTGRESQL_PORT) ,
150+
ssl: process.env.NEON_POSTGRESQL_SSL
151+
});
152+
await client.connect();
153+
154+
await client.end();
155+
})();
156+
```
157+
158+
### [PHP](#tab/php)
159+
160+
1. In code, get the PostgreSQL connection information from environment variables added by Service Connector.
161+
```php
162+
<?php
163+
$conn_string = getenv('NEON_POSTGRESQL_CONNECTIONSTRING');
164+
$dbconn = pg_connect($conn_string);
165+
?>
166+
```
167+
168+
### [Ruby](#tab/ruby)
169+
170+
1. Install dependencies.
171+
```bash
172+
gem install pg
173+
```
174+
2. In code, get the PostgreSQL connection information from environment variables added by Service Connector.
175+
```ruby
176+
require 'pg'
177+
require 'dotenv/load'
178+
179+
begin
180+
conn = PG::Connection.new(
181+
connection_string: ENV['NEON_POSTGRESQL_CONNECTIONSTRING'],
182+
)
183+
rescue PG::Error => e
184+
puts e.message
185+
186+
ensure
187+
connection.close if connection
188+
end
189+
```
190+
191+
### [Other](#tab/none)
192+
For other languages, use the connection properties that Service Connector sets to the environment variables to connect the database. For environment variable details, see [Integrate Neon Serverless Postgres with Service Connector](../how-to-integrate-neon-postgres.md).

0 commit comments

Comments
 (0)