1

I'm having this issue

 db, err := sql.Open("postgres", "user=xxx dbname=xxx connect_timeout=5 sslmode=disable")
 if err != nil {
    log.Fatal(err)
 }

I have no postgres installed on my localhost so sql.Open should return some error but actually it is not until I try to prepare a query and finally I get a connection refused error

stmt, err := c.DB.Prepare("SELECT id FROM services WHERE name = $1")
if err != nil {
    log.Fatal(err)
}

is this an expected behaviour? or I'm missing something...

1

1 Answer 1

3

According to this, Yes it is an expected behavior. Open() does not directly open a connection to the database. Instead the first connection is opened when the database is actually used the first time.

Open may just validate its arguments without creating a connection 
to the database. 
To verify that the data source name is valid, call Ping.

Use Ping() to check if the connection is valid or not.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks sadil I should read the documentation with more patience!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.