pq: 服务器上的Postrsql golang未启用SSL。

huangapple go评论91阅读模式
英文:

pq: SSL is not enabled on the server Postrsql golang

问题

我想用Golang连接PostgreSQL数据库。

代码:

import (
	"context"
	"fmt"
	"time"

	_ "github.com/bmizerany/pq"
	"github.com/jmoiron/sqlx"
)

func NewPostSql() (*sql.DB, error) {
	db, err := sql.Open("postgres", "postgres://postgres:ellez2004@localhost:5432/app?sslmode=disable")
	if err != nil {
		return nil, err
	}
	if err := db.Ping(); err != nil {
		fmt.Println("error:", err.Error())
	}
	return db, nil
}

但是我得到了错误,即使设置了sslmode=disable
错误信息:
pq: SSL is not enabled on the server

我该如何解决这个问题?

英文:

I want connect Postresql database with golang

Code :

  import (
	"context"
	"fmt"
	"time"

	_ "github.com/bmizerany/pq"
	"github.com/jmoiron/sqlx"
    )

  func NewPostSql()(*sql.DB,error) { 
	db, err := sql.Open("postgres", "postgres://postgres:ellez2004@localhost:5432/app?sslmode=disable")
	if err != nil { 
		return nil,err;
	}
	if err := db.Ping();err != nil {
		fmt.Println("error: ", err.Error());
	}
	return db,nil;
}

But i get error even though ssl = disable:
Error:

pq: SSL is not enabled on the server

How can I fix this??

答案1

得分: 2

我发现问题在于 "github.com/bmizerany/pq" 包无法正常工作。只需要安装 "github.com/lib/pq" 包即可。

import (
    "context"
    "fmt"
    "time"

    _ "github.com/lib/pq"
    "github.com/jmoiron/sqlx"
)

func NewPostSql() (*sql.DB, error) {
    db, err := sql.Open("postgres", "postgres://postgres:ellez2004@localhost:5432/app?sslmode=disable")
    if err != nil {
        return nil, err
    }
    if err := db.Ping(); err != nil {
        fmt.Println("error: ", err.Error())
    }
    return db, nil
}
英文:

I find problem "github.com/bmizerany/pq" package not workin.Just need
install
"github.com/lib/pq"

  import (
    "context"
    "fmt"
    "time"

    _ "github.com/lib/pq"
    "github.com/jmoiron/sqlx"
    )

  func NewPostSql()(*sql.DB,error) { 
    db, err := sql.Open("postgres", "postgres://postgres:ellez2004@localhost:5432/app?sslmode=disable")
    if err != nil { 
        return nil,err;
    }
    if err := db.Ping();err != nil {
        fmt.Println("error: ", err.Error());
    }
    return db,nil;
}

答案2

得分: -3

func NewPostSql() (*sql.DB, error) {
db, err := sql.Open("postgres", "postgres://ellez2004@localhost:5432/app?sslmode=disable")
if err != nil {
return nil, err
}
if err := db.Ping(); err != nil {
fmt.Println("error: ", err.Error())
}
return db, nil
}

你不应该两次使用 postgres

英文:
func NewPostSql() (*sql.DB, error) {
	db, err := sql.Open("postgres", "postgres://ellez2004@localhost:5432/app?sslmode=disable")
	if err != nil {
		return nil, err
	}
	if err := db.Ping(); err != nil {
		fmt.Println("error: ", err.Error())
	}
	return db, nil
}

You should not use postgres twice.

huangapple
  • 本文由 发表于 2022年1月20日 19:46:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/70785679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定