18 lines
465 B
Go
18 lines
465 B
Go
// Package database 负责 PostgreSQL 连接、迁移和演示数据初始化。
|
|
package database
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
// Connect 建立 PostgreSQL 连接池;数据库地址必须通过环境变量提供。
|
|
func Connect(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
|
|
if databaseURL == "" {
|
|
return nil, errors.New("缺少 DATABASE_URL 环境变量")
|
|
}
|
|
return pgxpool.New(ctx, databaseURL)
|
|
}
|