Updated

The Mets started the game with a homer from Ruben Tejada, and even after coming up empty after loading the bases in their final three innings, were able to squeeze past San Francisco, 2-1, at AT&T Park.

The Mets had 19 base runners, coming on nine hits, nine walks and a hit batsman, but gave the Giants every opportunity to win Wednesday night. New York went 1-for-13 with men in scoring position and left 13 runners on base, but managed to take their second victory in this four-game series.

Jonathon Niese (8-5) gave up three hits, including a sixth-inning homer to Buster Posey, as the Mets posted just their fifth win in 19 games since the All-Star break. Niese lasted seven innings.

"He threw all his pitches for strikes. I thought his changeup was good tonight. After the first inning he was outstanding," Mets manager Terry Collins said.

Tejada and David Wright each had three hits for the Mets.

Hunter Pence, a day after being traded from the Phillies, started in right field for the Giants but went 0-for-4 with a pair of strikeouts in his new team debut.

Matt Cain (10-4) had control problems early and lasted five innings in suffering his second loss in his last 16 starts.

The Giants, losers in six of their last seven contests, remained a game ahead of the Dodgers for first place in the NL West.

"It's a tough one to lose because we dodged bullets all night," Giants manager Bruce Bochy said. "We didn't do much with the bats or created a lot of chances either."

Tejada hit the second pitch of the game over the wall in left. Tejada had gone more than 600 at-bats since his only other career homer, which came Sept. 5, 2010 at Wrigley Field.

Cain threw 30 pitches in the first inning and struggled again in the second, loading the bases with one out after consecutive singles by Josh Thole, Niese and Tejada. Jordany Valdespin grounded a ball to third base for a force play, resulting in New York's second run, and Wright fanned to end the inning.

Gregor Blanco saved a run in the top of the fourth. With Tejada on second base and two outs, Valdespin sent a fly ball to left-center field. Blanco extended his body to make a great diving catch.

Posey homered to left with two outs in the sixth.

The Mets had great chances to pad their lead in the seventh, eighth and ninth after loading the bases, but came up empty each time.

In the seventh, Javier Lopez entered from the bullpen to replace George Kontos and got Ike Davis to ground into a 6-4-3 double play.

In the eighth, the Mets essentially ran themselves out of a possible chance to add to their edge. Clay Hensley walked three batters in the inning and also pegged Mike Baxter with a pitch, but Jason Bay's base-running gaffe and a sparkling defensive play by second baseman Ryan Theriot helped the Giants escape.

Daniel Murphy and Bay walked before Baxter was hit by a pitch to fill the bases with nobody out. Thole then hit a ball to first baseman Brett Pill, who threw home for the force out. Posey's throw to first deflected off the left shoulder of Thole, and Thole was ruled out of the baseline and the Mets ended up having runners at first and second with two outs.

"That's a bad rule. Surely you have to come inside that line or you're going to break your leg," Collins said.

New York pulled off a double steal before pinch-hitter Justin Tuner walked to load the bases again. Brad Penny spelled Hensley and Tejada hit a sharp grounder to Theriot, who made a sparkling diving stop and threw to first to keep it a 2-1 game.

Wright singled, Davis grounded a double over third base and Murphy was intentionally walked, all with one out in the ninth to load the bases against Penny. Bay then hit a sharp liner back to Penny, who made the belt-high grab and threw to first for the double play.

Emotional as Penny was after making the double play, the Giants weren't able to turn the defensive momentum into offense in their final at-bat.

Bobby Parnell retired the side in order in the ninth for his fourth save of the season.

Game Notes

The Giants placed infielder Aubrey Huff on the 15-day disabled list with a right knee sprain...Niese snapped a four-start winless skid...Cain has allowed eight homers over his last six starts and has given up 16 overall this year after yielding just nine total last season.