Skip to content

Commit

Permalink
update: postgresql兼容ssl链接
Browse files Browse the repository at this point in the history
  • Loading branch information
zodial committed Nov 20, 2024
1 parent e3cb2ab commit 3c54b65
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions bootstrap/providers/postgresql/postgresql_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"strings"
)

// PostgresqlProvider @Bean("postgresql")
Expand Down Expand Up @@ -51,11 +52,16 @@ func (p *PostgresqlProvider) Init() {
gConf.Logger.LogMode(logger.LogLevel(logrus.DebugLevel))
}

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

0 comments on commit 3c54b65

Please sign in to comment.