Files
backend/internal/database/postgres.go

32 lines
874 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package database
import (
"context"
"log"
"os"
"github.com/jackc/pgx/v5/pgxpool"
)
// Connect устанавливает соединение с базой данных PostgreSQL, используя пул соединений pgx.
func Connect() *pgxpool.Pool {
connStr := os.Getenv("DATABASE_URL")
if connStr == "" {
log.Fatal("DATABASE_URL environment variable is not set")
}
// Создаем пул соединений
pool, err := pgxpool.New(context.Background(), connStr)
if err != nil {
log.Fatalf("Unable to create connection pool: %v\n", err)
}
// Проверяем, что соединение действительно установлено
if err = pool.Ping(context.Background()); err != nil {
log.Fatalf("Unable to ping database: %v\n", err)
}
log.Println("Successfully connected to PostgreSQL using pgxpool!")
return pool
}