SAN DIEGO — The San Francisco Giants are more than ready to get home after a shaky opening week on the road.

Hunter Pence and Buster Posey hit consecutive homers, and Johnny Cueto struck out seven in seven strong innings for the Giants, who beat the San Diego Padres 5-3 Sunday to prevent a three-game sweep.

The Giants snapped a four-game losing streak. They won only twice during the season's opening week.

These being the Giants, there was a touch of ninth-inning drama as new closer Mark Melancon put two runners on with one out before getting Wil Myers to ground into a game-ending double play.

Melancon hadn't pitched since opening day, when he blew a save and took the loss at Arizona. The Giants hadn't won since the second game of the year.

"It's good for him to get the first one," manager Bruce Bochy said. "It got a little scary there, I guess you could say, but he made a pitch there when he had to. This was a big game for us. The last thing we wanted to do was get swept here and go 1-6 on this road trip. So, good for the guys. This was a big game for them."

Melancon, signed as a free agent in the offseason, allowed a one-out single to Manuel Margot and walked Travis Jankowski before getting Myers to hit into a double play.

Melancon pumped his fist after the final out.

"It was tough because we were losing," he said. "Today was about getting a win at all costs. I think we can build off today."

Pence hit a two-run homer to left field with one out in the third off Clayton Richard (1-1), and Posey followed with an opposite-field shot to right to give the Giants a 5-0 lead. It was the first time this season the Giants hit back-to-back homers.

Cueto (2-0) was working on a three-hit shutout when he walked Jankowski with one out in the sixth and then allowed Myers' homer into the second-floor balcony on the Western Metal Supply Co. Building in the left-field corner. It was Myers' second of the season.

The homers were the first of the year for Pence and Posey.

"They really hit two pretty good pitches out of the ballpark today," Padres manager Andy Green said. "Pence got down below the zone and picked up a slider off the dirt. Posey is about as professional a swing as you see in Major League Baseball. That really wasn't a bad pitch."

Cueto held the Padres to two runs and five hits with three walks, snapping his two-game skid at Petco Park. He was the loser in the All-Star Game on July 12 after allowing home runs by Eric Hosmer and Salvador Perez, and then lost to the Padres five days later.

Bochy was going to take Cueto out after six but the right-hander talked the manager into one more inning.

"I wasn't thinking about being swept," Cueto said. "I just told myself to go out there and do my job. But thank God everything worked out and we didn't get swept."

The Giants scored two runs on a crazy play in the second inning. With runners on first and second and one out, Chris Marrero singled to right to bring in Posey. Hunter Renfroe's throw was offline and hit Myers' glove before the first baseman accidentally kicked it into the Padres' dugout to allow Brandon Crawford to score. Renfroe was charged with an error.

San Diego's Yangervis Solarte greeted reliever Derek Law with a homer leading off the eighth, his second. Solarte had three hits.

Richard allowed five runs, four earned, and five hits in six innings. He was coming off a 4-0 win at the Los Angeles Dodgers on Tuesday night, when he pitched eight innings of five-hit ball and struck out five.

SALUTING THE COLONEL

The Padres unveiled a memorial to late Hall of Fame broadcaster Jerry Coleman in the press dining room at Petco Park. The large tribute case contains several mementos, including a New York Yankees cap and the flight jacket he wore as a Marine pilot in the Korean War. Coleman was the only major leaguer to see combat in two wars, flying a total of 120 missions in World War II and Korea. The voice of the Padres for 42 years, the former Yankees second baseman was 89 when he died on Jan. 5, 2014.

TRAINER'S ROOM

Padres RHP Trevor Cahill, who had been scheduled to start tonight at Colorado, was placed on the 10-day disabled list, retroactive to Thursday, with a strained lower back. The Padres recalled RHP Zach Lee from Triple-A El Paso to take his roster spot.

UP NEXT

Giants: LHP Matt Moore (0-1, 5.06 ERA) is scheduled to start the home opener this afternoon against RHP Taijuan Walker (1-0, 6.00) and the Arizona Diamondbacks.

Padres: RHP Jarred Cosart will start in place of Cahill in the opener of a three-game series at Colorado. Cosart lost a spring training competition with Cahill and began the season in the bullpen.