【问题标题】:Go postgres connection SSL not enabledGo postgres 连接 SSL 未启用
【发布时间】:2017-08-08 14:49:41
【问题描述】:

我正在尝试在不使用 SSL 的情况下连接到我的本地主机 postgresql 服务器,但出现此错误:

pq: SSL is not enabled on the server

没关系,我知道怎么解决:

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()
}

但是我仍然收到错误消息!

【问题讨论】:

  • 还有其他语法可以使用golang连接到postgres db,你有没有试过其他方法?
  • @zenwraight 是的,我最终使用了 URL 字符串版本来让最初的东西工作,但我需要能够动态更改数据库名称以进行测试 a.DB, err = sql.Open("postgres", "postgresql://localhost:5432/bookstore?sslmode=disable")跨度>
  • 是的,我只建议这个,因为我也遇到了同样的错误,然后最终只使用了上面的字符串
  • go 代码对我来说看起来是正确的。我也有类似的代码,但无法复制您的问题。只是想知道您使用的是哪个 postgres 版本?我在 9.3 上尝试过

标签: postgresql ssl go


【解决方案1】:

我能够通过全新安装的 postgres 重新创建您的错误。虽然错误输出是

pq: SSL is not enabled on the server

真正的错误是没有创建任何数据库。要创建一个 testdb,让我们运行

createdb testdb

在您的终端中,postgres 已经在后台运行。

【讨论】:

    【解决方案2】:

    我偶然发现这个尝试从我的 Go 程序连接到 Postgresql 服务器,但密码错误。

    【讨论】:

      猜你喜欢
      • 2015-01-27
      • 2016-12-27
      • 1970-01-01
      • 2015-02-27
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多