未知的身份验证响应:10 Postresql golang

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

Unknown authentication response: 10 Postresql golang

问题

我在使用golang连接到PostgreSQL时遇到了错误。

错误信息:pq: unknown authentication response: 10

代码:

cfg := config{
Host: "localhost",
Port: 5432,
Username: "postgres",
Password: "ellez2004",
DBname: "app",
}
pconfig := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",cfg.Host,cfg.Port,cfg.Password,cfg.Password,cfg.DBname)
db,err := sql.Open("postgres",pconfig)
if err != nil {
log.Fatal(err.Error())
}
defer db.Close();

insert := insert into "User" ("firstname","lastname") values('Allaz','Bairamov')
_ ,err = db.Exec(insert);
if err != nil {
fmt.Println("error ---", err.Error());
}
fmt.Println("success");
http.ListenAndServe(":8000", nil)

请帮忙解决:)

英文:

I get an error while connect to postresql with golang

ERROR ----------- pq: unknown authentication response: 10

CODE:

cfg := config{
	Host:     "localhost",
	Port:     5432,
	Username: "postgres",
	Password: "ellez2004",
	DBname:   "app",
}
pconfig := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",cfg.Host,cfg.Port,cfg.Password,cfg.Password,cfg.DBname)
db,err := sql.Open("postgres",pconfig)
if err != nil {
	log.Fatal(err.Error())
}
defer db.Close();

insert := `insert into "User" ("firstname","lastname") values('Allaz','Bairamov')`
_ ,err = db.Exec(insert);
if err != nil { 
	fmt.Println("error ---", err.Error());
}
fmt.Println("success");
http.ListenAndServe(":8000", nil)

Please help:)

答案1

得分: 1

似乎有一个拼写错误。应该是.Username而不是.Password

pconfig := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",cfg.Host,cfg.Port,cfg.Username,cfg.Password,cfg.DBname)

另外,请确保所有参数与Postgres连接匹配

    Host:     "localhost", // 您托管Postgres服务器的位置
    Port:     5432, // Postgres的默认端口
    Username: "postgres", // 这是访问数据库的用户凭据
    Password: "ellez2004", // 对应的密码
    DBname:   "app", // 数据库模式名称
英文:

Seems like there is typo. Should be .Username not .Password

pconfig := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",cfg.Host,cfg.Port,cfg.Password,cfg.Password,cfg.DBname)

Else, try make sure all the parameters tally with Postgres Connection

    Host:     "localhost", // where you host your Postgres server
    Port:     5432, // default port for Postgres
    Username: "postgres", // this is user credential to access of the database
    Password: "ellez2004", // corresponding password
    DBname:   "app", // the database schema name

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

发表评论

匿名网友

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

确定