Houston’s Justin Maxwell is caught stealing second base by Danny Espinosa. (Greg Fiume/Getty Images)

About three hours before his first pitch Tuesday night at Nationals Park, Gio Gonzalez pecked at his iPad and grooved to the Motown blasting from the Washington Nationals’ clubhouse stereo. As Marvin Gaye’s “Let’s Get It On” blared, he stood and danced. Gonzalez chattered at any teammate who passed. He playfully stole a plate of food from Wilson Ramos.

When most pitchers would be silently scowling, Gonzalez smiled. The Nationals needed Gonzalez’s electric left arm to beat the Houston Astros, 1-0, and they needed his relaxed vibes to survive the scare closer Brad Lidge provided in the ninth. Gonzalez fired seven scoreless innings to continue the Nationals’ rotation’s remarkable season, and Lidge wiggled free of a two-on, no out jam in the ninth to make it stand up.

The Nationals improved to 9-3, the third-best record in the majors, and extended their lead in the National League East despite another heart-pounding performance. Already, the Nationals have won games 3-2, 2-1 (twice) and 1-0. In four of their victories, they scored the winning run in the eighth inning or later.

“It’s not easy on me,” Manager Davey Johnson said. “We actually like it when it’s close like that. It puts more on every at-bat, every pitch. We’re playing games kind of like in a pennant race, and that’s good for later on in the season.”

Can they keep it up?

“I think it is sustainable for this team,” Lidge said. “For the simple fact that our rotation is so good.”

Tuesday, Gonzalez had scant need for relievers, outfielders or run support. He allowed two hits and two walks over seven scoreless innings. He struck out eight, let only three balls escape the infield and retired 13 straight at one point. After Jed Lowrie, the second batter of the game, flied out to center, the Astros did not hit another ball to the outfield in the air off Gonzalez.

“He was hitting my glove the whole night,” catcher Jesus Flores said. “His curveball was untouchable.”

After his rocky first Nationals appearance, at Wrigley Field, Gonzalez has showed precisely why Washington traded four prospects to the Oakland Athletics for him. In his two home starts, Gonzalez has allowed no runs in 14 innings on four hits and two walks while striking out 15.

In the clubhouse or on the mound, Gonzalez would not stop being himself: a serial grinner, an unrepentant goofball and one of the most dominant left-handed pitchers in baseball. He smiled after creaming a fly ball to the center field warning track. Afterward, he wore a Capitals T-shirt.

“For me, I always try to keep it loose,” Gonzalez said. “If I show tension, or if I show I’m tightening up a little bit, that’s going to make it worse for the position players and the hitter. My job is to always keep it smiling.”

The one time Gonzalez found trouble, he made his way out of it. In the sixth inning, with the Nationals leading, 1-0, Jose Altuve, the Astros’ pint-size second baseman, led off with a groundball single up the middle. Altuve moved to second on a sacrifice bunt. In the process of walking Lowrie, Gonzalez chucked a wild pitch.

For the first time, with No. 3 hitter J.D. Martinez coming to the plate, the Astros had advanced a runner past second base. Flores walked to the mound and told Gonzalez, “Don’t give in.”

Gonzalez threw Martinez a 1-2 curveball, and he hit an easy groundball to shortstop Ian Desmond. He had dashed Houston’s lone threat.

“Nasty,” said Jayson Werth. “His ball was moving all over the place.”

The Astros sent their own nasty left-hander to the mound. Wandy Rodriguez, Houston’s ace, shut down the Nationals, yielding only five hits, all singles. With two outs in the fourth, Werth flied to shallow center. Maryland alum and former National Justin Maxwell charged and slid, but the ball deflected off the palm of his glove. Werth had sprinted out of the box, and when the ball trickled away from Maxwell he bolted to second.

“I knew it was a tweener, so I’m hustling out of the box,” Werth said. “It’s a double all the way if it drops. My mind was made up when I hit it.”

Adam LaRoche followed with a popup behind shortstop. Left fielder Martinez and shortstop Lowrie couldn’t decide who should catch it, and neither did. The ball plopped to the turf, and Werth cruised home with the Nationals’ only run.

After 91 pitches, Gonzalez ceded to Tyler Clippard, who pitched around a leadoff bunt single to maintain the lead. Lidge entered for the ninth and immediately put a scare into all 17,886 in the park. Lowrie doubled into the right field corner, and Lidge walked Martinez on four pitches. Like that, the go-ahead run stood on base with no outs.

In right field, Werth relaxed. He had seen Lidge’s escape act when they played together in Philadelphia. “I was calm and collected out there,” Werth said. “I knew he had it all the way.”

On the mound, Lidge told himself, “Trust your stuff.” He knew he didn’t have his best slider, that it lacked the late, sharp sink. He still believed he would find a way.

Carlos Lee popped weakly to center. Chris Johnson, after taking three balls, hit a rocket into Rick Ankiel’s glove. Travis Buck grounded out to first. Lidge had escaped, and Gonzalez and the rest of the Nationals could exhale.

“I’m sure I gave a lot of people a heart attack today,” Lidge said. “I love being in that situation. I want to have a 1-2-3 inning, but sometimes they don’t go that way. When they’re not going easy, you’ve got to battle through that inning and make sure the run doesn’t score.”

The Nationals cared only about the end result. Gonzalez popped out of the dugout, smiling as ever. As they passed each other in the postgame handshake line, Werth leaned toward Lidge and told him, “Never a doubt.”