/
postgres.go
47 lines (43 loc) · 1.16 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
package util
import (
"fmt"
"github.com/jmoiron/sqlx"
)
func BuildPostgresConnStr(host string, port uint, user, password, dbname, sslmode string) (string, error) {
if host == "" || port == 0 || user == "" {
return "", fmt.Errorf("host, port, and user must be defined")
}
if sslmode == "" {
sslmode = "disable"
}
connString := fmt.Sprintf(
"host=%s port=%v user=%s sslmode=%s",
host, port, user, sslmode,
)
if dbname != "" {
connString = fmt.Sprintf("%s dbname=%s", connString, dbname)
}
if password != "" {
connString = fmt.Sprintf("%s password=%s", connString, password)
}
return connString, nil
}
// CreatePosgresDatabase creates a postgres database
func CreatePostgresDatabase(host string, port uint, user, password, dbname, sslmode string) error {
connString, err := BuildPostgresConnStr(
host, port, user, password, "", sslmode,
)
if err != nil {
return err
}
db, err := sqlx.Connect("postgres", connString)
if err != nil {
return fmt.Errorf("connecting to postgres: %v", err)
}
defer db.Close()
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", dbname))
if err != nil {
return fmt.Errorf("creating database: %v", err)
}
return nil
}