It pays to be an ex-cop in New York state...

The NYPD must cough up the names of almost all of its retirees and their pensions so the information can be published online by a watchdog group that tracks tax spending, a Manhattan judge has ruled.

The order marked a major victory for the conservative Empire Center for Public Policy, which previously obtained data for seven years worth of NYPD pension amounts in 2014 — but without the identities of the ex-cops receiving the money.

Those statistics show that one retiree, who left the job in 2010, pulled down an astounding $474,511 a year in fiscal 2014, the most recent year available. The unidentified ex-cop’s retirement benefit ranked No. 2 on a 2015 list of the highest public pensions in the state.

The second-highest NYPD pension, scored by an unidentified retiree collecting $323,357 a year, was No. 7 on the statewide list compiled by Newsday.

NYPD pensions are calculated based on a cop’s length of service and average annual total income, including overtime, during either a three- or five-year period, according to the city Police Pension Fund’s website.

In her ruling Monday, Manhattan Supreme Court Justice Melissa Crane wrote, “Public employees do not enjoy the same privacy rights as private-sector employees.”

Crane also noted that “[c]oncerned taxpayers have played a crucial role in identifying pension abuse,” citing media reports that revealed former cops scamming disability pensions based “on information the public provided.”

The justice’s ruling exempted the Police Pension Fund from releasing the names of former undercover officers but ordered it to turn over the remaining data for fiscal year 2017.

A spokesman for the city Law Department, which represents the pension fund, said, “We are studying the opinion and reviewing our legal options.”