title: pq: sorry, too many clients already
body: <p>I am getting <code>pq: sorry, too many clients already</code> error when I am calling the <code>GetMessages()</code> multiple times. </p>
  
  <p>Please find the updated code:</p>
  
  <p><code>main()</code> code</p>
  
  <pre><code>func main() {
    dbConn, err := InitDB()
    if err != nil {
      Log.Error("Connection Error: ", err.Error())
      return
    }
    defer dbConn.Close()
    go run()
    var input string
    fmt.Scanln(&amp;input)
  }
  </code></pre>
  
  <p>Database connection code is:</p>
  
  <pre><code>func InitDB()(*sql.DB, error) {
    connectionString := fmt.Sprintf("user=%v password='%v' dbname=%v sslmode=disable", USER, PASSWORD, DATABASE)
    db, err = sql.Open(DRIVER, connectionString)
    return db, err
  }
  </code></pre>
  
  <p><code>run</code> goroutine:</p>
  
  <pre><code>func run() {
    for {
      messages, err := GetMessages()
      if err != nil {
        Log.Error("Connection Error: ", err.Error())
        return
      }
      log.Info(messages)
    }
  }
  </code></pre>
  
  <p><code>GetMessages()</code> function code: </p>
  
  <pre><code>func GetMessages() (messages []string, err error) {
      rows, err := db.Query(`SELECT message1, message2, message3, message4, message5,
              message6, message7, message8, message9, message10, message11, message12, message13, 
              message14, message15, message16, message17, message18, message19, message20, message21,
              message22, message23, message24, message25, message26, message27, message28, message29,
              message30, message31, message32, message33, message34, message35, message36, message37,
              message38, message39, message40, message41, message42, message43, message44, message45,
              message46, message47, message48 FROM table1 WHERE id=1`)
  
      if err != nil {
          Log.Error("Query error", err)
          return messages, err
      }
  
      var pointers []interface{}
      defer rows.Close()
  
      for rows.Next() {
          pointers = make([]interface{}, 48)
          messages = make([]string, 48)
          for i, _ := range pointers {
              pointers[i] = &amp;messages[i]
          }
          err = rows.Scan(pointers...)
          if err != nil {
              Log.Error("Failed to scan row", err)
              return messages, err
          }
      }
  
      return messages, nil
  }
  </code></pre>
  
  <p>I checked <a href="https://stackoverflow.com/questions/47706147/how-to-reuse-a-single-postgres-db-connection-for-row-inserts-in-go/47711577#47711577">this</a> answer and I have used <code>scan</code> but still it isn't working</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Issue was in another function. I was using <code>db.Query</code> without closing the returned <code>rows</code> object and was repeatedly calling that function. I've updated my code; used <code>db.Exec</code> instead of <code>db.Query</code> and it's working now. Thank you so much <a href="https://stackoverflow.com/users/965900/mkopriva">@mkopriva</a> for <a href="https://stackoverflow.com/a/50666083/4596814">this</a> answer. :)</p>