From e40a119bdcfa5c390cba0679780de67aba7f9217 Mon Sep 17 00:00:00 2001 From: Cipher Vance Date: Thu, 18 Sep 2025 20:06:36 -0500 Subject: [PATCH] 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 --- config/database.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 config/database.go diff --git a/config/database.go b/config/database.go new file mode 100644 index 0000000..be6ad8e --- /dev/null +++ b/config/database.go @@ -0,0 +1,28 @@ +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 +}