25 lines
445 B
Go
25 lines
445 B
Go
|
|
package db
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
||
|
|
)
|
||
|
|
|
||
|
|
func Connect(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
|
||
|
|
cfg, err := pgxpool.ParseConfig(databaseURL)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if err := pool.Ping(ctx); err != nil {
|
||
|
|
pool.Close()
|
||
|
|
return nil, fmt.Errorf("ping: %w", err)
|
||
|
|
}
|
||
|
|
return pool, nil
|
||
|
|
}
|