英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论