HOUSTON -- For the first time in a while, the Houston Astros could enjoy a game.
"I'm glad it's over with, but those guys in there made it a lot better because of the way they played," Houston manager Brad Mills said.
Gonzalez, who ended an 0-for-16 streak, agreed with Mills.
"It was hard for us to win a game," Gonzalez said. "We finally got a game, and we all played hard. That's all we can do."
Houston, a big league-worst 35-68, won for only the third time in 28 games.
The skid matched Kansas City and the Chicago Cubs for the longest in the major leagues this year. It broke the Astros' previous record of 11, set in 1995 and matched over the end of the 2009 season and start of 2010, STATS LLC said.
Lucas Harrell (8-7) allowed two runs and five hits in 5 2/3 innings, his first victory in five starts since a six-hit shutout against San Diego on June 27.
Harrell, who improved to 6-1 at home this season, said he didn't feel great about the way he threw the ball, but he made big pitches when he needed to.
"There were a couple things that I did wrong," Harrell said. "I didn't feel like my body was in sync with my arm. I was missing sporadically."
Said Mills: "When we have played so well all year long at home and go through a rough homestand like this and then win, that is a big breath of fresh air to be able to do this."
James McDonald (10-5) walked a career-high seven in five innings, allowing five runs and six hits. He has lost consecutive decisions for the first time this season and has an 8.71 ERA in his last four starts.
"The last couple outings have been rough," McDonald said. "I think every year I've pitched, I've been in the same situation where you have to fight your way out of a couple of rough starts. I've got some work ahead of me."
Pirates manager Clint Hurdle said McDonald worked hard, but his delivery and release point were inconsistent.
"This guy was lights-out," Hurdle said. "This is his second full season in the major leagues. This isn't really that weird in the context of what goes on -- he's hit a wall. He was hard to hit, but then you're getting to a point where you're just trying to throw strikes and you're not painting locations like we've seen him do."
J.D. Martinez's sacrifice fly put Houston ahead in the third.
Garrett Jones hit a two-run homer in the fourth, a drive into the right-field upper deck. The Pirates have homered in a team-record 19 straight road games.
Schafer's two-run homer gave the Astros the lead for good in the bottom half. Martinez tripled in the fifth on a ball misplayed by Jones in right and then scored on Scott Moore's single. After catcher Michael McKenry made a diving tag at the plate on Moore on a ball in front of the plate, Schafer hit an RBI groundout for a 5-2 lead.
Moore singled in a run in the sixth off Kevin Correia. Neil Walker's RBI double off Fernando Rodriguez cut the gap to 6-3 in the seventh before Gonzalez hit a three-run double down the left-field line with two outs.
After the game, Houston optioned IF Brian Bixler to Triple-A Oklahoma City and recalled INF Brett Wallace. ... Scott Moore started at third base in place of Johnson, and newly acquired Steve Pearce started at first base. Pearce was claimed off waivers from Baltimore on Saturday. ... Pittsburgh has 108 home runs this season, one more than last year. ... RHP Bud Norris, 5-8 with a 5.05 ERA this season, will start for the Astros on Monday at Milwaukee against RHP Marco Estrada, who is 0-4 with a 4.52 ERA. ... LHP Erik Bedard, 5-11 with a 4.32 ERA, is to start for Pittsburgh on Monday at the Cubs.