cic-custodial/pkg/postgres/pool.go

35 lines
664 B
Go

package postgres
import (
"context"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
type PostgresPoolOpts struct {
DSN string
}
// NewPostgresPool creates a reusbale connection pool across the cic-custodial component.
func NewPostgresPool(o PostgresPoolOpts) (*pgxpool.Pool, error) {
parsedConfig, err := pgxpool.ParseConfig(o.DSN)
if err != nil {
return nil, err
}
dbPool, err := pgxpool.NewWithConfig(context.Background(), parsedConfig)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := dbPool.Ping(ctx); err != nil {
return nil, err
}
return dbPool, nil
}