-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
41 lines (32 loc) · 784 Bytes
/
main.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
package main
import (
"database/sql"
"log"
"os"
_ "github.com/lib/pq"
)
func main() {
log.Printf("pgismaster, Andy Gallagher 2019. See https://github.com/fredex42/pgismaster for details.")
connStr := "user=postgres sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
log.Fatalf("Could not connect to local database: %s", err)
}
rows, err := db.Query("select pg_is_in_recovery();")
if err != nil {
log.Fatalf("Could not query database: %s", err)
}
var isInRecovery bool
rows.Next()
scanErr := rows.Scan(&isInRecovery)
if scanErr != nil {
log.Fatalf("Could not interpret data from server: %s", err)
}
if isInRecovery {
log.Printf("Server is a standby")
os.Exit(1)
} else {
log.Printf("Server is a master")
os.Exit(0)
}
}