func GetDB(config JSONConfigurationDB) (*gorm.DB, error) {
	postgresDSN := fmt.Sprintf(
		DBString, config.Host, config.Port, config.Name, config.Username, config.Password)
	db, err := gorm.Open(DBDialect, postgresDSN)
	if err != nil {
		return nil, err
	}
	db.DB().SetMaxIdleConns(config.MaxIdleConns)
	db.DB().SetMaxOpenConns(config.MaxOpenConns)
	db.DB().SetConnMaxLifetime(time.Second * time.Duration(config.ConnMaxLifetime))
	return db, nil
}
GetDB to get PostgreSQL DB using GORM
package backend
import (
	"fmt"
	"time"
	"github.com/spf13/viper"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/postgres"
)
const (
	DBString = "host=%s port=%s dbname=%s user=%s password=%s sslmode=disable"
	DBDialect = "postgres"
	DBKey = "db"
)
type JSONConfigurationDB struct {
	Host            string `json:"host"`
	Port            string `json:"port"`
	Name            string `json:"name"`
	Username        string `json:"username"`
	Password        string `json:"password"`
	MaxIdleConns    int    `json:"max_idle_conns"`
	MaxOpenConns    int    `json:"max_open_conns"`
	ConnMaxLifetime int    `json:"conn_max_lifetime"`
}
func LoadConfiguration(file, key string) (JSONConfigurationDB, error) {
	var config JSONConfigurationDB
	viper.SetConfigFile(file)
	if err := viper.ReadInConfig(); err != nil {
		return config, err
	}
	dbRaw := viper.Sub(key)
	if err := dbRaw.Unmarshal(&config); err != nil {
		return config, err
	}
	return config, nil
}