Schafer socked a two-run homer and added an RBI groundout while Gonzalez delivered a bases-clearing double for Houston, which posted its first win since July 16.
Scott Moore chipped in with a pair of run-scoring singles in the victory -- only the third time in 28 games for Houston.
Lucas Harrell (8-7) yielded two runs on five hits over 5 2/3 innings to notch the decision, walking three and striking out five.
Pirates starter James McDonald (10-5) was touched for five runs on six hits over five full frames. The right-hander couldn't find the plate, issuing seven walks.
Garrett Jones socked a two-run homer and Neil Walker supplied a pair of RBI for the second-place Pirates, who came into the contest trailing National League Central-leading Cincinnati by two games.