rideaware-api/config/database.go
Cipher Vance e40a119bdc feat: add database configuration for PostgreSQL
- Create config/database.go with GORM PostgreSQL connection
- Support environment-based database configuration
- Add connection logging and error handling
2025-09-18 20:06:36 -05:00

28 lines
583 B
Go

package config
import (
"fmt"
"log"
"os"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func InitDB() *gorm.DB {
host := os.Getenv("PG_HOST")
port := os.Getenv("PG_PORT")
database := os.Getenv("PG_DATABASE")
user := os.Getenv("PG_USER")
password := os.Getenv("PG_PASSWORD")
// Try with quoted password
dsn := fmt.Sprintf(`host=%s port=%s user=%s password='%s' dbname=%s sslmode=disable`,
host, port, user, password, database)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatal("Failed to connect to database:", err)
}
return db
}