package db import ( "fmt" "github.com/spf13/viper" "gorm.io/driver/postgres" "gorm.io/gorm" "log" ) func connectPostgres() *gorm.DB { dsn := createPostgresDSN() connection, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { log.Fatal("Could not connect to Database", err) } return connection } func createPostgresDSN() string { dsn := fmt.Sprintf( "host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", viper.GetString("db.host"), viper.GetString("db.user"), viper.GetString("db.password"), viper.GetString("db.name"), viper.GetInt("db.port"), viper.GetString("db.sslmode"), viper.GetString("db.timezone"), ) return dsn }