-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
75 lines (61 loc) · 1.58 KB
/
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
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
package main
import (
"database/sql"
"fmt"
"github.com/jackc/pgx/v5/pgconn"
// If we want to use the pq driver, rename the driver to 'postgres` and use SAN
// in the cert's CN.
//_ "github.com/lib/pq"
_ "github.com/jackc/pgx/v5/stdlib"
)
const driver = "pgx"
func noTLSConnect() (*sql.DB, error) {
db, err := sql.Open(driver, "host=localhost port=5432 user=postgres password=password dbname=postgres")
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
return nil, err
}
return db, nil
}
func tlsConnect() (*sql.DB, error) {
// Let's connect to postgres using tls.
// TODO: The dsn for postgres works now. However, we still can't use sslmode=verify-full. Self-signed CA is a pain. Using sslmode=verify-ca for now.
db, err := sql.Open(driver, "host=localhost port=5432 user=postgres sslmode=verify-ca sslrootcert=docker/postgresql/ca-cert.pem sslcert=docker/postgresql/client-cert.pem sslkey=docker/postgresql/client-key.pem")
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
pgErr, ok := err.(*pgconn.PgError)
if !ok {
fmt.Println(pgErr)
}
return nil, err
}
return db, nil
}
func main() {
db, err := noTLSConnect()
if err != nil {
fmt.Println(err)
fmt.Println("This above error is expected when connecting to postgres without tls. The program will now connect to postgres using tls.")
}
defer func() {
if db != nil {
db.Close()
}
}()
tdb, err := tlsConnect()
if err != nil {
panic(err)
}
defer func() {
if tdb != nil {
tdb.Close()
}
}()
fmt.Println("Successfully connected to postgres using tls")
}