-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres.go
35 lines (31 loc) · 922 Bytes
/
postgres.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package database
import (
"strings"
"github.com/gomig/utils"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
// NewPostgresConnector create new POSTGRES connection
func NewPostgresConnector(host string, port string, user string, password string, database string) (*sqlx.DB, error) {
params := []string{}
params = append(params, "host="+host)
params = append(params, "user="+user)
params = append(params, "sslmode=disable")
if port != "" {
params = append(params, "port="+port)
}
if password != "" {
params = append(params, "password="+password)
}
if database != "" {
params = append(params, "dbname="+database)
}
db, err := sqlx.Open("postgres", strings.Join(params, " "))
if err != nil {
return nil, utils.TaggedError([]string{"PostgresDriver"}, err.Error())
}
if err := db.Ping(); err != nil {
return nil, utils.TaggedError([]string{"PostgresDriver"}, err.Error())
}
return db, nil
}