-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.go
More file actions
41 lines (33 loc) · 765 Bytes
/
db.go
File metadata and controls
41 lines (33 loc) · 765 Bytes
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"
"fmt"
"os"
_ "github.com/lib/pq"
)
const (
host = "postgresql"
port = 5432
)
func setupDatabase() (*sql.DB, error) {
psqlInfo := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
host,
port,
os.Getenv("POSTGRES_USER"),
os.Getenv("POSTGRES_PASSWORD"),
os.Getenv("POSTGRES_DB"))
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
return db, err
}
fmt.Println("Successfully connected to database!")
return db, nil
}
// DB functions go here.
// All DB functions should be a method of the server struct i.e. func (s *server) ...
// This is so that the DB functions can access the db through the server struct