Search code examples
postgresqlsslgo

Go postgres connection SSL not enabled


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!


Solution

  • 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.