The surgery has not yet been scheduled, but the Giants said the All-Star outfielder and the team are leaning toward the procedure.
"The good news is there's going to be a lot of season left to play when I come back," Pence said Friday before the NL West leaders played the Cardinals on Friday night. "So there's a lot to look forward to. That is exciting."
Pence, 33, said three doctors weighed in on the injury.He was told the surgery was fairly routine.
"It just makes sense to be 100 percent certain," Pence said. "It's also supposed to get you back quicker."
Pence said he felt the hamstring pop while he running to first base trying to beat out an infield grounder on Wednesday. He was helped off the field.
Pence said he was told he had a 50-50 chance of returning if he skipped surgery and just did rehabilitation work. But if he did that and reinjured himself, the recovery time would be longer.
Manager Bruce Bochy announced the extent of Pence's injury Friday, saying the Giants already have discussed possible trade options.
"We've got to get creative here," Bochy said. "I can't tell you what's going to happen or who's going to get the most playing time. If you look at our lineup as a group, we can get a little better."
Added Giants first baseman Brandon Belt: "This sucks, basically. He's obviously a big part of this team. It's going to be hard to replace somebody like him for two months."
It's the second right hamstring injury in two weeks for Pence. He topped the team with 36 RBIs and was second with seven home runs going into the series at St. Louis.
Information from The Associated Press was used in this report.