Files
go-migrate-db/internal/app/db/postgres.go
Kylesoda 438b6afa1c feat: add postgres connection helper
Add a thin wrapper around pgx to open and verify a Postgres connection from a URL or individual parameters.
2026-04-01 09:00:00 -05:00

29 lines
489 B
Go

package db
import (
"context"
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
)
func Connect(ctx context.Context, dbURL string) (*pgxpool.Pool, error) {
pool, err := pgxpool.New(ctx, dbURL)
if err != nil {
return nil, fmt.Errorf("unable to connect to database: %w", err)
}
if err := pool.Ping(ctx); err != nil {
pool.Close()
return nil, fmt.Errorf("unable to ping database: %w", err)
}
return pool, nil
}
func Close(pool *pgxpool.Pool) {
if pool != nil {
pool.Close()
}
}