pq: password authentication failed for user "user-name" while accessing postgres in vscode

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

pq: password authentication failed for user "user-name" while accessing postgres in vscode

问题

我正在使用Golang制作一个API,并且我正在使用Postgres作为数据库。我使用了以下命令:

args := "host=" + host + " port=" + port + " dbName=" + dbName + "username=" + username + " sslmode=disable password=" + password
	db, err := gorm.Open("postgres", args)
	if err != nil {
		log.Fatal(err)
	}
db.AutoMigrate(models.Hospital{})
	DB = db

但是在构建模型时,我遇到了以下错误:

pq: password authentication failed for user "Ansh Joshi"

尽管当我尝试使用相同的密码在pgAdmin中登录时,我可以完美地登录。然后我无法理解的是为什么在使用vscode时无法连接。

非常感谢您的帮助。提前谢谢...

英文:

I am making an API using Golang, and I am using Postgres as database. I have used the following command:-

args := "host=" + host + " port=" + port + " dbName=" + dbName + "username=" + username + " sslmode=disable password=" + password
	db, err := gorm.Open("postgres", args)
	if err != nil {
		log.Fatal(err)
	}
db.AutoMigrate(models.Hospital{})
	DB = db

But while I build the model, I am getting the error as:-

pq: password authentication failed for user "Ansh Joshi"

Though when I try to login using the same password in pgAdmin, I am able to log in it perfectly fine. Then the thing I am not able to understand is why I am unable to connect it while using vscode.

Any help would be much appreciated. Thank you in advance...

答案1

得分: 3

我不确定你打开与gorm的连接的代码是否正确。

在文档中,写的是这样的:

dsn := "host=localhost user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

我建议你使用fmt.Sprintf来设置dsn,以便更易读。因为你的user中有空格,所以你应该在user的开头和结尾添加'

这是一个示例:

args := fmt.Sprintf("host=%s port=%s dbname=%s user='%s' password=%s sslmode=%s", host, port, dbName, username, password, sslmode)
fmt.Println(args)

db, err := gorm.Open(postgres.Open(args), nil)
if err != nil {
    log.Fatal(err)
}
英文:

I'm not sure your code to open connection with gorm is correct.

In the documentation, it's write like this

dsn := "host=localhost user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

I recommend you to set dsn with fmt.Sprintf for more readable.<br/>
Because your user have space, you should add &#39; in start and end user

This is the example:

	args := fmt.Sprintf(&quot;host=%s port=%s dbname=%s user=&#39;%s&#39; password=%s sslmode=%s&quot;, host, port, dbName, username, password, sslmode)
	fmt.Println(args)

	db, err := gorm.Open(postgres.Open(args), nil)
	if err != nil {
		log.Fatal(err)
	}

答案2

得分: 2

在连接字符串中,'username'不是'user'的可接受同义词。而'dbName'也不是'dbname'的可接受同义词。

因此,错误消息中的用户名称"Ansh Joshi"并不是来自您传递的值,而是(显然)回退到默认值,即使用您的操作系统用户名。当您可以成功从pgAdmin连接时,您可能使用的是与"Ansh Joshi"不同的用户名:一个与密码匹配的用户名。错误消息中存在与正确用户名不同的用户名应该让您意识到错误的性质。

密码错误会优先显示其他错误。如果您没有遇到密码错误,您可能会遇到类似FATAL: unrecognized configuration parameter "username"的错误。

英文:

In the connection string, 'username' is not an accepted synonym for 'user'. And 'dbName' is not an accepted synonym for 'dbname'.

So the &quot;Ansh Joshi&quot; for the user name in the error message is not coming from the value you passed in, but instead is (apparently) falling back to the default, which is to use your OS username. When you can successfully connect from pgAdmin, you are presumably using a different user name than "Ansh Joshi": a user name that does match the password. The presence in the error message of a user name which differs from the correct one should have tipped you off about the nature of the error.

The password error preempts the other errors you get. If you hadn't failed with the password error, you probably would have failed with something like FATAL: unrecognized configuration parameter &quot;username&quot;.

huangapple
  • 本文由 发表于 2022年2月28日 03:10:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/71287725.html
匿名

发表评论

匿名网友

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

确定