Postgres connection timed out

Hi,
I am facing the following problem:
I am opening a Postgres connection without any error (my GO app and Postgres DDBB live within a container)


	//Connect to Postgres DDBB
	db, err := sql.Open("postgres", *databaseURL)

	if err != nil {
		panic(fmt.Sprintf("Got error when connect database, the error is '%v'", err))
	}

So, no errors display when executing this code, but when I do


	err := db.QueryRow("select id from ir_attachment where NAME='$1'", pdfFile).Scan(&pi_id)

	if err == sql.ErrNoRows {
		pi_id = 0
		fmt.Printf("Error no rows")
	} else if err != nil {
		log.Fatal(err)
		fmt.Printf("Error fatal!")
	}

I only get a time out…none of the above errors are shown either…

2017/01/11 11:32:55 dial tcp 10.0.0.6:5432: getsockopt: connection timed out

Any help would be appreciated.

Cheers.

sql.Open only creates a connection pool, checks that the driver is registered. The firt use creates the first connection - so use db.Ping to check the db connection parameters!

Hi,
Thanks, the db.Ping also times out indeed.
I don’t really know what you mean by having the driver registered. Can you explain that to me please?

import (
	"crypto/sha1"
	"database/sql"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"regexp"

	"github.com/kardianos/osext"
	_ "github.com/lib/pq"
	"github.com/pkg/sftp"
	"golang.org/x/crypto/ssh"
)

Here is my import, and as far as I know I don’t have to register the driver anywhere, except this post that I’ve found:
http://golang-basic.blogspot.com.es/2014/06/golang-database-step-by-step-guide-on.html

Thanks

github.com/lib/pq has an init function that registers the driver with database/sql. You don’t have to do anything more than import the package.

All the database drivers that work with database/sql work in a similar manner.

The problem is solved, I think it was an issue with the parameters, to debug this I did from command line:

root@7d5797398768:/# psql "host=postgres port=5432 user=odoo password=odoo dbname=mydb sslmode=disable"

Thanks a lot

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.