2024-01-16 21:10:29 +02:00
|
|
|
package postgresql
|
|
|
|
|
|
|
|
import (
|
2024-01-18 18:48:27 +02:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2024-01-16 21:10:29 +02:00
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Database struct {
|
2024-01-18 18:48:27 +02:00
|
|
|
db *pgxpool.Pool
|
2024-01-16 21:10:29 +02:00
|
|
|
}
|
|
|
|
|
2024-01-18 18:48:27 +02:00
|
|
|
func New(user, password, addr, dbname string) (*Database, error) {
|
|
|
|
const op = "storage.postgresql.New"
|
|
|
|
|
|
|
|
pool, err := pgxpool.New(context.Background(), fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", user, password, addr, dbname))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
|
|
}
|
2024-01-16 21:10:29 +02:00
|
|
|
|
2024-01-18 18:48:27 +02:00
|
|
|
return &Database{db: pool}, nil
|
2024-01-16 21:10:29 +02:00
|
|
|
}
|