22 lines
542 B
Go
22 lines
542 B
Go
package database
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
||
|
|
)
|
||
|
|
|
||
|
|
func Open(ctx context.Context, databaseURL, applicationName string) (*pgxpool.Pool, error) {
|
||
|
|
poolConfig, err := pgxpool.ParseConfig(databaseURL)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("parse database configuration: %w", err)
|
||
|
|
}
|
||
|
|
poolConfig.ConnConfig.RuntimeParams["application_name"] = applicationName
|
||
|
|
pool, err := pgxpool.NewWithConfig(ctx, poolConfig)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("create database pool: %w", err)
|
||
|
|
}
|
||
|
|
return pool, nil
|
||
|
|
}
|