titledescription
Connect
This page has instructions demonstrating how to connect to QuestDB from NodeJS, Java or Go. The examples show the Postgres and InfluxDB integrations.

This page shows how to connect to QuestDB using different programming languages or tools.

Prerequisites

Make sure you have QuestDB running and accessible, you can do so from Docker, the binaries or Homebrew for macOS users.

Postgres compatibility

You can query data using the Postgres endpoint that QuestDB exposes. This is accessible via port 8812.

  1. const { Client } = require("pg")
  2. const start = async () => {
  3. const client = new Client({
  4. database: "qdb",
  5. host: "127.0.0.1",
  6. password: "quest",
  7. port: 8812,
  8. user: "admin",
  9. })
  10. await client.connect()
  11. console.log("Connected")
  12. }
  13. start()
  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "log"
  6. _ "github.com/lib/pq"
  7. )
  8. const (
  9. host = "localhost"
  10. port = 8812
  11. user = "admin"
  12. password = "quest"
  13. dbname = "qdb"
  14. )
  15. func main() {
  16. connStr := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
  17. db, err := sql.Open("postgres", connStr)
  18. if err != nil {
  19. panic(err)
  20. }
  21. defer db.Close()
  22. log.Println("Connected")
  23. }
  1. # With the client already installed locally
  2. psql -h localhost -p 8812 -U admin -d qdb
  3. # OR, with Docker (does not require locally installed client)
  4. docker run -it --rm --network=host -e PGPASSWORD=quest postgres psql -h localhost -p 8812 -U admin -d qdb
  1. package com.myco;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.util.Properties;
  6. public class ConnectExample {
  7. public static void main(String[] args) throws SQLException {
  8. Properties properties = new Properties();
  9. properties.setProperty("user", "admin");
  10. properties.setProperty("password", "quest");
  11. properties.setProperty("sslmode", "disable");
  12. final Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:8812/qdb", properties);
  13. System.out.println("Connected");
  14. connection.close();
  15. }
  16. }
  1. // compile with
  2. // g++ libpq_example.c -o libpq_example.exe -I pgsql\include -L dev\pgsql\lib -std=c++17 -lpthread -lpq
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <libpq-fe.h>
  6. void do_exit(PGconn *conn) {
  7. PQfinish(conn);
  8. exit(1);
  9. }
  10. int main() {
  11. PGconn *conn = PQconnectdb("host=localhost user=admin password=quest port=8812 dbname=testdb");
  12. if (PQstatus(conn) == CONNECTION_BAD) {
  13. fprintf(stderr, "Connection to database failed: %s\n",
  14. PQerrorMessage(conn));
  15. do_exit(conn);
  16. }
  17. PQfinish(conn);
  18. return 0;
  19. }
  1. import psycopg2
  2. try:
  3. connection = psycopg2.connect(user="admin",
  4. password="quest",
  5. host="127.0.0.1",
  6. port="8812",
  7. database="qdb")
  8. cursor = connection.cursor()
  9. # Print PostgreSQL Connection properties
  10. print(connection.get_dsn_parameters(), "\n")
  11. # Print PostgreSQL version
  12. cursor.execute("SELECT version();")
  13. record = cursor.fetchone()
  14. print("You are connected to - ", record, "\n")
  15. except (Exception, psycopg2.Error) as error:
  16. print("Error while connecting to PostgreSQL", error)
  17. finally:
  18. #closing database connection.
  19. if (connection):
  20. cursor.close()
  21. connection.close()
  22. print("PostgreSQL connection is closed")
  1. using Npgsql;
  2. string username = "admin";
  3. string password = "quest";
  4. string database = "qdb";
  5. int port = 8812;
  6. var connectionString = $"host=localhost;port={port};username={username};password={password};
  7. database={database};ServerCompatibilityMode=NoTypeLoading;";
  8. await using NpgsqlConnection connection = new(connectionString);
  9. await connection.OpenAsync();

InfluxDB line protocol

QuestDB implements the InfluxDB line protocol, this endpoint is accessible on port 9009.

  1. const net = require("net")
  2. const client = new net.Socket()
  3. const HOST = "localhost"
  4. const PORT = 9009
  5. function run() {
  6. client.connect(PORT, HOST, () => {
  7. console.log("Connected")
  8. })
  9. }
  10. run()
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "net"
  7. "time"
  8. )
  9. func main() {
  10. host := "127.0.0.1:9009"
  11. tcpAddr, err := net.ResolveTCPAddr("tcp4", host)
  12. checkErr(err)
  13. conn, err := net.DialTCP("tcp", nil, tcpAddr)
  14. checkErr(err)
  15. log.Println("Connected")
  16. defer conn.Close()
  17. }
  18. func checkErr(err error) {
  19. if err != nil {
  20. panic(err)
  21. }
  22. }