22 lines
487 B
Go
22 lines
487 B
Go
package database
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
func NewDB() (*sql.DB, error) {
|
|
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"))
|
|
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
|
|
}
|