-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-pg-schema.go
84 lines (71 loc) · 1.68 KB
/
get-pg-schema.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
78
79
80
81
82
83
84
// The utility helps to verify that you can
// connect to a PostgreSQL database and get
// a list of available databases and tables
// in that database and public schema.
package main
// Imports all of the needed packages.
import (
"database/sql"
"fmt"
"os"
_ "github.com/lib/pq"
)
func main() {
// Try to parse the arguments from the cmd .
arguments := os.Args
if len(arguments) != 6 {
fmt.Println("дай - hostname post username password db")
return
}
// Put the values to the vars.
host := arguments[1]
port := arguments[2]
usrName := arguments[3]
password := arguments[4]
dbName := arguments[5]
// Create string of connection data.
conn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", host, port, usrName, password, dbName)
// Try to open connection to db.
db, err := sql.Open("postgres", conn)
if err != nil {
fmt.Println("Open():", err)
return
}
defer db.Close()
// Try to read databases names.
rows, err := db.Query(`SELECT "datname" FROM "pg_database"
WHERE datistemplate = false`)
if err != nil {
fmt.Println("Query", err)
return
}
for rows.Next() {
var name string
err = rows.Scan(&name)
if err != nil {
fmt.Println("Scan:", err)
return
}
fmt.Println("*", name)
}
defer rows.Close()
// Try to read tables names.
rows, err = db.Query(`SELECT table_name FROM information_schema.tables
WHERE table_schema='public'
ORDER BY table_name`)
if err != nil {
fmt.Println("Query", err)
return
}
// BUG(r): Just the bug example)).
for rows.Next() {
var name string
err = rows.Scan(&name)
if err != nil {
fmt.Println("Scan:", err)
return
}
fmt.Println("+T", name)
}
defer rows.Close()
}