Golang的lib/pg无法连接到PostgreSQL。

huangapple go评论81阅读模式
英文:

Golang lib/pg can't connect to postgres

问题

我有这样的代码:

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"

我认为这会真正有所帮助 Golang的lib/pg无法连接到PostgreSQL。

英文:

I have such code:

package main

import (
        &quot;database/sql&quot;
        &quot;fmt&quot;
        &quot;log&quot;

        _ &quot;github.com/lib/pq&quot;
)

func main() {
        db, err := sql.Open(&quot;postgres&quot;, &quot;user=postgres dbname=vagrant sslmode=disable&quot;)
        if err != nil {
                log.Fatal(err)
        }

        rows, err := db.Query(&quot;SELECT 3+5&quot;)
        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 &quot;postgres&quot;
exit status 1

But I can access postgres:

[vagrant@localhost go-postgres]$ psql -U postgres vagrant
psql (9.4.4)
Type &quot;help&quot; 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:

&lt; 2015-12-19 12:13:05.094 UTC &gt;LOG:  could not connect to Ident server at address &quot;::1&quot;, port 113: Connection refused
&lt; 2015-12-19 12:13:05.094 UTC &gt;FATAL:  Ident authentication failed for user &quot;postgres&quot;
&lt; 2015-12-19 12:13:05.094 UTC &gt;DETAIL:  Connection matched pg_hba.conf line 84: &quot;host    all             all             ::1/128                 ident&quot;

I think it will really help Golang的lib/pg无法连接到PostgreSQL。

答案1

得分: 4

好的,以下是翻译好的内容:

好的,
正确的连接字符串是:

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

我假设pg库使用与psql或Ruby驱动程序相同的默认值。吸取了教训。

英文:

Ok,
correct connection string is:

&quot;user=postgres host=/tmp dbname=vagrant sslmode=disable&quot;

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

答案2

得分: 2

在控制台中运行:

psql "user=postgres dbname=vagrant sslmode=disable"

如果无法连接,则需要更改sslmode,否则我会考虑更多的解决方法。

英文:

Run in console:

psql &quot;user=postgres dbname=vagrant sslmode=disable&quot;

If you cannot connect so it needs to change sslmode else I will think more.

答案3

得分: 0

对于我来说,将主机设置为 /run/postgresql 是有效的。
来源:https://github.com/lib/pq/issues/645#issuecomment-320799610

主机 URL 取决于 Postgres 使用的套接字。套接字位置根据 postgres 的构建方式和位置而异。可以使用以下方法找到套接字位置:

  • 在 psql 中运行 \conninfo。它会显示套接字位置以及其他详细信息。
  • 在 postgresql.conf 中查找 unix_socket_directory 的值。

在我的 Ubuntu 发行版中,/var/run 是指向 /run 的软链接,这就是为什么 /run/postgresql 能够正常工作,尽管 unix_socket_directory\conninfo 提到的是 /var/run/postgresql

英文:

For me, setting host to /run/postgresql worked.
Credit: https://github.com/lib/pq/issues/645#issuecomment-320799610

The host url depends on the socket Postgres is using. The socket location varies based on how and where postgres was built. The socket location can be found using:

  • Inside psql, run \conninfo. It shows the socket location along with other details
  • unix_socket_directory value in postgresql.conf

In my Ubuntu distro, /var/run is a soft link to /run, that's why /run/postgresql works even though unix_socket_directory and \conninfo mention /var/run/postgresql.

huangapple
  • 本文由 发表于 2015年12月23日 21:14:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/34436547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定