mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-11-28 07:27:03 +08:00
27 lines
536 B
Go
27 lines
536 B
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func Open(ctx context.Context, url string) (*pgxpool.Pool, error) {
|
|
cfg, err := pgxpool.ParseConfig(url)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("解析数据库连接地址: %w", err)
|
|
}
|
|
|
|
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("创建数据库连接池: %w", err)
|
|
}
|
|
if err := pool.Ping(ctx); err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("连接数据库: %w", err)
|
|
}
|
|
|
|
return pool, nil
|
|
}
|