You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
801 B

  1. package initialize
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/initialize/internal"
  5. "gorm.io/driver/postgres"
  6. "gorm.io/gorm"
  7. )
  8. // GormPgSql 初始化 Postgresql 数据库
  9. // Author [piexlmax](https://github.com/piexlmax)
  10. // Author [SliverHorn](https://github.com/SliverHorn)
  11. func GormPgSql() *gorm.DB {
  12. p := global.GVA_CONFIG.Pgsql
  13. if p.Dbname == "" {
  14. return nil
  15. }
  16. pgsqlConfig := postgres.Config{
  17. DSN: p.Dsn(), // DSN data source name
  18. PreferSimpleProtocol: false,
  19. }
  20. if db, err := gorm.Open(postgres.New(pgsqlConfig), internal.Gorm.Config()); err != nil {
  21. return nil
  22. } else {
  23. sqlDB, _ := db.DB()
  24. sqlDB.SetMaxIdleConns(p.MaxIdleConns)
  25. sqlDB.SetMaxOpenConns(p.MaxOpenConns)
  26. return db
  27. }
  28. }