Advertisement
Guest User

Untitled

a guest
Jul 5th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 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.  
  26. log.Printf("%v", openQuery)
  27. db, err := sql.Open("postgres", openQuery)
  28.  
  29. // test and open pgsql connection
  30. err = db.Ping()
  31. log.Printf("%v", err)
  32. if err == nil {
  33. s.DB = db
  34. } else {
  35. err = fmt.Errorf("cannot connect to the database err=%v\n", err)
  36. }
  37.  
  38. return err
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement