简体   繁体   English

pq:在 vscode 中访问 postgres 时用户“用户名”的密码验证失败

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

I am making an API using Golang, and I am using Postgres as database.我正在使用 Golang 制作一个 API,我正在使用 Postgres 作为数据库。 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:-但是当我构建 model 时,我得到的错误是:-

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.尽管当我尝试在 pgAdmin 中使用相同的密码登录时,我能够完美地登录。 Then the thing I am not able to understand is why I am unable to connect it while using vscode.然后我无法理解的是为什么我在使用 vscode 时无法连接它。

Any help would be much appreciated.任何帮助将非常感激。 Thank you in advance...先感谢您...

I'm not sure your code to open connection with gorm is correct.我不确定您打开与 gorm 连接的代码是否正确。

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.我建议您使用fmt.Sprintf设置dsn以提高可读性。
Because your user have space , you should add ' in start and end user因为您的user have space ,所以您应该在开始和最终user中添加'

This is the example:这是例子:

    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)
    }

In the connection string, 'username' is not an accepted synonym for 'user'.在连接字符串中,“用户名”不是“用户”的公认同义词。 And 'dbName' is not an accepted synonym for 'dbname'.并且“dbName”不是“dbname”的公认同义词。

So the "Ansh Joshi" 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.因此,错误消息中用户名的"Ansh Joshi"不是来自您传入的值,而是(显然)回退到默认值,即使用您的操作系统用户名。 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.当您可以从 pgAdmin 成功连接时,您可能正在使用与“Ansh Joshi”不同的用户名:一个与密码匹配的用户名。 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 "username" .如果您没有因密码错误而失败,您可能会因FATAL: unrecognized configuration parameter "username"类的内容而失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接到在 Docker 中运行的 postgres 时出错:pq:用户“postgres”的密码验证失败 - Getting an error while connecting to postgres running in Docker: pq: password authentication failed for user “postgres” panic:pq:用户“ postgres”的密码认证失败,同时通过hypertable中的timescaledb-parallel-copy将数据添加到表中 - panic: pq: password authentication failed for user “postgres” while adding the data into table through timescaledb-parallel-copy in hypertable 致命:连接到 postgres 时,用户“postgres”的密码身份验证失败 - FATAL: password authentication failed for user "postgres" While connecting to postgres '用户“postgres”的密码验证失败' - 'password authentication failed for user "postgres"' 用户postgres的密码验证失败 - password authentication failed for user postgres Postgres集群:用户“ postgres”的密码身份验证失败 - Postgres cluster: password authentication failed for user “postgres” Docker postgres映像中的用户密码验证失败 - Password authentication failed for user in Docker postgres image Kubernetes postgres - 用户密码验证失败 - Kubernetes postgres - password authentication failed for user PostgreSQL:致命:用户“postgres”的密码身份验证失败 - PostgreSQL: FATAL: password authentication failed for user "postgres" 用户“ postgres”的jitterbit-password身份验证失败 - jitterbit-password authentication failed for user “postgres”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM