duanlianyun0462 2015-12-23 13:14
浏览 128
已采纳

Golang lib / pg无法连接到Postgres

I have such code:

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

And its result is:

[vagrant@localhost go-postgres]$ go run test.go
2015/12/19 11:03:53 pq: Ident authentication failed for user "postgres"
exit status 1

But I can access postgres:

[vagrant@localhost go-postgres]$ psql -U postgres vagrant
psql (9.4.4)
Type "help" for help.

vagrant=#

And I don't have any troubles with using Rails app with postgres.

Anyone has an idea?

EDIT: Here is my pg_hba.conf:

local   all             all                                     trust
host    all             all             127.0.0.1/32            ident
host    all             all             ::1/128                 ident

EDIT2:

I found this in my postgres logs:

< 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"

I think it will really help ;)

  • 写回答

2条回答 默认 最新

  • donglin6659 2015-12-28 08:13
    关注

    Ok, correct connection string is:

    "user=postgres host=/tmp dbname=vagrant sslmode=disable"
    

    I assumed pg library uses same defaults as psql or ruby driver. Lesson learned.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型