View errors by: Date Added Package/Type

pq: SSL is not enabled on the server

pq.ErrSSLNotSupported "pq: SSL is not enabled on the server" package documentation: github.com/lib/pq

What causes the error?

The pq.ErrSSLNotSupported error typically occurs in development when you try to connect to a database without specifying, or with an invalid setting for, the sslmode option in your connection string.

Let’s look at an example. Suppose you are using the lib/pq driver and you create your connection string.

connStr := "host=localhost port=5432 user=pquser dbname=some_db"
db, err := sql.Open("postgres", connStr)
if err != nil {
  // Your code errors here!
	log.Fatal(err)
}

The string we see here doesn’t include a setting for sslmode, so by default it will be set to require ssl, which most development PostgreSQL databases aren’t using.

From the docs:

* sslmode - Whether or not to use SSL (default is require, this is not
  the default for libpq)

How can I fix it?

The simplest fix is usually to just set sslmode to disable which tells the lib/pq package that you won’t be using SSL when connecting to your PostgreSQL database.

// The last bit on this string is what needs changed
connStr := "host=localhost port=5432 user=pquser dbname=some_db sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
	log.Fatal(err)
}

If you need to turn sslmode on or off depending on your environment, you can set it in a config of some sort and then build your connection string dynamically.

connStr := "host=%s port=%s user=%s dbname=%s sslmode=%s"
// Set each value dynamically w/ Sprintf
connStr = fmt.Sprintf(connStr, host, port, user, dbname, sslmode)

Created by Jon Calhoun, but you can check out the source and contribute on GitHub