Skip to content

Commit b1888fd

Browse files
committed
neon postgres code sample
1 parent 155720e commit b1888fd

File tree

4 files changed

+348
-9
lines changed

4 files changed

+348
-9
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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/02/2024
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 to other cloud 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+
> [!NOTE]
46+
> System-assigned managed identity, User-assigned managed identity and Service principal are only supported on Azure CLI.
47+
48+
## Default environment variable names or application properties and sample code
49+
50+
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.
51+
52+
### Connection String
53+
54+
> [!WARNING]
55+
> 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.
56+
57+
#### [.NET](#tab/dotnet)
58+
59+
| Default environment variable name | Description | Example value |
60+
| ------------------------------------- | --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
61+
| `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>;` |
62+
63+
#### [Java](#tab/java)
64+
65+
| Default environment variable name | Description | Example value |
66+
| ------------------------------------- | --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
67+
| `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>` |
68+
69+
#### [SpringBoot](#tab/springBoot)
70+
71+
| Application properties | Description | Example value |
72+
| ------------------------------ | ----------------- | --------------------------------------------------------------------------------------------------------------- |
73+
| `spring.datasource.url` | Database URL | `jdbc:postgresql://ep-still-mud-a12aa123.eastus2.azure.neon.tech:5432/<database-name>?sslmode=require` |
74+
| `spring.datasource.username` | Database username | `<username>` |
75+
| `spring.datasource.password` | Database password | `<password>` |
76+
77+
#### [Python](#tab/python)
78+
79+
| Default environment variable name | Description | Example value |
80+
| ------------------------------------- | -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
81+
| `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>` |
82+
83+
#### [Django](#tab/django)
84+
85+
| Default environment variable name | Description | Example value |
86+
| --------------------------------- | ----------------- | -------------------------------------------------------- |
87+
| `NEON_POSTGRESQL_NAME` | Database name | `<database-name>` |
88+
| `NEON_POSTGRESQL_HOST` | Database host URL | `ep-still-mud-a12aa123.eastus2.azure.neon.tech` |
89+
| `NEON_POSTGRESQL_USER` | Database username | `<username>` |
90+
| `NEON_POSTGRESQL_PASSWORD` | Database password | `<database-password>` |
91+
92+
#### [Go](#tab/go)
93+
94+
| Default environment variable name | Description | Example value |
95+
|-------------------------------------|---------------------------------|---------------------------------------------------------------------------------------------------------------------------------|
96+
| `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>` |
97+
98+
#### [NodeJS](#tab/nodejs)
99+
100+
| Default environment variable name | Description | Example value |
101+
| --------------------------------- | ----------------- | -------------------------------------------------------- |
102+
| `NEON_POSTGRESQL_HOST` | Database host URL | `ep-still-mud-a12aa123.eastus2.azure.neon.tech` |
103+
| `NEON_POSTGRESQL_USER` | Database username | `<username>` |
104+
| `NEON_POSTGRESQL_PASSWORD` | Database password | `<password>` |
105+
| `NEON_POSTGRESQL_DATABASE` | Database name | `<database-name>` |
106+
| `NEON_POSTGRESQL_PORT` | Port number | `5432` |
107+
| `NEON_POSTGRESQL_SSL` | SSL option | `true` |
108+
109+
#### [PHP](#tab/php)
110+
111+
| Default environment variable name | Description | Example value |
112+
|-----------------------------------|--------------------------------------|---------------------------------------------------------------------------------------------------------------------------------|
113+
| `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>` |
114+
115+
#### [Ruby](#tab/ruby)
116+
117+
| Default environment variable name | Description | Example value |
118+
|-----------------------------------|---------------------------------|----------------------------------------------------------------------------------|
119+
| `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>` |
120+
121+
#### [Other](#tab/none)
122+
123+
| Default environment variable name | Description | Example value |
124+
| --------------------------------- | ----------------- | -------------------------------------------------------- |
125+
| `NEON_POSTGRESQL_HOST` | Database host URL | `ep-still-mud-a12aa123.eastus2.azure.neon.tech` |
126+
| `NEON_POSTGRESQL_USERNAME` | Database username | `<username>` |
127+
| `NEON_POSTGRESQL_DATABASE` | Database name | `<database-name>` |
128+
| `NEON_POSTGRESQL_PORT` | Port number | `5432` |
129+
| `NEON_POSTGRESQL_SSL` | SSL option | `true` |
130+
| `NEON_POSTGRESQL_PASSWORD` | Database password | `<password>`|
131+
132+
---
133+
134+
#### Sample code
135+
136+
Refer to the steps and code below to connect to Neon Serverless Postgres using a connection string.
137+
[!INCLUDE [code sample for postgresql secrets](./includes/code-neon-postgres-secret.md)]
138+
139+
140+
## Next steps
141+
142+
Follow the tutorials listed below to learn more about Service Connector.
143+
144+
> [!div class="nextstepaction"]
145+
> [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: 10/11/2023
6+
ms.author: wchi
7+
---
8+
9+
### [.NET](#tab/dotnet)
10+
11+
1. Install dependencies. Follow the guidance to [install Npgsql](https://www.npgsql.org/doc/installation.html)
12+
1. In code, get the PostgreSQL connection string from environment variables added by Service Connector service.
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. Follow the guidance to [install pgJDBC](https://jdbc.postgresql.org/documentation/).
27+
1. In code, get the PostgreSQL connection string from environment variables added by Service Connector service.
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 App 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. Follow the guidance to [install psycopg2](https://pypi.org/project/psycopg2/).
68+
1. In code, get the PostgreSQL connection information from environment variables added by Service Connector service.
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. Follow the guidance to [install Django](https://docs.djangoproject.com/en/4.2/topics/install/) and [psycopg2](https://pypi.org/project/psycopg2/).
83+
```bash
84+
pip install django
85+
pip install psycopg2
86+
```
87+
1. In setting file, get the PostgreSQL database information from environment variables added by Service Connector service.
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 service.
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 service.
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 service.
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+
1. In code, get the PostgreSQL connection information from environment variables added by Service Connector service.
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)