You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.0 KiB
Go

package database
import (
"context"
"log/slog"
"time"
"git.runcible.io/learning/pulley/internal/config"
"github.com/jackc/pgx/v5/pgxpool"
)
// https://medium.com/@neelkanthsingh.jr/understanding-database-connection-pools-and-the-pgx-library-in-go-3087f3c5a0c
// pgxpool.New implicitly creates a dbConfig from the database uri params which can include connection params
// newPgxConfig creates a pgxpool Config from the application service config
// func newPgxConfig(cfg config.ServiceConfig) (pgxpool.Config, error) {
// dbconfig, err := pgxpool.ParseConfig(cfg.DatabaseUri)
// }
// OpenPgPool creates a db pool and tests connectivity
func OpenPgPool(ctx context.Context, cfg config.ServiceConfig) (*pgxpool.Pool, error) {
pool, err := pgxpool.New(ctx, cfg.DatabaseUri)
if err != nil {
return nil, err
}
timoutCtx, close := context.WithTimeout(ctx, 5*time.Second)
defer close()
if err := pool.Ping(timoutCtx); err != nil {
slog.Error("Error in attempting first postgres database connection")
pool.Close()
return nil, err
}
return pool, nil
}