dataRT/data/postgres/postgres.go

34 lines
750 B
Go
Raw Normal View History

2025-09-19 16:17:46 +08:00
package postgres
import (
"datart/config"
"fmt"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var client *gorm.DB
func init() {
postgresConfig := config.Conf().PostgresConf("default")
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s",
postgresConfig.GetHost(), postgresConfig.GetUser(), postgresConfig.GetPassword(),
postgresConfig.GetDBName(), postgresConfig.GetPort(), postgresConfig.GetSSLMode(),
postgresConfig.GetTimeZone())
2025-11-06 21:09:50 +08:00
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{SkipDefaultTransaction: true})
2025-09-19 16:17:46 +08:00
if err != nil {
panic(err)
}
client = db
}
// close postgres default client
func Close() error {
db, err := client.DB()
if err != nil {
return err
}
return db.Close()
}