38 lines
702 B
Go
38 lines
702 B
Go
package dbase
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
_ "github.com/jackc/pgx"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type Deps struct {
|
|
Host string
|
|
Port string
|
|
Username string
|
|
Password string
|
|
DBName string
|
|
}
|
|
|
|
func ConnectPool(ctx context.Context, deps Deps) (*pgxpool.Pool, error) {
|
|
connString := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s",
|
|
deps.Host, deps.Port, deps.Username, deps.Password, deps.DBName)
|
|
|
|
config, err := pgxpool.ParseConfig(connString)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pool, err := pgxpool.NewWithConfig(ctx, config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err = pool.Ping(ctx); err != nil {
|
|
pool.Close()
|
|
return nil, err
|
|
}
|
|
|
|
return pool, nil
|
|
}
|