服务器上未启用SSL。

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

SSL is not enabled on the server

问题

尝试使用Go语言与PostgreSQL数据库进行通信,准备语句的代码如下:

  1. var stmt *sql.Stmt
  2. var err error
  3. stmt, err = db.Prepare(selectStatement)
  4. if err != nil {
  5. fmt.Printf("db.Prepare error: %v\n", err)
  6. return err
  7. }

出现以下错误:

  1. db.Prepare error: pq: SSL is not enabled on the server

有什么解决方案吗?

如果需要,我可以提供更多信息。

英文:

Trying to communicate with a postgres database with go, preparing the statement like this:

  1. var stmt *sql.Stmt
  2. var err error
  3. stmt, err = db.Prepare(selectStatement)
  4. if err != nil {
  5. fmt.Printf("db.Prepare error: %v\n",err)
  6. return err
  7. }

Throws the following error:

  1. db.Prepare error: pq: SSL is not enabled on the server

Any solution ?

I can add more information, if needed.

答案1

得分: 257

你应该建立没有 SSL 加密的数据库连接,像这样:

  1. db, err := sql.Open("postgres", "user=test password=test dbname=test sslmode=disable")
英文:

You should establish DB connection without SSL encryption, like that:

  1. db, err := sql.Open("postgres", "user=test password=test dbname=test sslmode=disable")

答案2

得分: 176

如果你的数据源名称是一个URL,你可以这样做:

  1. db, err := sql.Open("postgres", "postgres://username:password@localhost/db_name?sslmode=disable")

sslmode只是作为查询参数添加到数据库URL中。

英文:

If your data source name is a url, you will do it like this:

  1. db, err := sql.Open("postgres", "postgres://username:password@localhost/db_name?sslmode=disable")

sslmode is just added to the db url like a query parameter.

答案3

得分: 35

请注意:

即使您已经指定了sslmode=disable,但如果其他参数为空,也会发生此错误。例如,dbname=

例如,连接字符串:

user=test password=test dbname=sslmode=disable也会引发此错误,因为dbname为空。

英文:

Notice, please:

This even occurs, if you have indicated a sslmode=disable, but have empty other param. For example dbname=

For example, connection string:

user=test password=test dbname=sslmode=disable will also issue this error, because dbname is empty.

答案4

得分: 27

要在没有SSL的情况下建立连接,请尝试以下代码:

  1. <!-- language: lang-none -->
  2. postgres://username:password@host:5432/database?sslmode=disable
英文:

To establish a connection without SSL, try

<!-- language: lang-none -->

  1. postgres://username:password@host:5432/database?sslmode=disable

答案5

得分: 22

这是我让它工作的方法:

  1. db, err := sql.Open("postgres", "postgres://{user}:{password}@{hostname}:{port}/{database-name}?sslmode=disable")
英文:

This is how I got it working:

  1. db, err := sql.Open(&quot;postgres&quot;, &quot;postgres://{user}:{password}@{hostname}:{port}/{database-name}?sslmode=disable&quot;)

答案6

得分: 2

当你使用sqlx连接到postgres时,可以尝试以下代码:

  1. import (
  2. "github.com/jmoiron/sqlx"
  3. _ "github.com/lib/pq"
  4. )
  5. func main () {
  6. // c是我的配置结构体,请使用你自己的结构体
  7. databaseUrl := fmt.Sprintf(
  8. "postgres://%s:%s@%s:%s/%s?sslmode=disable",
  9. c.Postgres.User, c.Postgres.Password, c.Postgres.Host, c.Postgres.Port, c.Postgres.Name,
  10. )
  11. db, _ := sqlx.Open("postgres", databaseUrl)
  12. }
英文:

when you are using sqlx for connection to postgres try this one:

  1. import (
  2. &quot;github.com/jmoiron/sqlx&quot;
  3. _ &quot;github.com/lib/pq&quot;
  4. )
  5. func main () {
  6. // c is my configuration struct use your own struct instead
  7. databseUrl := fmt.Sprintf(
  8. &quot;postgres://%s:%s@%s:%s/%s?sslmode=disable&quot;,
  9. c.Postgres.User, c.Postgres.Password, c.Postgres.Host, c.Postgres.Port, c.Postgres.Name,
  10. )
  11. db, _ := sqlx.Open(&quot;postgres&quot;, databaseUrl)
  12. }

答案7

得分: 0

你可以使用结构体(Struct)来传递值,或者从环境变量或配置文件中加载值。

以下是解决方案:

  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. _ "github.com/lib/pq"
  6. "log"
  7. )
  8. type Config struct {
  9. DBDriver string
  10. DBSource string
  11. ServerAddress string
  12. }
  13. func main() {
  14. db, err := openDB()
  15. if err != nil {
  16. log.Printf("Databse Connection Error", err)
  17. }
  18. err = db.Ping()
  19. fmt.Println("Database Conn opend")
  20. if err != nil {
  21. log.Printf("Error while Pinging Database")
  22. }
  23. }
  24. func openDB() (*sql.DB, error) {
  25. // Here you can populate the DB params from Env or Config file or use marshaling to fill in the values
  26. dbParams := Config{
  27. DBDriver: "postgres",
  28. DBSource: "postgres://usename:password@localhost:5432/DatabaseName?sslmode=disable",
  29. ServerAddress: "",
  30. }
  31. db, err := sql.Open(dbParams.DBDriver, dbParams.DBSource)
  32. if err != nil {
  33. fmt.Println("error", err)
  34. return nil, err
  35. }
  36. return db, nil
  37. }

希望对你有帮助!

英文:

You can use a Struct to pass the values or load it from Env or Config file.

Here is the solution,

  1. package main
  2. import (
  3. &quot;database/sql&quot;
  4. &quot;fmt&quot;
  5. _ &quot;github.com/lib/pq&quot;
  6. &quot;log&quot;
  7. )
  8. type Config struct {
  9. DBDriver string
  10. DBSource string
  11. ServerAddress string
  12. }
  13. func main() {
  14. db, err := openDB()
  15. if err != nil {
  16. log.Printf(&quot;Databse Connection Error&quot;, err)
  17. }
  18. err = db.Ping()
  19. fmt.Println(&quot;Database Conn opend&quot;)
  20. if err != nil {
  21. log.Printf(&quot;Error while Pinging Database&quot;)
  22. }
  23. }
  24. func openDB() (*sql.DB, error) {
  25. // Here you can populate the DB params from Env or Config file or use marshaling to fill in the values
  26. dbParams := Config{
  27. DBDriver: &quot;postgres&quot;,
  28. DBSource: &quot;postgres://usename:password@localhost:5432/DatabaseName?sslmode=disable&quot;,
  29. ServerAddress: &quot;&quot;,
  30. }
  31. db, err := sql.Open(dbParams.DBDriver, dbParams.DBSource)
  32. if err != nil {
  33. fmt.Println(&quot;error&quot;, err)
  34. return nil, err
  35. }
  36. return db, nil
  37. }

huangapple
  • 本文由 发表于 2014年2月23日 03:13:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/21959148.html
匿名

发表评论

匿名网友

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

确定