SSL is not enabled on the server

Go

Go Problem Overview


Trying to communicate with a postgres database with go, preparing the statement like this:

var stmt *sql.Stmt
var err error

stmt, err = db.Prepare(selectStatement)
if err != nil {
    fmt.Printf("db.Prepare error: %v\n",err)
    return err
}

Throws the following error:

db.Prepare error: pq: SSL is not enabled on the server

Any solution ?

I can add more information, if needed.

Go Solutions


Solution 1 - Go

You should establish DB connection without SSL encryption, like that:

db, err := sql.Open("postgres", "user=test password=test dbname=test sslmode=disable") 

Solution 2 - Go

If your data source name is a url, you will do it like this:

db, err := sql.Open("postgres", "postgres://username:password@localhost/db_name?sslmode=disable")

sslmode is just added to the db url like a query parameter.

Solution 3 - Go

Notice, please:

This even occurs, if you have indicated a sslmode=disable, but have empty other param. For example dbname=

For example, connection string:

user=test password=test dbname=sslmode=disable will also issue this error, because dbname is empty.

Solution 4 - Go

To establish a connection without SSL, try

postgres://username:password@host:5432/database?sslmode=disable

Solution 5 - Go

This is how I got it working:

db, err := sql.Open("postgres", "postgres://{user}:{password}@{hostname}:{port}/{database-name}?sslmode=disable")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGustavo Semião-LoboView Question on Stackoverflow
Solution 1 - GoKavuView Answer on Stackoverflow
Solution 2 - GoeatonphilView Answer on Stackoverflow
Solution 3 - GofropsView Answer on Stackoverflow
Solution 4 - GoHarald NordgrenView Answer on Stackoverflow
Solution 5 - GogildniyView Answer on Stackoverflow