SAN FRANCISCO — Jon Niese allowed one run in seven innings and Ruben Tejada hit his first home run in 629 at-bats as the Mets beat the Giants Wednesday night at AT&T Park, 2-1.

Niese (8-5) allowed a second-inning single to Marco Scutaro and then didn’t give up another hit until Buster Posey’s solo home run in the sixth.

Niese has a troubling stat line when it comes to rest. On regular (four days) rest, he went into Wednesday night 2-4 with a 6.90 ERA. With extra rest (five days or more), he’s 5-1 with a 2.08 ERA.

Wednesday night’s start — on regular rest — bucked the trend. Niese allowed three hits overall. He walked two and struck out five.

Tim Byrdak and Jon Rauch combined for a scoreless eighth and Bobby Parnell pitched a perfect ninth to earn his fourth save.

“We pitched very well tonight,” manager Terry Collins said. “That was the difference.”

advertisement | advertise on newsday

The Mets won despite going 1-for-13 with runners in scoring position and leaving 13 men on base. They also loaded the bases in each of the final three innings and scored in none of them.

“This game is an incredible game,” Niese said. “If anybody’s got it figured out, let me know.”

When asked how many runs the Mets should have scored, Collins said, “About nine.”

Tejada (3-for-4, walk) led off the game with a home run off Matt Cain. It was Tejada’s first of the season, first in his career as a leadoff man and first since Sept. 5, 2010.

The Mets made it 2-0 in the second. They loaded the bases on three one-out singles, including a liner by Niese on a fake-bunt-swing-away play that nearly beheaded charging third baseman Scutaro. Jordany Valdespin drove in the run with a forceout grounder to third.

Mets videos

Cain (10-4) went five innings and allowed two runs and seven hits. He walked three and struck out five.

The Mets loaded the bases twice in the eighth inning, but did not score. Josh Thole bounced into what could have been a 1-2-3 double play, but the throw from Posey hit him on the shoulder and went into rightfield. But hold on. Plate umpire Dale Scott called Thole out for getting hit while out of the baseline. Replays backed the call.

“That’s a bad rule,’’ Collins said. “Unless you put first base outside that white line, that’s a bad rule . . . Dale was right. The call was absolutely correct. He’s inside the bag. But you’ve got to be there to touch it.”

The Mets reloaded the bases (they had three walks, a hit by pitch and three stolen bases in the inning). But Ryan Theriot robbed Tejada of a two-run single by throwing him out after a diving stop of his grounder to the second-base hole.

The Mets also loaded the bases in the ninth. Jason Bay (0-for-4, walk) lined into a double play on a ball caught by pitcher Brad Penny.