A man fatally shot himself in the head while riding in the back of a yellow cab on the Upper West Side early Sunday morning, police said.

The 30-year-old man got into the taxi at about 2 a.m. near the corner of 93rd Street and Amsterdam Avenue, then pulled out a gun and shot himself once in the head while sitting in the backseat of the car, authorities said.

The driver pulled over and called 911 after the shooting, cops said.

Medics responded to the scene and rushed the man to Mount Sinai St. Luke’s Hospital, where he was pronounced dead, authorities said.