This repository has been archived on 2023-07-09. You can view files and clone it, but cannot push or open issues or pull requests.
sudoscientist-go-backend/packages/database/database.go

22 lines
487 B
Go
Raw Permalink Normal View History

2019-02-03 06:57:08 +00:00
package database
2019-04-14 03:38:51 +00:00
2019-02-03 06:57:08 +00:00
import (
2019-04-14 03:38:51 +00:00
"database/sql"
2019-02-03 06:57:08 +00:00
"fmt"
"os"
)
func NewDB() (*sql.DB, error) {
2019-04-14 03:38:51 +00:00
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s", os.Getenv("DB_HOST"), os.Getenv("DB_PORT"), os.Getenv("DB_USER"), os.Getenv("DB_PW"), os.Getenv("DB_NAME"), os.Getenv("DB_SSL"))
2019-02-03 06:57:08 +00:00
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Connected to database")
return db, nil
}