英文:
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"
我认为这会真正有所帮助
英文:
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
答案1
得分: 4
好的,以下是翻译好的内容:
好的,
正确的连接字符串是:
"user=postgres host=/tmp dbname=vagrant sslmode=disable"
我假设pg库使用与psql
或Ruby驱动程序相同的默认值。吸取了教训。
英文:
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.
答案2
得分: 2
在控制台中运行:
psql "user=postgres dbname=vagrant sslmode=disable"
如果无法连接,则需要更改sslmode,否则我会考虑更多的解决方法。
英文:
Run in console:
psql "user=postgres dbname=vagrant sslmode=disable"
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论