Advertisement
Guest User

Untitled

a guest
Jul 5th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "database/sql"
  5. "fmt"
  6. "log"
  7.  
  8. _ "github.com/lib/pq"
  9. )
  10.  
  11. // Set a new db connection
  12. func (s *Switch) newDBConn() error {
  13. // open pgsql database
  14. if s.PGhost == "" {
  15. s.PGhost = "localhost"
  16. }
  17. if s.PGport == "" {
  18. s.PGport = "5432"
  19. }
  20. openQuery := "user=" + s.PGuser +
  21. " password=" + s.PGpasswd +
  22. " dbname=" + s.PGdbname +
  23. " host=" + s.PGhost +
  24. " port=" + s.PGport +
  25. " sslmode=disable"
  26.  
  27. log.Printf("%v", openQuery) // user=gerep password= dbname=managerdev host=localhost port=5432 sslmode=disable
  28. db, err := sql.Open("postgres", openQuery)
  29.  
  30. // test and open pgsql connection
  31. err = db.Ping()
  32. log.Printf("%v", err)
  33. if err == nil {
  34. lock.Lock()
  35. s.DB = db
  36. //s.DB.SetMaxIdleConns(700)
  37. //s.DB.SetMaxOpenConns(700)
  38. lock.Unlock()
  39. } else {
  40. err = fmt.Errorf("cannot connect to the database err=%v\n", err)
  41. }
  42.  
  43. return err
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement