Files
silo-server/internal/database/postgres.go
T

36 lines
907 B
Go

package database
import (
"context"
"fmt"
"github.com/Silo-Server/silo-server/internal/config"
"github.com/jackc/pgx/v5/pgxpool"
)
// NewPool creates a new PostgreSQL connection pool using the provided
// DatabaseConfig. It configures the pool with the specified maximum number of
// connections and verifies the connection by issuing a ping.
func NewPool(ctx context.Context, cfg config.DatabaseConfig) (*pgxpool.Pool, error) {
poolCfg, err := pgxpool.ParseConfig(cfg.URL)
if err != nil {
return nil, fmt.Errorf("parsing database URL: %w", err)
}
if cfg.MaxConnections > 0 {
poolCfg.MaxConns = int32(cfg.MaxConnections)
}
pool, err := pgxpool.NewWithConfig(ctx, poolCfg)
if err != nil {
return nil, fmt.Errorf("creating connection pool: %w", err)
}
if err := pool.Ping(ctx); err != nil {
pool.Close()
return nil, fmt.Errorf("pinging database: %w", err)
}
return pool, nil
}