-
Notifications
You must be signed in to change notification settings - Fork 22
/
WorkspaceONE_Query_PostgreSQL.go
77 lines (64 loc) · 1.62 KB
/
WorkspaceONE_Query_PostgreSQL.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
74
75
76
77
package main
import (
"database/sql"
"fmt"
"io/ioutil"
_ "github.com/lib/pq"
)
func connectDB() *sql.DB{
fmt.Println("[*] Get password of the db")
b, err := ioutil.ReadFile("/usr/local/horizon/conf/db.pwd")
if err != nil {
fmt.Print(err)
}
password := string(b)
fmt.Println("[+] " + password)
var host = "localhost"
var port int = 5432
var user = "horizon"
var dbname = "saas"
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
fmt.Println("[*] psqlInfo:" + psqlInfo)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("[+] Successfully connected!")
return db
}
func queryUser(db *sql.DB){
fmt.Println("[*] Querying User")
var strUsername,strEmail,idUser,createdDate,domain string
var searchStr = `SELECT "strUsername","strEmail","idUser","createdDate","domain" FROM "Users"`
fmt.Println(" " + searchStr)
rows,err:=db.Query(searchStr)
if err!= nil{
panic(err)
}
defer rows.Close()
for rows.Next(){
err:= rows.Scan(&strUsername,&strEmail,&idUser,&createdDate,&domain)
if err!= nil{
//fmt.Println(err)
}
fmt.Println(" - idUser : " + idUser)
fmt.Println(" strUsername: " + strUsername)
fmt.Println(" strEmail : " + strEmail)
fmt.Println(" createdDate: " + createdDate)
fmt.Println(" domain : " + domain)
}
err = rows.Err()
if err!= nil{
panic(err)
}
}
func main() {
db:=connectDB()
queryUser(db)
}