They did it despite Melky Cabrera not being in the lineup.
Cabrera, the major league leader in hits and the MVP of last Tuesday night's All-Star game, took a temporary leave to attend the birth of his child in Florida. He caught a red-eye flight to Orlando following Saturday night's 3-2 extra-inning win over the Astros and is expected to rejoin the team in Atlanta on Tuesday.
"I think we've got enough depth in the lineup where we can get the job done," said Posey of his fifth three-hit game this season. "Obviously any time Melky's not in there we're going to miss him, though. I'm just trying to keep a good approach and have a plan every time I go up there."
San Francisco's first sweep of the Astros since 2010 increased its lead to 1½ games over the Los Angeles Dodgers in the NL West.
That's a far better position than the Giants were in when they stumbled into the All-Star break having dropped five of six. Now they head to Atlanta and Philadelphia trying to improve on their 20-24 road record.
"To get a sweep is hard to do, I don't care who you're playing," San Francisco manager Bruce Bochy said. "These games were hard-fought games. We didn't do a lot with their pitching the last couple of days. It really could have gone either way."
That certainly wasn't the case a month ago when Cain pitched a perfect game against the Astros on June 13 -- the first in franchise history.
Cain wasn't as dominant in the rematch but did just enough to win for the second time since that historic night. San Francisco's right-hander struck out six and walked one.
"I still needed to go out there and pitch," Cain said. "Most of all, we won last night in an exciting game and then the night before, so I wanted to keep that going."
Cain (10-3) snapped his personal three-game winless streak but didn't come close to duplicating his first outing when he retired 27 consecutive Houston hitters earlier this season.
He allowed five hits, made a throwing error that led to an unearned run and pitched with runners in scoring position most of the afternoon before leaving with one out in the seventh.
Four relievers combined to blank Houston over the final 2 2/3 innings. Santiago Casilla, who blew a save on Saturday and was booed when he entered the game with one out in the ninth on Sunday, retired two batters for his 23rd save.
Marwin Gonzalez had two hits and scored Houston's only run. The Astros fell to 1-10 in July.
"They are tough on us," said Houston starter Bud Norris following his team's second straight one-run loss to the Giants. "A lot of close games and we need to get over the hump."
Houston scored only five runs in the series while Astros hitters struck out 34 times in the three games.
That looked like it might be enough for Cain, who had retired 34 consecutive Houston hitters dating to 2011 before Gonzalez's leadoff double in the third.
Gonzalez, activated from the disabled list before the game, later scored on Cain's two-out throwing error.
Cain retired Scott Moore to get out of the two-on, two-out jam but wasn't as fortunate the next inning. Brian Bogusevic singled and scored on Chris Johnson's double to the gap in left center to tie the game 2-2.
The Giants regained the lead on Posey's single in the fifth, and Cain stuck around long enough to get the win.
Jeremy Affeldt replaced Cain with one out in the seventh and runners on the corner. He quickly retired Carlos Corporan on a foul out to the catcher then got pinch-hitter Justin Maxwell to fly out to right.
Norris, who grew up about an hour north of AT&T Park, allowed all three San Francisco runs and walked six. He has lost six consecutive decisions after beginning the year 5-1.
The Astros placed IF Jed Lowrie and C Jason Castro on the disabled list before the game. Lowrie injured his right ankle and leg during a collision at second base in the third inning of Saturday's game while continues to hindered by soreness in his right leg, the same one he had surgery on in March 2011. Houston also purchased the contract of Corporan from Triple-A Oklahoma City. ... San Francisco LHP Barry Zito (7-6) will pitch Tuesday's series opener in Atlanta. ... J.A. Happ (6-9) goes for Houston on Monday in San Diego.