Files
backend/internal/database/postgres.go
2025-06-18 09:01:14 +03:00

30 lines
611 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 (
"database/sql"
"log"
"os"
_ "github.com/jackc/pgx/v5/stdlib"
)
// Connect устанавливает соединение с базой данных PostgreSQL
func Connect() *sql.DB {
connStr := os.Getenv("DATABASE_URL")
if connStr == "" {
log.Fatal("DATABASE_URL environment variable is not set")
}
db, err := sql.Open("pgx", connStr)
if err != nil {
log.Fatalf("Unable to connect to database: %v\n", err)
}
if err = db.Ping(); err != nil {
log.Fatalf("Unable to ping database: %v\n", err)
}
log.Println("Successfully connected to PostgreSQL!")
return db
}