Postgresql Golang lib/pg can';t连接到postgres
我有这样的代码:Postgresql Golang lib/pg can';t连接到postgres,postgresql,go,Postgresql,Go,我有这样的代码: package main import ( "database/sql" "fmt" "log" _ "github.com/lib/pq" ) func main() { db, err := sql.Open("postgres", "user=postgres dbname=vagrant sslmode=disable") if err != nil {
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/lib/pq"
)
func main() {
db, err := sql.Open("postgres", "user=postgres dbname=vagrant sslmode=disable")
if err != nil {
log.Fatal(err)
}
rows, err := db.Query("SELECT 3+5")
if err != nil {
log.Fatal(err)
}
fmt.Println(rows)
}
其结果是:
[vagrant@localhost go-postgres]$ go run test.go
2015/12/19 11:03:53 pq: Ident authentication failed for user "postgres"
exit status 1
但我可以访问postgres:
[vagrant@localhost go-postgres]$ psql -U postgres vagrant
psql (9.4.4)
Type "help" for help.
vagrant=#
我在postgres上使用Rails应用程序没有任何问题
有人有主意吗
编辑:
这是我的pg_hba.conf:
local all all trust
host all all 127.0.0.1/32 ident
host all all ::1/128 ident
编辑2:
我在我的postgres日志中发现:
< 2015-12-19 12:13:05.094 UTC >LOG: could not connect to Ident server at address "::1", port 113: Connection refused
< 2015-12-19 12:13:05.094 UTC >FATAL: Ident authentication failed for user "postgres"
< 2015-12-19 12:13:05.094 UTC >DETAIL: Connection matched pg_hba.conf line 84: "host all all ::1/128 ident"
<2015-12-19 12:13:05.094 UTC>日志:无法连接到地址为“::1”的Ident服务器,端口113:连接被拒绝
<2015-12-19 12:13:05.094 UTC>致命:用户“postgres”的身份验证失败
<2015-12-19 12:13:05.094 UTC>详细信息:连接匹配pg_hba.conf第84行:“所有主机::1/128标识”
我认为这真的会有帮助;) 运行控制台:
psql "user=postgres dbname=vagrant sslmode=disable"
如果您无法连接,则需要更改sslmode,否则我会考虑更多。好的,
正确的连接字符串为:
"user=postgres host=/tmp dbname=vagrant sslmode=disable"
我假设pg库使用与
psql
或ruby驱动程序相同的默认值。吸取的教训。您确定用户postgres没有密码吗?也就是说,user=postgres password=postgres
有效吗?@Ainar-G我是。我可以连接到psql并使用其他应用程序(Rails),无需密码。请显示“cat/var/lib/pgsql//data/pg_hba.conf”结果尝试将“ident”替换为我服务器上的“trust”:local all trust;托管所有127.0.0.1/32信任;主机所有::1/128信任OK,看起来我很懒。。。我需要选中此选项:host-要连接的主机。以/开头的值适用于unix域套接字。(默认为localhost)
。其他驱动程序默认使用unix套接字,此处默认为localhost。看起来我还没有安装ident。。。