// InitDBopens a connection to the database.
func OpenDB() error {
	var err error
	sqlDB, err = db.Open(pgConnFlag)
	if err != nil {
		return err
	}

	if err := sqlDB.Ping(); err != nil {
		sqlDB.Close()
		sqlDB = nil
		return err
	}
	lg.Infoln("Successfully connected to the database")
	return nil
}
Exemple #2
0
// InitDB opens a connection to the database.
func InitDB() error {
	var err error
	sqlDB, err = db.Open(*pgConnString)
	if err != nil {
		return err
	}

	if err := sqlDB.Ping(); err != nil {
		sqlDB.Close()
		sqlDB = nil
		return err
	}
	sqlDB.SetMaxIdleConns(*pgMaxIdleConnections)
	sqlDB.SetMaxOpenConns(*pgMaxOpenConnections)
	lg.Infoln("Successfully connected to the database")
	return nil
}