You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
663 B
Go
42 lines
663 B
Go
package postgres
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"github.com/ajikamaludin/go-grpc_basic/pkg/v1/config"
|
|
_ "github.com/lib/pq"
|
|
)
|
|
|
|
const (
|
|
POSTGRES string = "postgres"
|
|
)
|
|
|
|
type Conn struct {
|
|
Conn string
|
|
}
|
|
|
|
func New(config *config.Config) (*Conn, error) {
|
|
conn := fmt.Sprintf("host=%v port=%v user=%v password=%v dbname=%v sslmode=disable",
|
|
config.Postgres.Host,
|
|
config.Postgres.Port,
|
|
config.Postgres.Username,
|
|
config.Postgres.Password,
|
|
config.Postgres.Dbname)
|
|
db, err := sql.Open(POSTGRES, conn)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = db.Ping()
|
|
defer db.Close()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Conn{
|
|
Conn: conn,
|
|
}, nil
|
|
}
|