通过 pq 连接到 PostgreSQL 数据库时返回“bad connection”错误。

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

Connecting to a PostgreSQL database through pq returns a "bad connection" error

问题

我正在使用两台不同的计算机,使用Go和PostgreSQL制作一个Web应用程序。这两台计算机的设置是相同的(Ubuntu,并安装了最新版本的Go和PostgreSQL)。问题是我无法让我的应用程序连接到我的笔记本电脑上的数据库。

我使用了以下代码:

func (db *Database) Dial(user string, password string, dbname string) {
    var err error
    db.Conn, err = sql.Open("postgres", "user="+user+" password="+password+" dbname="+dbname+" sslmode=require")
    if err != nil {
        fmt.Println("无法连接到" + dbname + "!")
        log.Fatal(err)
    }
    err = db.Conn.Ping()
    if err != nil {
        fmt.Println("无法Ping到" + dbname + "!")
        fmt.Println(err)
    }
}

然后我得到了以下错误信息:

无法Ping到my_database!

driver: bad connection

我在Stack Overflow上找到了很多关于相同错误的问题,但是没有找到解决我的情况的解决方案。

另外,就像在我的台式电脑上一样,在我的笔记本电脑上也有一个名为postgres的用户,并且我可以通过psql连接到数据库,所以守护进程是活动的,密码也是正确的。我在两台计算机上使用完全相同的设置和代码。

我的问题是:如何获取有关错误的更多信息?我认为"bad connection"这个错误信息太过笼统。我相信更多的信息会对我有很大帮助。

另外,你有什么关于这个错误可能的原因的想法吗?

更新

PostgreSQL日志显示如下:

2014-09-29 14:23:26 EDT FATAL: password authentication failed for user "postgres"

2014-09-29 14:23:26 EDT DETAIL: Connection matched pg_hba.conf line 92: "host all all 127.0.0.1/32 md5"

但是我已经仔细检查了密码,应该是正确的。我也可以使用postgres用户登录,运行psql命令并执行查询。

英文:

I am making a web app in Go and PostgreSQL using two different computers. The setup is the same on both computers (Ubuntu with last versions of Go and PostgreSQL). The problem is that I cannot get my app to connect to the database on my laptop.

I use this piece of code:

<!-- language: lang-golang -->

func (db *Database) Dial(user string, password string, dbname string) {
    var err error
    db.Conn, err = sql.Open(&quot;postgres&quot;, &quot;user=&quot;+user+&quot; password=&quot;+password+&quot; dbname=&quot;+dbname+&quot; sslmode=require&quot;)
    if err != nil {
        fmt.Println(&quot;Connection to &quot; + dbname + &quot; not possible!&quot;)
        log.Fatal(err)
    }
    err = db.Conn.Ping()
    if err != nil {
        fmt.Println(&quot;Ping to &quot; + dbname + &quot; not possible!&quot;)
        fmt.Println(err)
    }
}

And I get:

> Ping to my_database not possible!
>
> driver: bad connection

I found many questions on SO with the same error, but I found no solution that would solve my case.

Also, on my laptop, just like on my desktop computer, there is a user postgres and I can connect to the database through psql, so the daemon is active and the password is right. I use the exact same setup and code on both computers.

My question is: how do I get more information about the error? I find "bad connection" to be too vague. I'm sure that a bit more of information would help me a lot.

Also, do you have an idea of would could cause the error?

Update

The PostgreSQL log says this:

> 2014-09-29 14:23:26 EDT FATAL: password authentication failed for user "postgres"
>
>2014-09-29 14:23:26 EDT DETAIL: Connection matched pg_hba.conf line 92: "host all all 127.0.0.1/32 md5"

But I double checked the password and it shoud be fine. I can also log in as the posgres user, run the psql command and execute queries.

答案1

得分: 3

我找到了解决方法。

我以 postgres 用户登录,并运行 psql postgres 命令。换句话说,我连接到了 postgres 数据库。然后我运行了命令 \password 并输入了一个新密码。

我输入了与一开始使用的完全相同的密码,不知何故现在它可以工作了。

问题已解决,但如果有人对可能导致问题的原因有任何想法,我很感兴趣。也许与密码过期有关?

英文:

I found a way to fix this.

I log in as postgres and run psql postgres. In other words, I connect to the postgres database. Then I run the command \password and enter a new password.

I entered the exact same password that I have been using since the beginning and somehow now it works.

The problem is solved, but if someone has an idea about what could have been the cause, I'm interested. Maybe it has something to do with password expiration?

huangapple
  • 本文由 发表于 2014年9月30日 00:43:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/26104846.html
匿名

发表评论

匿名网友

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

确定