21 lines
465 B
Go
21 lines
465 B
Go
package database
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"database/sql"
|
|
)
|
|
|
|
func NewDB() (*sql.DB, error) {
|
|
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", os.Getenv("DBHOST"), os.Getenv("DBPORT"), os.Getenv("DBUSER"), os.Getenv("DBPW"), os.Getenv("DBNAME"))
|
|
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
|
|
}
|