| author | wchigit |
|---|---|
| ms.service | service-connector |
| ms.topic | include |
| ms.date | 10/11/2023 |
| ms.author | wchi |
- 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("AZURE_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("AZURE_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.xmlfile. Find the version of Spring Cloud Azure here.<dependencyManagement> <dependencies> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-dependencies</artifactId> <version>5.20.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('AZURE_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('AZURE_POSTGRESQL_HOST') user = os.getenv('AZURE_POSTGRESQL_USER') password = os.getenv('AZURE_POSTGRESQL_PASSWORD') database = os.getenv('AZURE_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("AZURE_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.AZURE_POSTGRESQL_HOST, user: process.env.AZURE_POSTGRESQL_USER, password: process.env.AZURE_POSTGRESQL_PASSWORD, database: process.env.AZURE_POSTGRESQL_DATABASE, port: Number(process.env.AZURE_POSTGRESQL_PORT) , ssl: process.env.AZURE_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('AZURE_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['AZURE_POSTGRESQL_CONNECTIONSTRING'], ) rescue PG::Error => e puts e.message ensure connection.close if connection end
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 Azure Database for PostgreSQL with Service Connector.