postgresql - 由没有密码的用户连接到 Postgres DB
问题描述
我通过以下方式创建了一个用户:
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"
如何使用没有密码的用户连接到数据库?
解决方案
为用户分配密码然后使用它,或者更改您的 pg_hba.conf 文件,以便对该用户/dbname/connection-method/host 使用其他一些身份验证方法(然后重新启动服务器以使更改生效) .
sudo -u postgres psql -c "ALTER USER sample password 'yahkeixuom5R';"
推荐阅读
- c# - 使用与位置(纬度和经度)有关的地址列表(包含坐标)对用户集合进行排序
- python - 无需 SKLearn 即可快速创建交互术语
- python-3.x - Windows 10 无法检测到 Python 环境变量
- c# - 无法对空引用执行运行时绑定 Umbraco
- javascript - 使用 fullPage.js moveTo 有什么问题?
- python - 错误:解压缩数据时出现错误 -3:标头检查不正确
- angular-material - 如何在堆叠的 mat-chip-list 中将 mat-chips 居中
- parceljs - 如何处理包裹中的输出文件
- git-lfs - 如何在 Git LFS 存储库之间迁移
- c# - Linq GroupBy 多列投影到新对象