Python example

This Python example uses the psycopg2 library for connecting to Timescale Cloud PostgreSQL.

from psycopg2.extras import RealDictCursor
import psycopg2

uri = "postgres://tsdbadmin:[email protected]:20985/defaultdb?sslmode=require"

db_conn = psycopg2.connect(uri)
c = db_conn.cursor(cursor_factory=RealDictCursor)

c.execute("SELECT 1 = 1")
result = c.fetchone()

Node.js example

This example uses the node-postgres library.

var pg = require('pg');

var config = {
    database: "defaultdb",
    host: "pg-3b8d4ed6-myfirstcloudhub.timescaledb.io",
    password: "nr0dfnswz36xs9pi",
    port: 20985,
    ssl: "require",
    user: "tsdbadmin",
};

var client = new pg.Client(config);

client.connect(function (err) {
    if (err)
        throw err;
    client.query('SELECT 1 AS value', [], function (err, result) {
        if (err)
            throw err;

        console.log(result.rows[0]);
        client.end(function (err) {
            if (err)
                throw err;
        });
    });
});

This example uses the Sequelize library.

Prerequisites:

  • pg-native package installed.
  • Service CA certificate, which you can download from the Timescale Cloud console's service page by clicking the "view CA certificate" button.
const Sequelize = require('sequelize');

const database = 'defaultdb';
const username = 'tsdbadmin';
const password = 'nr0dfnswz36xs9pi';
const host = 'pg-3b8d4ed6-myfirstcloudhub.timescaledb.io';
const port = 20985;
const sslrootcert = '/path/to/ca.pem';

const sequelize = new Sequelize(database, username, password, {
  host,
  port,
  dialect: 'postgres',
  native: true,
  ssl: true,
  dialectOptions: {
    ssl: {
      sslmode: 'verify-ca',
      sslrootcert
    }
  },
});

sequelize.query('SELECT 1 AS value')
  .then(([results, metadata]) => console.log(results));

C# example

This C# example uses the Npgsql driver for connecting to Timescale Cloud PostgreSQL. 

using Npgsql;
using System;

namespace tsdb
{
    class Program
    {
        static void Main(string[] args)
        {
            NpgsqlConnection conn = new NpgsqlConnection("Server=pg-3b8d4ed6-myfirstcloudhub.timescaledb.io;User Id=tsdbadmin;Password=nr0dfnswz36xs9pi;Database=defaultdb;Port=20985;SSLMode=Require;Trust Server Certificate=true");
            conn.Open();

            // This will return 1333332, can be any query you wish
            NpgsqlCommand command = new NpgsqlCommand("SELECT 666666*2::BIGINT", conn);
            Int64 count = (Int64)command.ExecuteScalar();

            Console.Write("{0}\n", count);
            conn.Close();
        }
    }
}

Java example

You'll need to download the service CA certificate from the Timescale Cloud console's service page by clicking the "view CA certificate" button.

package pg;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public final class Connect {
  public static void main(String[] args) {
    Properties props = new Properties();
    props.put("jdbc.url", "jdbc:postgresql://pg-3b8d4ed6-myfirstcloudhub.timescaledb.io:20985/defaultdb");
    props.put("user", "tsdbadmin");
    props.put("password", "nr0dfnswz36xs9pi");
    props.put("ssl", "true");
    props.put("sslmode", "verify-ca");
    props.put("sslrootcert", "/path/to/ca.pem");

    try {
      Connection c = DriverManager.getConnection(props.getProperty("jdbc.url"), props);
      System.out.println("Success");
      c.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Maven dependency for PostgreSQL JDBC driver (You can also download the latest driver from postgresql.org):

    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>42.2.0</version>
    </dependency>

AWS Elastic Beanstalk

When using an AWS Elastic Beanstalk application to connect & use a TimescaleDB service hosted in Timescale Cloud, you will need to make the database connection private key for the SSL certificate available to your application. AWS provides a way to store private keys securely in AWS S3 and make those keys available to your application during deployment. Refer to the AWS Elastic Beanstalk documentation for more information.

Did this answer your question?