The Vertica SQL Driver for Go

Posted July 11, 2019 by Roger Huebner, Distinguished Engineer

Go programming language logo and little blue beaver mascot

We are happy to announce the alpha release of the Vertica SQL Driver for Go. Go, also called GoLang, is a statically-typed, high performance language, syntactically similar to C. Originating at Google, it has grown rapidly in popularity over the past decade and has become a language of choice for highspeed components. The Vertica SQL Driver for Go was created to provide native Vertica support for integration with the many components and projects that require GoLang support.

Feel free to download the code from, run the tests and try out the sample application. If you want to contribute some fixes of your own, please follow the instructions in the file, after which, the pull request will be gratefully received.

Now a few caveats:

  • The driver supports most functionality across most versions of Vertica (generally Vertica 8.0 forward), although some features will be unavailable when connecting to much older versions.
  • On the other hand, features are being constantly added (e.g., SSL mutual authentication). Please watch for future updates.
  • We also reserve the right to introduce breaking changes until the driver is out of beta. Of course, we will attempt to avoid such things, but please be prepared.
Go and SQL
Go has SQL database support built in as part of its standard distribution. This is convenient as common constructs such as connections, tables, rows, and have a standard representation. As with JDBC this allows developers to deal with these high-level concepts and defers the actual implementation to the drivers themselves.

This blog entry is not intended to be a tutorial on Go SQL itself, although due to the nature of Go’s SQL support, these instructions are generally usable for most endpoints. For further information, please refer to Go’s SQL package itself:

Below is a quick step-by-step walkthrough of a sample code to get you up and running. We assume you have a Vertica set up and have a fundamental understanding of Go.

Sample Code
Let’s start by declaring our main package.
package main Then we can import a few packages to get us started. The driver includes its own rudimentary logger and we will use it in this example. It is important that we include the vertica-sql-go library as this will cause it to register itself with the Go SQL package.
import ( "context" "database/sql" "os" _ "" "" ) Declare our main() function and create a background context to use throughout the example.
func main() { logger.SetLogLevel(logger.INFO) var testLogger = logger.New("samplecode") ctx := context.Background() Now the important bit, attempt a connection to our database. The form used in the connection string will be “vertica://(user):(password)@(host):(port) /(database)”.

There are a few other options that can be included, related to SSL and statement preparation, but we’ll assume an unencrypted connection for this demonstration.
connDB, err := sql.Open("vertica", "vertica://dbadmin:@localhost:5433/dbadmin") if err != nil { testLogger.Fatal(err.Error()) os.Exit(1) } defer connDB.Close() It should be noted that any error that has occurred by now will most likely be related to the connection string itself as the framework will make a lazy connection to the database.

By calling Ping()/PingContext(), we can force the framework to attempt the actual connection and authentication.
if err = connDB.PingContext(ctx); err != nil { testLogger.Fatal(err.Error()) os.Exit(1) } rows, err := connDB.QueryContext(ctx, "SELECT * FROM v_monitor.cpu_usage LIMIT 5") if err != nil { testLogger.Fatal(err.Error()) os.Exit(1) } defer rows.Close() We now have a connection to the database. Let’s start using it. We will do a basic query against a standard Vertica table that records the CPU usage over time.

It’s important to close your result cursor when you’re done with it.
for rows.Next() { var nodeName string var startTime string var endTime string var avgCPU float64 if err = rows.Scan(&nodeName, &startTime, &endTime, &avgCPU); err != nil { testLogger.Fatal(err.Error()) os.Exit(1) } testLogger.Info("%s\t%s\t%s\t%f", nodeName, startTime, endTime, avgCPU) } We’ve successfully run the test. Let’s log it and exit gracefully.
testLogger.Info("Test complete") os.Exit(0) }
The Vertica SQL Driver for Go provides a Vertica-native implementation of Go’s SQL contract. Using standard Go interfaces, developers and systems integrators will be able to conveniently and consistently access their Vertica data sources.

We encourage everyone interested to “star” this project on github, file issues and contribute.