[Yandex Cloud documentation](../../../index.md) > [Yandex MPP Analytics for PostgreSQL](../../index.md) > [Step-by-step guides](../index.md) > [Connection](index.md) > Code examples

# Code examples for connecting to a Yandex MPP Analytics for PostgreSQL cluster

**Examples were tested in the following environment:**

* Yandex Cloud virtual machine running Ubuntu 20.04 LTS:
    * Bash: `5.0.16`.
* Yandex Cloud virtual machine running Windows Server 2019 Datacenter:
    * PostgreSQL: `13`.
    * PowerShell: `5.1.17763.1490 Desktop`.

Creating a Yandex MPP Analytics for PostgreSQL cluster does not entail creating a user database. To test the connection, use the `postgres` service database.

To connect to a publicly accessible cluster, [prepare an SSL certificate](index.md#get-ssl-cert). In these examples, the `root.crt` SSL certificate is located in the following directory:

* `/home/<home_directory>/.postgresql/` for Ubuntu.
* `$HOME\AppData\Roaming\postgresql` for Windows.

You can connect to a cluster using either a master host's regular FQDN or a primary master host's [special FQDN](fqdn.md#fqdn-master). When connecting using a JDBC connector, you can specify two master hosts at the same time. To learn how to get a host's FQDN, see [these guides](fqdn.md#get-fqdn).

To see code examples with the host FQDN filled in, open the cluster page in the [management console](https://console.yandex.cloud) and click **Connect**.

## C# EF Core {#csharpefcore}

Required packages:

* Microsoft.EntityFrameworkCore
* Npgsql.EntityFrameworkCore.PostgreSQL

{% list tabs group=connection %}

- Connecting with SSL {#with-ssl}

  ```csharp
  using System;
  using System.Threading.Tasks;
  using Microsoft.EntityFrameworkCore;

  namespace ConsoleApp
  {
      public class VersionString
      {
          public int id { get; set; }
          public string versionString { get; set; }
      }
      public class ApplicationContext : DbContext
      {
          public ApplicationContext()
          {
              Database.EnsureCreated();
          }
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
              var host      = "c-<cluster_ID>.rw.mdb.yandexcloud.net";
              var port      = "6432";
              var db        = "postgres";
              var username  = "<username>";
              var password  = "<user_password>";
              optionsBuilder.UseNpgsql($"Host={host};Port={port};Database={db};Username={username};Password={password};Ssl Mode=Require;Trust Server Certificate=true;");
          }
          public DbSet<VersionString> VersionStrings { get; set; }

      }
      class Program
      {
          static async Task Main(string[] args)
          {
              using (ApplicationContext db = new ApplicationContext())
              {
                  var versionStrings = await db.VersionStrings.FromSqlRaw(@"select 1 as id, version() as versionString;").ToListAsync();
                  Console.WriteLine(versionStrings[0].versionString);
              }
          }
      }
  }
  ```

{% endlist %}

## Go {#go}

Before connecting, install the required dependencies:

```bash
sudo apt update && sudo apt install --yes golang git && \
go mod init example && go get github.com/jackc/pgx/v4
```

{% list tabs group=connection %}

- Connecting without SSL {#without-ssl}

  1. Code example:

      `connect.go`

      ```go
      package main

      import (
      	"context"
      	"fmt"
      	"os"

      	"github.com/jackc/pgx/v4"
      )

      const (
      	host     = "c-<cluster_ID>.rw.mdb.yandexcloud.net"
      	port     = 6432
      	user     = "<username>"
      	password = "<user_password>"
      	dbname   = "postgres"
      )

      func main() {

      	connstring := fmt.Sprintf(
      		"host=%s port=%d dbname=%s user=%s password=%s target_session_attrs=read-write",
      		host, port, dbname, user, password)

      	connConfig, err := pgx.ParseConfig(connstring)
      	if err != nil {
      		fmt.Fprintf(os.Stderr, "Unable to parse config: %v\n", err)
      		os.Exit(1)
      	}

      	conn, err := pgx.ConnectConfig(context.Background(), connConfig)
      	if err != nil {
      		fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
      		os.Exit(1)
      	}

      	defer conn.Close(context.Background())

      	var version string

      	err = conn.QueryRow(context.Background(), "select version()").Scan(&version)
      	if err != nil {
      		fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
      		os.Exit(1)
      	}

      	fmt.Println(version)
      }
      ```

  1. Connecting:

      ```bash
      go run connect.go
      ```

- Connecting with SSL {#with-ssl}

  1. Code example:

      `connect.go`

      ```go
      package main

      import (
      	"context"
      	"crypto/tls"
      	"crypto/x509"
      	"fmt"
      	"os"

      	"github.com/jackc/pgx/v4"
      )

      const (
      	host     = "c-<cluster_ID>.rw.mdb.yandexcloud.net"
      	port     = 6432
      	user     = "<username>"
      	password = "<user_password>"
      	dbname   = "postgres"
      	ca       = "/home/<home_directory>/.postgresql/root.crt"
      )

      func main() {

      	rootCertPool := x509.NewCertPool()
      	pem, err := ioutil.ReadFile(ca)
      	if err != nil {
      		panic(err)
      	}

      	if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
      		panic("Failed to append PEM.")
      	}

      	connstring := fmt.Sprintf(
      		"host=%s port=%d dbname=%s user=%s password=%s sslmode=verify-full target_session_attrs=read-write",
      		host, port, dbname, user, password)

      	connConfig, err := pgx.ParseConfig(connstring)
      	if err != nil {
      		fmt.Fprintf(os.Stderr, "Unable to parse config: %v\n", err)
      		os.Exit(1)
      	}

      	connConfig.TLSConfig = &tls.Config{
      		RootCAs:            rootCertPool,
      		ServerName: "c-<cluster_ID>.rw.mdb.yandexcloud.net",
      	}

      	conn, err := pgx.ConnectConfig(context.Background(), connConfig)
      	if err != nil {
      		fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
      		os.Exit(1)
      	}

      	defer conn.Close(context.Background())

      	var version string

      	err = conn.QueryRow(context.Background(), "select version()").Scan(&version)
      	if err != nil {
      		fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
      		os.Exit(1)
      	}

      	fmt.Println(version)
      }
      ```

      For this connection method, you must specify the full path to the PostgreSQL `root.crt` certificate in the `ca` variable.

  1. Connecting:

      ```bash
      go run connect.go
      ```

{% endlist %}

## Java {#java}

Before connecting:

1. Install the dependencies:

    ```bash
    sudo apt update && sudo apt install --yes default-jdk maven
    ```

1. Create a directory for the Maven project:

    ```bash
    cd ~/ && mkdir --parents project/src/java/com/example && cd project/
    ```

1. Create a configuration file for Maven:

    {% cut "pom.xml" %}

    ```xml
    <?xml version="1.0" encoding="utf-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

      <modelVersion>4.0.0</modelVersion>
      <groupId>com.example</groupId>
      <artifactId>app</artifactId>
      <packaging>jar</packaging>
      <version>0.1.0</version>
      <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
      </properties>
      <dependencies>
        <dependency>
          <groupId>org.postgresql</groupId>
          <artifactId>postgresql</artifactId>
          <version>42.2.16</version>
        </dependency>
      </dependencies>
      <build>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <sourceDirectory>src</sourceDirectory>
        <resources>
          <resource>
            <directory>src</directory>
          </resource>
        </resources>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
              <execution>
                <goals>
                  <goal>attached</goal>
                </goals>
                <phase>package</phase>
                <configuration>
                  <descriptorRefs>
                    <descriptorRef>
                    jar-with-dependencies</descriptorRef>
                  </descriptorRefs>
                  <archive>
                    <manifest>
                      <mainClass>com.example.App</mainClass>
                    </manifest>
                  </archive>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
              <archive>
                <manifest>
                  <mainClass>com.example.App</mainClass>
                </manifest>
              </archive>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    ```

    {% endcut %}

    You can check the current `postgresql` version on the [project page in the Maven repository](https://mvnrepository.com/artifact/org.postgresql/postgresql).

{% list tabs group=connection %}

- Connecting without SSL {#without-ssl}

  1. Code example:

      `src/java/com/example/App.java`

      ```java
      package com.example;

      import java.sql.*;

      public class App {
        public static void main(String[] args) {
          String DB_URL  = "jdbc:postgresql://<primary_master_host_FQDN>:6432,<standby_master_host_FQDN>:6432/postgres?targetServerType=master&ssl=false&sslmode=disable";
          String DB_USER = "<username>";
          String DB_PASS = "<user_password>";

          try {
            Class.forName("org.postgresql.Driver");

            Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
            ResultSet q = conn.createStatement().executeQuery("SELECT version()");
            if (q.next()) {
              System.out.println(q.getString(1));
            }

            conn.close();
          } catch (Exception ex) {
            ex.printStackTrace();
          }
        }
      }
      ```

  1. Building and connecting:

      ```bash
      mvn clean package && \
      java -jar target/app-0.1.0-jar-with-dependencies.jar
      ```

- Connecting with SSL {#with-ssl}

  1. Code example:

      `src/java/com/example/App.java`

      ```java
      package com.example;

      import java.sql.*;

      public class App {
        public static void main(String[] args) {
          String DB_URL  = "jdbc:postgresql://<primary_master_host_FQDN>:6432,<standby_master_host_FQDN>:6432/postgres?targetServerType=master&ssl=true&sslmode=verify-full";
          String DB_USER = "<username>";
          String DB_PASS = "<user_password>";

          try {
            Class.forName("org.postgresql.Driver");

            Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
            ResultSet q = conn.createStatement().executeQuery("SELECT version()");
            if (q.next()) {
              System.out.println(q.getString(1));
            }

            conn.close();
          } catch (Exception ex) {
            ex.printStackTrace();
          }
        }
      }
      ```

  1. Building and connecting:

      ```bash
      mvn clean package && \
      java -jar target/app-0.1.0-jar-with-dependencies.jar
      ```

{% endlist %}

## Node.js {#nodejs}

Before connecting, install the required dependencies:

```bash
sudo apt update && sudo apt install --yes nodejs npm && \
npm install pg
```

{% list tabs group=connection %}

- Connecting without SSL {#without-ssl}

    `app.js`

    ```javascript
    "use strict";
    const pg = require("pg");

    const config = {
        connectionString:
            "postgres://<username>:<user_password>@c-<cluster_ID>.rw.mdb.yandexcloud.net:6432/postgres"
    };

    const conn = new pg.Client(config);

    conn.connect((err) => {
        if (err) throw err;
    });
    conn.query("SELECT version()", (err, q) => {
        if (err) throw err;
        console.log(q.rows[0].version);
        conn.end();
    });
    ```

- Connecting with SSL {#with-ssl}

    `app.js`

    ```javascript
    "use strict";
    const fs = require("fs");
    const pg = require("pg");

    const config = {
        connectionString:
            "postgres://<username>:<user_password>@c-<cluster_ID>.rw.mdb.yandexcloud.net:6432/postgres",
        ssl: {
            rejectUnauthorized: true,
            ca: fs
                .readFileSync("/home/<home_directory>/.postgresql/root.crt")
                .toString(),
        },
    };

    const conn = new pg.Client(config);

    conn.connect((err) => {
        if (err) throw err;
    });
    conn.query("SELECT version()", (err, q) => {
        if (err) throw err;
        console.log(q.rows[0].version);
        conn.end();
    });
    ```

    For this connection method, you must specify the full path to the PostgreSQL `root.crt` certificate in the `ca` variable.

{% endlist %}

Connecting:

```bash
node app.js
```

## ODBC {#odbc}

Before connecting, install the required dependencies:

```bash
sudo apt update && sudo apt install --yes unixodbc odbc-postgresql
```

The system will automatically register the PostgreSQL ODBC driver in `/etc/odbcinst.ini`.

{% list tabs group=connection %}

- Connecting without SSL {#without-ssl}

  1. Code example:

      `/etc/odbc.ini`

      ```ini
      [postgresql]
      Driver=PostgreSQL Unicode
      Servername=c-<cluster_ID>.rw.mdb.yandexcloud.net
      Username=<username>
      Password=<user_password>
      Database=postgres
      Port=6432
      Pqopt=target_session_attrs=read-write
      ```

  1. Connecting:

      ```bash
      isql -v postgresql
      ```

      Once connected to the DBMS, run the `SELECT version();` command.

- Connecting with SSL {#with-ssl}

  1. Code example:

      `/etc/odbc.ini`

      ```ini
      [postgresql]
      Driver=PostgreSQL Unicode
      Servername=c-<cluster_ID>.rw.mdb.yandexcloud.net
      Username=<username>
      Password=<user_password>
      Database=postgres
      Port=6432
      Pqopt=target_session_attrs=read-write
      Sslmode=verify-full
      ```

  1. Connecting:

      ```bash
      isql -v postgresql
      ```

      Once connected to the DBMS, run the `SELECT version();` command.

{% endlist %}

## PHP {#php}

Before connecting, install the required dependencies:

```bash
sudo apt update && sudo apt install --yes php php-pgsql
```

{% list tabs group=connection %}

- Connecting without SSL {#without-ssl}

  1. Code example:

      `connect.php`

      ```php
      <?php
        $conn = pg_connect("
            host=c-<cluster_ID>.rw.mdb.yandexcloud.net
            port=6432
            sslmode=disable
            dbname=postgres
            user=<username>
            password=<user_password>
            target_session_attrs=read-write
        ");

      $q = pg_query($conn, "SELECT version()");
      $result = pg_fetch_row($q);
      echo $result[0];

      pg_close($conn);
      ?>
      ```

  1. Connecting:

      ```bash
      php connect.php
      ```

- Connecting with SSL {#with-ssl}

  1. Code example:

      `connect.php`

      ```php
      <?php
        $conn = pg_connect("
            host=c-<cluster_ID>.rw.mdb.yandexcloud.net
            port=6432
            sslmode=verify-full
            dbname=postgres
            user=<username>
            password=<user_password>
            target_session_attrs=read-write
        ");

      $q = pg_query($conn, "SELECT version()");
      $result = pg_fetch_row($q);
      echo $result[0];

      pg_close($conn);
      ?>
      ```

  1. Connecting:

      ```bash
      php connect.php
      ```

{% endlist %}

## Python {#python}

Before connecting, install the required dependencies:

```bash
sudo apt update && sudo apt install --yes python3 python3-pip && \
pip3 install psycopg2-binary
```

{% list tabs group=connection %}

- Connecting without SSL {#without-ssl}

  1. Code example:

      `connect.py`

      ```python
      import psycopg2

      conn = psycopg2.connect("""
          host=c-<cluster_ID>.rw.mdb.yandexcloud.net
          port=6432
          sslmode=disable
          dbname=postgres
          user=<username>
          password=<user_password>
          target_session_attrs=read-write
      """)

      q = conn.cursor()
      q.execute('SELECT version()')

      print(q.fetchone())

      conn.close()
      ```

  1. Connecting:

      ```bash
      python3 connect.py
      ```

- Connecting with SSL {#with-ssl}

  1. Code example:

      `connect.py`

      ```python
      import psycopg2

      conn = psycopg2.connect("""
          host=c-<cluster_ID>.rw.mdb.yandexcloud.net
          port=6432
          sslmode=verify-full
          dbname=postgres
          user=<username>
          password=<user_password>
          target_session_attrs=read-write
      """)

      q = conn.cursor()
      q.execute('SELECT version()')

      print(q.fetchone())

      conn.close()
      ```

  1. Connecting:

      ```bash
      python3 connect.py
      ```

{% endlist %}

## Ruby {#ruby}

Before connecting, install the required dependencies:

```bash
sudo apt update && sudo apt install --yes ruby ruby-pg
```

{% list tabs group=connection %}

- Connecting without SSL {#without-ssl}

  1. Code example:

      `connect.rb`

      ```ruby
      require "pg"

      conn = PG.connect("
              host=c-<cluster_ID>.rw.mdb.yandexcloud.net
              port=6432
              dbname=postgres
              user=<username>
              password=<user_password>
              target_session_attrs=read-write
              sslmode=disable
      ")

      q = conn.exec("SELECT version()")
      puts q.getvalue 0, 0

      conn.close()
      ```

  1. Connecting:

      ```bash
      ruby connect.rb
      ```

- Connecting with SSL {#with-ssl}

  1. Code example:

      `connect.rb`

      ```ruby
      require "pg"

      conn = PG.connect("
              host=c-<cluster_ID>.rw.mdb.yandexcloud.net
              port=6432
              dbname=postgres
              user=<username>
              password=<user_password>
              target_session_attrs=read-write
              sslmode=verify-full
      ")

      q = conn.exec("SELECT version()")
      puts q.getvalue 0, 0

      conn.close()
      ```

  1. Connecting:

      ```bash
      ruby connect.rb
      ```

{% endlist %}