Updated on 2025-05-29 GMT+08:00

Connecting to a Database (Using SSL)

The Go driver supports SSL connections to the database. After the SSL mode is enabled, if the Go driver connects to the database server in SSL mode, the Go driver uses the standard TLS 1.3 protocol by default, and the TLS version must be 1.2 or later. This section describes how applications configure the client in SSL mode through the Go driver.

In SSL-based certificate authentication mode, you do not need to specify the user password in the connection string.

Configuring the Client

Before performing the steps below, log in to the GaussDB management console. On the Instances page, click the instance name to go to the Basic Information page. On the displayed page, click under SSL to download the root certificate or certificate bundle, and save the ca.pem root certificate to the client.

Example:
package main
// Set the dependency package using its path from the environment.
import (
 "database/sql"
 "fmt"
 _ [Go_driver_module_name] /* Replace it with the actual Go driver module name. */
 "log"
)
// One-way authentication is used as an example. In this example, the username and password are stored in environment variables. Before running this example, set environment variables in the local environment as required.
func main() {
 hostip := os.Getenv("GOHOSTIP")   // GOHOSTIP indicates the IP address written into the environment variable.
 port := os.Getenv("GOPORT")       // GOPORT indicates the port number written into the environment variable.
 usrname := os.Getenv("GOUSRNAME") // GOUSRNAME indicates the username written into the environment variable.
 passwd := os.Getenv("GOPASSWD")   // GOPASSWDW indicates the user password written into the environment variable.
 sslpasswd := os.Getenv("GOSSLPASSWD") // GOSSLPASSWD indicates the passphrase written into the environment variable.
 dsnStr := "host=" + hostip + " port=" + port + " user=" + usrname + " password=" + passwd + " dbname=gaussdb"
 parameters := []string{
  " sslmode=verify-ca sslrootcert=certs/ca.pem",
 }
 for _, param := range parameters {
  db, err := sql.Open("gaussdb", dsnStr+param)
  if err != nil {
   log.Fatal(err)
  }
  var f1 int
  err = db.QueryRow("select 1").Scan(&f1)
  if err != nil {
   log.Fatal(err)
		} else {
   fmt.Printf("RESULT: select 1: %d\n", f1)
  }
  db.Close()
 }
}