【问题标题】:Connect to Postgres DB by a user which has no password由没有密码的用户连接到 Postgres DB
【发布时间】:2020-10-28 00:06:57
【问题描述】:

我通过以下方式创建了一个用户:

sudo -u postgres psql -c "CREATE USER sample;"

然后创建了一个数据库:

sudo -u postgres psql -c "CREATE DATABASE practice OWNER sample;"

现在我正在尝试通过以下 sn-p 连接到该数据库:

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

但是输出是:

2020/07/07 16:43:44 pq: password authentication failed for user "sample"

如何使用没有密码的用户连接数据库?

【问题讨论】:

    标签: postgresql go go-gorm


    【解决方案1】:

    要么为用户分配一个密码然后使用它,要么更改您的 pg_hba.conf 文件以便 some other method of authentication 用于该用户/dbname/connection-method/host(然后重新启动您的服务器以使更改生效)。

    sudo -u postgres psql -c "ALTER USER sample password 'yahkeixuom5R';"
    

    【讨论】:

    • 我可以使用 Python 和 SQLAlchemy 通过postgresql://sample:@/practice 连接到 db,而我没有更改 pg_hba.conf 中的任何内容。所以我不明白为什么无法连接。您对通过psql 与没有密码的用户连接有什么想法吗?
    • @shayanrokrok 您的应用指定了“localhost”,它使用 pg_hba 中的“主机”行,而 @/ 使用的是 unix 域套接字,它使用 pg_hba 中的“本地”行。他们显然指定了不同的东西。
    • 你说的太对了。正确的 URL 是:postgresql://sample:@localhost/practice
    猜你喜欢
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 2015-10-07
    • 2016-06-18
    • 2016-11-18
    • 2016-09-14
    • 1970-01-01
    相关资源
    最近更新 更多