InitPostgresql connect postgresql
()
| 13 | |
| 14 | // InitPostgresql connect postgresql |
| 15 | func InitPostgresql() *sgorm.DB { |
| 16 | postgresqlCfg := config.Get().Database.Postgresql |
| 17 | opts := []postgresql.Option{ |
| 18 | postgresql.WithMaxIdleConns(postgresqlCfg.MaxIdleConns), |
| 19 | postgresql.WithMaxOpenConns(postgresqlCfg.MaxOpenConns), |
| 20 | postgresql.WithConnMaxLifetime(time.Duration(postgresqlCfg.ConnMaxLifetime) * time.Minute), |
| 21 | } |
| 22 | if postgresqlCfg.EnableLog { |
| 23 | opts = append(opts, |
| 24 | postgresql.WithLogging(logger.Get()), |
| 25 | postgresql.WithLogRequestIDKey("request_id"), |
| 26 | ) |
| 27 | } |
| 28 | |
| 29 | if config.Get().App.EnableTrace { |
| 30 | opts = append(opts, postgresql.WithEnableTrace()) |
| 31 | } |
| 32 | |
| 33 | // add custom gorm plugin |
| 34 | //opts = append(opts, postgresql.WithGormPlugin(yourPlugin)) |
| 35 | |
| 36 | dsn := utils.AdaptivePostgresqlDsn(postgresqlCfg.Dsn) |
| 37 | db, err := postgresql.Init(dsn, opts...) |
| 38 | if err != nil { |
| 39 | panic("init postgresql error: " + err.Error()) |
| 40 | } |
| 41 | |
| 42 | sgorm.SetDriver("postgresql") |
| 43 | return db |
| 44 | } |