[英]Connect to Postgres DB by a user which has no password
I created a user by:我通过以下方式创建了一个用户:
sudo -u postgres psql -c "CREATE USER sample;"
Then created a database:然后创建了一个数据库:
sudo -u postgres psql -c "CREATE DATABASE practice OWNER sample;"
Now I'm trying to connect to this DB by following snippet:现在我正在尝试通过以下代码段连接到该数据库:
dsn := url.URL{
User: url.UserPassword("sample", ""),
Scheme: "postgres",
Host: fmt.Sprintf("%s", "localhost"),
Path: "practice",
RawQuery: (&url.Values{"sslmode": []string{"disable"}}).Encode(),
}
db, err := gorm.Open(
"postgres",
dsn.String(),
)
if err != nil {
log.Fatal(err)
}
But the output is:但是 output 是:
2020/07/07 16:43:44 pq: password authentication failed for user "sample"
How do I connect to DB with a user which has no password?如何使用没有密码的用户连接到数据库?
Either assign the user a password and then use it, or change your pg_hba.conf file so that some other method of authentication is used for that user/dbname/connection-method/host (and then restart your server so the change takes effect).给用户分配一个密码然后使用它,或者更改您的 pg_hba.conf 文件,以便对该用户/dbname/connection-method/host 使用其他一些身份验证方法(然后重新启动您的服务器以使更改生效) .
sudo -u postgres psql -c "ALTER USER sample password 'yahkeixuom5R';"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.