db.go 545 B

1234567891011121314151617181920212223242526272829
  1. package repository
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/jackc/pgx/v5/pgxpool"
  6. )
  7. func NewPool(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
  8. cfg, err := pgxpool.ParseConfig(databaseURL)
  9. if err != nil {
  10. return nil, fmt.Errorf("parse config: %w", err)
  11. }
  12. cfg.MaxConns = 20
  13. cfg.MinConns = 2
  14. pool, err := pgxpool.NewWithConfig(ctx, cfg)
  15. if err != nil {
  16. return nil, fmt.Errorf("create pool: %w", err)
  17. }
  18. if err := pool.Ping(ctx); err != nil {
  19. return nil, fmt.Errorf("ping db: %w", err)
  20. }
  21. return pool, nil
  22. }