Skip to content

Latest commit

 

History

History
192 lines (161 loc) · 5.96 KB

File metadata and controls

192 lines (161 loc) · 5.96 KB
author wchigit
ms.service service-connector
ms.topic include
ms.date 10/11/2023
ms.author wchi
  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("AZURE_POSTGRESQL_CONNECTIONSTRING");
    using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
    {
        connection.Open();
    }
  1. Install dependencies following the pgJDBC guidance.
  2. 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());
    }
  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.
    <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>
  2. Set up a Spring Boot application, more details in this section.
  1. Install dependencies following the psycopg2 guidance.
  2. 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()
  1. Install dependencies following the Django guidance and psycopg2 guidance.
    pip install django
    pip install psycopg2
  2. 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'},
        }
    }
  1. Install dependencies.
    go get github.com/lib/pq
  2. 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()
  1. Install dependencies.
    npm install pg dotenv
  2. 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();
    })();
  1. 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);
    ?>
  1. Install dependencies.
    gem install pg
  2. 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.