Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go postgres connection SSL not enabled

Tags:

postgresql

ssl

go

I'm trying to connect to my localhost postgresql server without SSL and I'm getting this error:

pq: SSL is not enabled on the server

That's fine, I know how to fix it:

type App struct {
    Router *mux.Router
    DB     *sql.DB
}

func (a *App) Initialize(dbname string) {
    connectionString := fmt.Sprintf("dbname=%s sslmode=disable", dbname)
    var err error
    a.DB, err = sql.Open("postgres", connectionString)
    if err != nil {
        log.Fatal(err)
    }

    defer a.DB.Close()
}

However I'm still getting the error!

like image 966
Godzilla74 Avatar asked Aug 08 '17 14:08

Godzilla74


People also ask

How do I connect to PostgreSQL SSL?

With SSL support compiled in, the PostgreSQL server can be started with SSL enabled by setting the parameter ssl to on in postgresql. conf. The server will listen for both normal and SSL connections on the same TCP port, and will negotiate with any connecting client on whether to use SSL .

Could not connect to database PQ SSL is not enabled on the server?

pq: SSL is not enabled on the server By default, sslmode is set to required with lib/pq , so you need to actually specify another setting to fix this. Update your connection string to include sslmode=disable and you should be back in action.


1 Answers

I was able to recreate your error with a fresh install of postgres. While the error output was

pq: SSL is not enabled on the server

the real error was not having any databases created. To create a testdb let's run

createdb testdb

in your terminal with postgres already running in the background.

like image 178
cogell Avatar answered Oct 22 '22 10:10

cogell