Ssl Is Not Enabled on the Server

Ssl Is Not Enabled on the Server

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.

3

7 Answers

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

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

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.

1

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.

1

To establish a connection without SSL, try

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

This is how I got it working:

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

when you are using sqlx for connection to postgres try this one:

import (
    ""
    _ ""
)

func main () {
  // c is my configuration struct use your own struct instead
  databseUrl := fmt.Sprintf(
        "postgres://%s:%s@%s:%s/%s?sslmode=disable",
        c.Postgres.User, c.Postgres.Password, c.Postgres.Host, c.Postgres.Port, c.Postgres.Name,
  )

  db, _ := sqlx.Open("postgres", databaseUrl)

}

You can use a Struct to pass the values or load it from Env or Config file.

Here is the solution,

package main

import (
    "database/sql"
    "fmt"
    _ ""
    "log"
)

type Config struct {
    DBDriver      string
    DBSource      string
    ServerAddress string
}

func main() {

    db, err := openDB()
    if err != nil {
        log.Printf("Databse Connection Error", err)
    }

    err = db.Ping()
    fmt.Println("Database Conn opend")

    if err != nil {
        log.Printf("Error while Pinging Database")
    }

}

func openDB() (*sql.DB, error) {

    // Here you can populate the DB params from Env or Config file or use marshaling to fill in the values
    dbParams := Config{
        DBDriver:      "postgres",
        DBSource:      "postgres://usename:password@localhost:5432/DatabaseName?sslmode=disable",
        ServerAddress: "",
    }

    db, err := sql.Open(dbParams.DBDriver, dbParams.DBSource)

    if err != nil {
        fmt.Println("error", err)
        return nil, err
    }

    return db, nil
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.