从代码
下面的代码输出以下内容:
2022/06/21 16:01:07 Failed to connect to db: failed to connect to 'host=localhost user=postgres database=local': server error (FATAL: Ident authentication failed for user "postgres" (SQLSTATE 28000)) exit status 1
import (
"context"
"log"
"github.com/jackc/pgx/v4"
)
func main() {
dbCtx := context.Background()
db, err := pgx.Connect(
dbCtx,
"postgres://postgres:smashthestate@localhost:5432/local",
)
if err != nil {
log.Fatalf("Failed to connect to db: %v\n", err)
}
defer db.Close(dbCtx)
// do stuff with db...
}
从终端
但是,可以从终端连接到数据库。例如,如果使用相同的参数(数据库名称、用户名、密码)运行此命令,将给出正确的输出:
psql -d local -U postgres -W -c 'select * from interest;'
即使用户发送的命令既不是 也可以正常root
工作postgres
。
本地连接的身份验证方法设置为trust
inside pg_hba.conf
:
local all all trust
那么,我在这里错过了什么?为什么在命令行中一切正常,但在代码中却不行?
慕田峪9158850