Pittsburgh starting pitcher James McDonald produced the Pirates' only hit, a two-out single to left in the sixth.
Cain (1-0) threw 106 pitches in San Francisco's home opener, did not walk a batter and struck out 11. Cain signed a six-year, $127.5 million contract extension this month.
San Francisco catcher Buster Posey, playing at home for the first time since suffering a broken leg in a home plate collision last May, doubled in the Giants' first run in the opening inning.
Aubrey Huff had an RBI single later in the first and also contributed a two-run homer in the eighth.
McDonald (0-1) allowed three runs on six hits in 5 2/3 to take the loss.
Prior to the game, the Giants honored Bryan Stowe, the San Francisco fan who was severely beaten outside Dodger Stadium last year. Stowe's 13-year-old son threw out the first pitch.
San Francisco has won three out of four since an 0-3 start. Pittsburgh has dropped four straight.