Postgresql 由没有密码的用户连接到Postgres DB

Postgresql 由没有密码的用户连接到Postgres DB,postgresql,go,go-gorm,Postgresql,Go,Go Gorm,我通过以下方式创建了一个用户: sudo -u postgres psql -c "CREATE USER sample;" 然后创建了一个数据库: sudo -u postgres psql -c "CREATE DATABASE practice OWNER sample;" 现在,我正试图通过以下代码段连接到此数据库: dsn := url.URL{ User: url.UserPassword("sample&

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

sudo -u postgres psql -c "CREATE USER sample;"
然后创建了一个数据库:

sudo -u postgres psql -c "CREATE DATABASE practice OWNER sample;"
现在,我正试图通过以下代码段连接到此数据库:

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"

如何使用没有密码的用户连接到DB?

为用户分配密码,然后使用它,或者更改pg_hba.conf文件,以便用于该用户/dbname/connection method/host(然后重新启动服务器,使更改生效)


我可以通过
p连接到dbostgresql://sample:@/练习使用Python和SQLAlchemy,而我在
pg_hba.conf
中没有做任何更改。所以我不明白为什么不能连接。您对通过
psql
与没有密码的用户连接有何想法?@shayanrokrok您的应用程序正在指定“localhost”,它使用pg_hba的“host”行,而@/正在使用unix域套接字,它使用pg_hba的“local”行。他们显然指定了不同的事情。你说得太对了。正确的URL是:
postgresql://sample:@localhost/practice
sudo -u postgres psql -c "ALTER USER sample password 'yahkeixuom5R';"