Skip to content

Commit

Permalink
update: 新增pgsql的sslmode配置,默认disable,可设置值:disable / allow / prefer / r…
Browse files Browse the repository at this point in the history
…equire / verify-ca / verify-full
  • Loading branch information
zodial committed Dec 11, 2024
1 parent 3c54b65 commit e316a31
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions bootstrap/providers/postgresql/postgresql_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"strings"
)

// PostgresqlProvider @Bean("postgresql")
Expand Down Expand Up @@ -39,6 +38,7 @@ func (p *PostgresqlProvider) Init() {
username := config.GetString("username")
password := config.GetString("password")
dbname := config.GetString("database")
sslmode := config.GetString("sslmode", "disable")
timezone := config.GetString("timezone", "Asia/Shanghai")

gConf := &gorm.Config{
Expand All @@ -52,16 +52,12 @@ func (p *PostgresqlProvider) Init() {
gConf.Logger.LogMode(logger.LogLevel(logrus.DebugLevel))
}

dsnStr := fmt.Sprintf("host=%v user=%v password=%v dbname=%v port=%v sslmode=require TimeZone=%v", host, username, password, dbname, port, timezone)
dsnStr := fmt.Sprintf("host=%v user=%v password=%v dbname=%v port=%v sslmode=%v TimeZone=%v", host, username, password, dbname, port, sslmode, timezone)
dsn := postgres.Open(dsnStr)
db, err := gorm.Open(dsn, gConf)
if err != nil {
dsn = postgres.Open(strings.Replace(dsnStr, "require", "disable", 1))
db, err = gorm.Open(dsn, gConf)
if err != nil {
logrus.Error("postgresql 链接错误", err)
panic(err)
}
logrus.Error("postgresql connect error", err)
panic(err)
}
p.dbs[name.(string)] = db
}
Expand Down

0 comments on commit e316a31

Please sign in to comment.