A retired police commander kept a stunning secret for more than two decades: Before joining the NYPD, he peddled crack, tried to murder a fellow drug dealer and was close pals with a notorious cop killer.

Corey Pegues — who collects a $135,000 tax-free line-of-duty disability pension for a back injury — revealed his sordid past on the “Combat Jack’’ podcast Aug. 13, in a blatant attempt to hype his yet-to-be-published book.

Pegues, 45, who retired in April 2013 after more than 20 years on the force, is untouchable because the statute of limitations has expired on his crimes, most of which he confessed to committing in his teens and 20s in Queens.

The former deputy inspector bragged that as a young gangbanger, he made enough money selling crack to buy 75 pairs of expensive sneakers as well as gold jewelry now worth some $30,000.

He chillingly admitted pulling the trigger twice on a rival drug dealer, saying the man would have been killed if the gun hadn’t misfired. “I was maybe like 17,’’ he told the interviewer. “I was like, ‘Yo, I’m going to murder him’ . . . It was all about street cred.’’

In his most shocking revelation, Pegues — who is married with children, including a daughter in the NYPD — bragged that he was a close pal of David McClary, the coldblooded triggerman who fired five shots into the head of rookie Officer Edward Byrne as the cop sat in a patrol car guarding the home of a witness in 1988.

He had advice for aspiring criminals: “The biggest asset I learned from the streets is don’t trust nobody. Everybody’s a threat. I don’t trust my mother,’’ he said in the interview. “You can’t trust nobody.”

The NYPD declined to comment. A senior police official, though, said, “I doubt any of it is true — but if it is, shame on him.”

Other cops had no doubts.

Rumors about Pegues’ gangsta past had circulated for years, including how he once sported a tattoo — “Thug’s Life” — on his neck that he had removed.

Any of his past crimes would have disqualified him from joining the NYPD, had those vetting his application been aware of them. But Pegues was able to sign up without a hitch.

He rose steadily in the ranks and became a deputy inspector who commanded the 67th Precinct in East Flatbush.

He also served as an adjunct professor at Monroe College in The Bronx, teaching criminal justice for more than four years, according to his LinkedIn résumé.

During the interview, Pegues boasted of his loyalty in the early 1980s to crack kingpin Kenneth “Supreme” McGriff.

McGriff, who ran the feared “Supreme Team,” was sentenced to life without parole following his 2007 Brooklyn federal court conviction for racketeering, drug trafficking, murder-for-hire and other crimes, records show.

What Pegues most feared was that his colleagues in the NYPD would learn about his friendship with a cop killer.

“You’re talking about the most infamous murder in the history of the Police Department with [Officer] Eddie Byrne, and I have to hide that relationship for 20-something years,” he said.

“If they [fellow cops] would have had any inclination that David McClary was my man . . . I probably would have had a hard time,’’ he said in a monumental understatement. “It would have been a problem.”

The president of Pegues’ union was outraged. “I was shocked and disgusted hearing the criminal conduct bragged about,” Roy Richter of the NYPD Captains Endowment Association told The Post. “If true, it is a complete betrayal of the oath and the sacrifice police officers make to society.”

Other cops took to The Rant, an Internet bulletin board popular with officers, to trash Pegues.

“This guy was promoted to deputy inspector and he openly admits to carrying an illegal firearm, selling drugs and even attempting homicide,’’ wrote one. “How embarrassing to the department.”

Additional reporting by Leonica Valentine