---
title: "Connect to a PostgreSQL TimescaleDB cluster"
sidebar_label: "Connect to a cluster"
sidebar_position: 9
description: "How to connect to a PostgreSQL TimescaleDB cluster with or without an SSL certificate in the console and from various programming languages"
---

import Formbricks from '@theme/MDXComponents/Formbricks'
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
import {TabItemLabel} from '@selectel/docux/components'
import ConnectionAddresses from '@site/i18n/en/docusaurus-plugin-content-docs/current/_partials/managed-databases/common/connection-addresses.mdx'

# Connect to a PostgreSQL TimescaleDB cluster

You can connect to a PostgreSQL TimescaleDB cluster:

* via the psql console client;
* via graphical database management tools: pgAdmin or office suites with ODBC or JDBC support;
* from program code.

For all methods, you can connect [via SSL](#connect-with-ssl) and [without SSL](#connect-without-SSL).

For connection, use the [port](#connection-ports) and [address](#connection-addresses).

## Connection ports \{#connection-ports}

Use the following ports to connect to PostgreSQL TimescaleDB:

* 5433 — port for connecting to the selected node via [connection pooler](/managed-databases/timescaledb/connection-pooler.mdx) — allows reducing the load on PostgreSQL TimescaleDB;
* 5432 — port for connecting directly to the PostgreSQL TimescaleDB process.

## Connection addresses \{#connection-addresses}

<ConnectionAddresses DatabaseName="PostgreSQL TimescaleDB" slug="timescaledb" />

## Connect via SSL \{#connect-with-ssl}

Connecting using TLS (SSL) encryption ensures a secure connection between your server and the database cluster.

<Tabs queryString="connect-with-ssl">
  <TabItem value="bash" default>
    <TabItemLabel>
      Bash
    </TabItemLabel>

    1. Download the root certificate and place it in the `~/.postgresql/`:

       ```bash
       mkdir -p ~/.postgresql/
       wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.postgresql/root.crt
       chmod 0600 ~/.postgresql/root.crt
       ```

    2. Connect to the cluster:

       ```bash
       psql "host=<host> \
             port=<port> \
             dbname=<database_name> \
             user=<database_user_name> \
             sslmode=verify-ca"
       ```

       Specify:

       * `<host>` — node DNS address or [public IP address](/managed-databases/timescaledb/public-ip.mdx) (Floating IP);
       * `<port>` — [connection port](#connection-ports);
       * `<database_name>` — database name;
       * `<database_user_name>` — database user name.
  </TabItem>

  <TabItem value="powershell">
    <TabItemLabel>
      PowerShell
    </TabItemLabel>

    1. In the [Control Panel](https://my.selectel.ru/vpc/default/dbaas/), click **Download Certificate** to download the root certificate and place it in the `%APPDATA%\postgresql\`.
    2. Connect to the cluster:

       ```bash
       psql "host=<host> `
             port=<port> `
             dbname=<database_name> `
             user=<database_user_name> `
             sslmode=verify-ca"
       ```

       Specify:

       * `<host>` — node DNS address or [public IP address](/managed-databases/timescaledb/public-ip.mdx) (Floating IP);
       * `<port>` — [connection port](#connection-ports);
       * `<database_name>` — database name;
       * `<database_user_name>` — database user name.
  </TabItem>

  <TabItem value="python">
    <TabItemLabel>
      Python
    </TabItemLabel>

    1. Download the root certificate and place it in the `~/.postgresql/`:

       ```bash
       mkdir -p ~/.postgresql/
       wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.postgresql/root.crt
       chmod 0600 ~/.postgresql/root.crt
       ```

    2. Install the psycopg2 library:

       ```bash
       pip3 install psycopg2-binary
       ```

    3. Use the following connection example:

       ```python
       import psycopg2

       conn = psycopg2.connect("""
             host=<host>
             dbname=<database_name>
             user=<database_user_name>
             password=<password>
             sslrootcert=<path>
             sslmode=verify-ca
             port=<port>
       """)

       cur = conn.cursor()
       cur.execute('SELECT 40+2')

       print(cur.fetchone())

       cur.close()
       conn.close()
       ```

       Specify:

       * `<host>` — node DNS address or [public IP address](/managed-databases/timescaledb/public-ip.mdx) (Floating IP);
       * `<database_name>` — database name;
       * `<database_user_name>` — database user name;
       * `<password>` — user password;
       * `<path>` — full path to the root certificate;
       * `<port>` — [connection port](#connection-ports).
  </TabItem>

  <TabItem value="php">
    <TabItemLabel>
      PHP
    </TabItemLabel>

    1. Download the root certificate and place it in the `~/.postgresql/`:

       ```bash
       mkdir -p ~/.postgresql/
       wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.postgresql/root.crt
       chmod 0600 ~/.postgresql/root.crt
       ```

    2. Install the pgsql library:

       ```bash
       apt install php-pgsql
       ```

    3. Use the following connection example:

       ```php
       <?php

       $dbconn = pg_connect("
             host=<host>
             dbname=<database_name>
             user=<database_user_name>
             password=<password>
             sslrootcert=<path>
             sslmode=verify-ca
             port=<port>
       ");

       $query = 'SELECT 40 + 2';
       $result = pg_query($query);
       $row = pg_fetch_row($result);

       echo $row[0];

       pg_close($dbconn);
       ?>
       ```

       Specify:

       * `<host>` — node DNS address or [public IP address](/managed-databases/timescaledb/public-ip.mdx) (Floating IP);
       * `<database_name>` — database name;
       * `<database_user_name>` — database user name;
       * `<password>` — user password;
       * `<path>` — full path to the root certificate;
       * `<port>` — [connection port](#connection-ports).
  </TabItem>

  <TabItem value="go">
    <TabItemLabel>
      Go
    </TabItemLabel>

    1. Download the root certificate and place it in the `~/.postgresql/`:

       ```bash
       mkdir -p ~/.postgresql/
       wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.postgresql/root.crt
       chmod 0600 ~/.postgresql/root.crt
       ```

    2. Use the following connection example:

       ```go
       package main

       import (
             "context"
             "fmt"
             "os"

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

       func main() {

             connectionString := fmt.Sprintf("postgres://%s:%s@%s:<port>/%s?sslmode=verify-ca&sslrootcert=%s",
                 "<database_user_name>",
                 "<password>",
                 "<host>",
                 "<database_name>",
                 "<path>"
             )

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

             var sum int64
             err = conn.QueryRow(context.Background(), "SELECT 40+2").Scan(&sum)
             if err != nil {
                 fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
                 os.Exit(1)
             }

             fmt.Println(sum)
       }
       ```

       Specify:

       * `<port>` — [connection port](#connection-ports);
       * `<database_user_name>` — database user name;
       * `<password>` — user password;
       * `<host>` — node DNS address or [public IP address](/managed-databases/timescaledb/public-ip.mdx) (Floating IP);
       * `<database_name>` — database name;
       * `<path>` — full path to the root certificate.
  </TabItem>

  <TabItem value="nodejs">
    <TabItemLabel>
      Node.js
    </TabItemLabel>

    1. Download the root certificate and place it in the `~/.postgresql/`:

       ```bash
       mkdir -p ~/.postgresql/
       wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.postgresql/root.crt
       chmod 0600 ~/.postgresql/root.crt
       ```

    2. Install the pg library:

       ```bash
       npm install pg
       ```

    3. Use the following connection example:

       ```js
       const fs = require('fs');
       const pg = require('pg');

       const config = {
           host: '<host>',
           port: '<port>',
           database: '<database_name>',
           user: '<database_user_name>',
           password: '<password>',
           ssl: {
             rejectUnauthorized: true,
             ca: fs.readFileSync('<path>').toString(),
           },
       };

       const client = new pg.Client(config);

       client.connect((error) => {
           if (error) throw error;
       });

       client.query('SELECT 40 + 2 AS sum', (error, res) => {
           if (error) throw error;
           console.log(res.rows);
           client.end();
       });
       ```

       Specify:

       * `<host>` — node DNS address or [public IP address](/managed-databases/timescaledb/public-ip.mdx) (Floating IP);
       * `<port>` — [connection port](#connection-ports);
       * `<database_name>` — database name;
       * `<database_user_name>` — database user name;
       * `<password>` — user password;
       * `<path>` — full path to the root certificate.
  </TabItem>
</Tabs>

## Connect without SSL \{#connect-without-SSL}

<Tabs queryString="connect-without-ssl">
  <TabItem value="bash" default>
    <TabItemLabel>
      Bash
    </TabItemLabel>

    1. Open the CLI.
    2. Connect to the cluster:

       ```bash
       psql "host=<host> \
             port=<port> \
             dbname=<database_name> \
             user=<database_user_name> \
             sslmode=disable"
       ```

       Specify:

       * `<host>` — node DNS address or [public IP address](/managed-databases/timescaledb/public-ip.mdx) (Floating IP);
       * `<port>` — [connection port](#connection-ports);
       * `<database_name>` — database name;
       * `<database_user_name>` — database user name.
  </TabItem>

  <TabItem value="powershell">
    <TabItemLabel>
      PowerShell
    </TabItemLabel>

    1. Open the CLI.
    2. Connect to the cluster:

       ```bash
       psql "host=<host> `
             port=<port> `
             dbname=<database_name> `
             user=<database_user_name> `
             sslmode=disable"
       ```

       Specify:

       * `<host>` — node DNS address or [public IP address](/managed-databases/timescaledb/public-ip.mdx) (Floating IP);
       * `<port>` — [connection port](#connection-ports);
       * `<database_name>` — database name;
       * `<database_user_name>` — database user name.
  </TabItem>

  <TabItem value="python">
    <TabItemLabel>
      Python
    </TabItemLabel>

    1. Install the psycopg2 library:

       ```bash
       pip3 install psycopg2-binary
       ```

    2. Use the following connection example:

       ```python
       import psycopg2

       conn = psycopg2.connect("""
             host=<host>
             dbname=<database_name>
             user=<database_user_name>
             password=<password>
             port=<port>
       """)

       cur = conn.cursor()
       cur.execute('SELECT 40+2')

       print(cur.fetchone())

       cur.close()
       conn.close()
       ```

       Specify:

       * `<host>` — node DNS address or [public IP address](/managed-databases/timescaledb/public-ip.mdx) (Floating IP);
       * `<database_name>` — database name;
       * `<database_user_name>` — database user name;
       * `<password>` — user password;
       * `<port>` — [connection port](#connection-ports).
  </TabItem>

  <TabItem value="php">
    <TabItemLabel>
      PHP
    </TabItemLabel>

    1. Install the pgsql library:

       ```bash
       apt install php-pgsql
       ```

    2. Use the following connection example:

       ```php
       <?php

       $dbconn = pg_connect("
             host=<host>
             dbname=<database_name>
             user=<database_user_name>
             password=<password>
             port=<port>
       ");

       $query = 'SELECT 40 + 2';
       $result = pg_query($query);
       $row = pg_fetch_row($result);

       echo $row[0];

       pg_close($dbconn);
       ?>
       ```

       Specify:

       * `<host>` — node DNS address or [public IP address](/managed-databases/timescaledb/public-ip.mdx) (Floating IP);
       * `<database_name>` — database name;
       * `<database_user_name>` — database user name;
       * `<password>` — user password;
       * `<port>` — [connection port](#connection-ports).
  </TabItem>

  <TabItem value="go">
    <TabItemLabel>
      Go
    </TabItemLabel>

    Use the following connection example:

    ```go
    package main

    import (
        "context"
        "fmt"
        "os"

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

    func main() {
        connectionString := fmt.Sprintf("postgres://%s:%s@%s:<port>/%s",
            "<database_user_name>",
            "<password>",
            "<host>",
            "<database_name>",
        )

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

        var sum int64
        err = conn.QueryRow(context.Background(), "SELECT 40+2").Scan(&sum)
        if err != nil {
            fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
            os.Exit(1)
        }

        fmt.Println(sum)
    }
    ```

    Specify:

    * `<port>` — [connection port](#connection-ports);
    * `<database_user_name>` — database user name;
    * `<password>` — user password;
    * `<host>` — node DNS address or [public IP address](/managed-databases/timescaledb/public-ip.mdx) (Floating IP);
    * `<database_name>` — database name.
  </TabItem>

  <TabItem value="nodejs">
    <TabItemLabel>
      Node.js
    </TabItemLabel>

    1. Install the pg library:

       ```bash
       npm install pg
       ```

    2. Use the following connection example:

       ```js
       const pg = require('pg');

       const config = {
           host: '<host>',
           port: '<port>',
           database: '<database_name>',
           user: '<database_user_name>',
           password: '<password>',
       };

       const client = new pg.Client(config);

       client.connect((error) => {
           if (error) throw error;
       });

       client.query('SELECT 40 + 2 AS sum', (error, res) => {
           if (error) throw error;
           console.log(res.rows);
           client.end();
       });
       ```

       Specify:

       * `<host>` — node DNS address or [public IP address](/managed-databases/timescaledb/public-ip.mdx) (Floating IP);
       * `<port>` — [connection port](#connection-ports);
       * `<database_name>` — database name;
       * `<database_user_name>` — database user name;
       * `<password>` — user password.
  </TabItem>
</Tabs>

<Formbricks />
