-
Notifications
You must be signed in to change notification settings - Fork 88
/
postgres.go
73 lines (60 loc) · 1.64 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package identity
import (
"database/sql"
"fmt"
"github.com/lib/pq"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/persistence"
)
func postgresUserExists(user string) (bool, error) {
db := persistence.MustGetDBSession()
query := "SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = $1"
row := db.QueryRow(query, user)
var exists bool
err := row.Scan(&exists)
if err == sql.ErrNoRows {
return false, nil
} else if err != nil {
return false, errors.Wrap(err, "failed to query user")
}
return true, nil
}
func CreateDexPostgresDatabase(database, user, password string) error {
db := persistence.MustGetDBSession()
databaseQ := pq.QuoteIdentifier(database)
userQ := pq.QuoteIdentifier(user)
query := "SELECT 1 FROM pg_database WHERE datname = $1"
row := db.QueryRow(query, database)
var exists bool
err := row.Scan(&exists)
if err == sql.ErrNoRows {
query := fmt.Sprintf("CREATE DATABASE %s", databaseQ)
_, err := db.Exec(query)
if err != nil {
return errors.Wrap(err, "failed to create database")
}
} else if err != nil {
return errors.Wrap(err, "failed to query database")
}
exists, err = postgresUserExists(user)
if err != nil {
return errors.Wrap(err, "failed to query user")
}
if !exists {
query := fmt.Sprintf("CREATE USER %s", userQ)
_, err := db.Exec(query)
if err != nil {
return errors.Wrap(err, "failed to create user")
}
}
query = fmt.Sprintf(
`ALTER USER %s WITH PASSWORD '%s';
GRANT ALL PRIVILEGES ON DATABASE %s TO %s;`,
userQ, password, databaseQ, userQ,
)
_, err = db.Exec(query)
if err != nil {
return errors.Wrap(err, "failed to grant user privileges")
}
return nil
}