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

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

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:

  1. cfg := config{
  2. Host: "localhost",
  3. Port: 5432,
  4. Username: "postgres",
  5. Password: "ellez2004",
  6. DBname: "app",
  7. }
  8. 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)
  9. db,err := sql.Open("postgres",pconfig)
  10. if err != nil {
  11. log.Fatal(err.Error())
  12. }
  13. defer db.Close();
  14. insert := `insert into "User" ("firstname","lastname") values('Allaz','Bairamov')`
  15. _ ,err = db.Exec(insert);
  16. if err != nil {
  17. fmt.Println("error ---", err.Error());
  18. }
  19. fmt.Println("success");
  20. http.ListenAndServe(":8000", nil)

Please help:)

答案1

得分: 1

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

  1. 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连接匹配

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

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

  1. 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

  1. Host: "localhost", // where you host your Postgres server
  2. Port: 5432, // default port for Postgres
  3. Username: "postgres", // this is user credential to access of the database
  4. Password: "ellez2004", // corresponding password
  5. 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:

确定