英文:
SSL is not enabled on the server
问题
尝试使用Go语言与PostgreSQL数据库进行通信,准备语句的代码如下:
var stmt *sql.Stmt
var err error
stmt, err = db.Prepare(selectStatement)
if err != nil {
fmt.Printf("db.Prepare error: %v\n", err)
return err
}
出现以下错误:
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:
var stmt *sql.Stmt
var err error
stmt, err = db.Prepare(selectStatement)
if err != nil {
fmt.Printf("db.Prepare error: %v\n",err)
return err
}
Throws the following error:
db.Prepare error: pq: SSL is not enabled on the server
Any solution ?
I can add more information, if needed.
答案1
得分: 257
你应该建立没有 SSL 加密的数据库连接,像这样:
db, err := sql.Open("postgres", "user=test password=test dbname=test sslmode=disable")
英文:
You should establish DB connection without SSL encryption, like that:
db, err := sql.Open("postgres", "user=test password=test dbname=test sslmode=disable")
答案2
得分: 176
如果你的数据源名称是一个URL,你可以这样做:
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:
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的情况下建立连接,请尝试以下代码:
<!-- language: lang-none -->
postgres://username:password@host:5432/database?sslmode=disable
英文:
To establish a connection without SSL, try
<!-- language: lang-none -->
postgres://username:password@host:5432/database?sslmode=disable
答案5
得分: 22
这是我让它工作的方法:
db, err := sql.Open("postgres", "postgres://{user}:{password}@{hostname}:{port}/{database-name}?sslmode=disable")
英文:
This is how I got it working:
db, err := sql.Open("postgres", "postgres://{user}:{password}@{hostname}:{port}/{database-name}?sslmode=disable")
答案6
得分: 2
当你使用sqlx
连接到postgres
时,可以尝试以下代码:
import (
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
func main () {
// c是我的配置结构体,请使用你自己的结构体
databaseUrl := fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s?sslmode=disable",
c.Postgres.User, c.Postgres.Password, c.Postgres.Host, c.Postgres.Port, c.Postgres.Name,
)
db, _ := sqlx.Open("postgres", databaseUrl)
}
英文:
when you are using sqlx
for connection to postgres
try this one:
import (
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
func main () {
// c is my configuration struct use your own struct instead
databseUrl := fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s?sslmode=disable",
c.Postgres.User, c.Postgres.Password, c.Postgres.Host, c.Postgres.Port, c.Postgres.Name,
)
db, _ := sqlx.Open("postgres", databaseUrl)
}
答案7
得分: 0
你可以使用结构体(Struct)来传递值,或者从环境变量或配置文件中加载值。
以下是解决方案:
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
"log"
)
type Config struct {
DBDriver string
DBSource string
ServerAddress string
}
func main() {
db, err := openDB()
if err != nil {
log.Printf("Databse Connection Error", err)
}
err = db.Ping()
fmt.Println("Database Conn opend")
if err != nil {
log.Printf("Error while Pinging Database")
}
}
func openDB() (*sql.DB, error) {
// Here you can populate the DB params from Env or Config file or use marshaling to fill in the values
dbParams := Config{
DBDriver: "postgres",
DBSource: "postgres://usename:password@localhost:5432/DatabaseName?sslmode=disable",
ServerAddress: "",
}
db, err := sql.Open(dbParams.DBDriver, dbParams.DBSource)
if err != nil {
fmt.Println("error", err)
return nil, err
}
return db, nil
}
希望对你有帮助!
英文:
You can use a Struct to pass the values or load it from Env or Config file.
Here is the solution,
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
"log"
)
type Config struct {
DBDriver string
DBSource string
ServerAddress string
}
func main() {
db, err := openDB()
if err != nil {
log.Printf("Databse Connection Error", err)
}
err = db.Ping()
fmt.Println("Database Conn opend")
if err != nil {
log.Printf("Error while Pinging Database")
}
}
func openDB() (*sql.DB, error) {
// Here you can populate the DB params from Env or Config file or use marshaling to fill in the values
dbParams := Config{
DBDriver: "postgres",
DBSource: "postgres://usename:password@localhost:5432/DatabaseName?sslmode=disable",
ServerAddress: "",
}
db, err := sql.Open(dbParams.DBDriver, dbParams.DBSource)
if err != nil {
fmt.Println("error", err)
return nil, err
}
return db, nil
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论