36 lines
566 B
Go
36 lines
566 B
Go
package repository
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
_ "github.com/lib/pq"
|
|
)
|
|
|
|
const (
|
|
usersTable = "users"
|
|
)
|
|
|
|
type Config struct {
|
|
Host string
|
|
Port string
|
|
Username string
|
|
Password string
|
|
DBName string
|
|
SSLMode string
|
|
}
|
|
|
|
func NewPostgresDB(cfg Config) (*sql.DB, error) {
|
|
db, err := sql.Open("postgres",
|
|
fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=%s",
|
|
cfg.Username, cfg.Password, cfg.Host, cfg.DBName, cfg.SSLMode))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = db.Ping()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return db, nil
|
|
}
|