President Trump’s key immigration rule change, to discourage immigrants from relying on public health programs, food stamps, housing assistance and other welfare, got derailed Friday by three federal judges. Their decisions, blocking Trump’s rule from going into effect, are wins for Democrats and immigration lobbyists. America’s poor are the losers.

Our country needs to take care of our own needy first.

Trump’s proposed rule would make it harder for immigrants who use welfare to qualify for green cards and give preference to those who fend for themselves. The rule would also require visa applicants to show they won’t use welfare if they’re let in the country.

Manhattan federal Judge George Daniels blasted Trump’s rule as an “exclusion in search of a justification,” implying it was a racist ploy to keep out foreigners.

Daniels has his facts wrong. There are already over 300,000 families in New York on Housing Authority wait lists, and over 4 million people nationwide waiting for public housing or housing vouchers. Some suffer in squalor on the street.

Welcoming more people into this country who can’t keep a roof over their head without assistance is insanity.

Public opinion is on the president’s side. A Harvard/Harris poll shows 60% of voters agree that immigrants likely to rely heavily on welfare should be denied permanent residence. That’s what Trump’s rule boils down to.

Daniels mistakenly claims there’s “absolutely no support in the history of US immigration law” for it. He needs a refresher course. In 1882, as immigration soared, Congress voted to exclude “any person unable to take care of himself or herself without becoming a public charge.” That law is still on the books.

In 1999, President Bill Clinton redefined “public charge” to include only those receiving cash welfare payments, not health care, food stamps or housing help.

Clinton’s rule contradicts American values, not Trump’s.

National Academy of Sciences research shows that the average first-generation immigrant costs state and local government $1,600 per year, including public education and safety-net programs, minus taxes paid. Newcomers who don’t use safety-net programs cost less, a benefit to taxpayers. Yet New York Attorney General Letitia James took the lead challenging Trump’s rule. It makes you wonder who she’s serving. Definitely not taxpayers.

Fortunately, Friday’s decisions almost certainly will be reversed on appeal. Meanwhile, Trump has announced another immigration rule — this one to correct an outrage that gives immigrants a better deal on health insurance than US citizens get.

The Affordable Care Act allows legal newcomers to enroll in ObamaCare on arrival — no waiting. If they’re poor, they get a free ride, including having Uncle Sam pick up co-pays and deductibles. Even immigrants over age 65 get this benefit — an invitation to bring over ailing Grandma.

No such cushy deal for Americans. In 14 states, 2 million who don’t qualify for Medicaid because they earn too much also don’t qualify for ObamaCare because they earn too little. Because they’re American, they’re caught in the middle and get nothing — no insurance at all. If they were immigrants, they could enroll in ObamaCare. The rules for immigrants are more lenient.

Another outrage: Some 2.5 mill­ion middle-class people dropped ObamaCare in the last two years because they can’t afford it. While they remain uninsured, their tax dollars are buying plans for newcomers to the US.

As global migration soars, nearly every developed country has adopted rules requiring immigrants to be self-sufficient. Denmark, Germany and Austria bar them from using welfare. Finland and Belgium require proof of employment.

But here, Democrats oppose any limits on immigration, tarring those who disagree as racists. That name-calling divides the nation. We need a civil debate over how to welcome migrants to keep our economy growing yet still put our own needy first.

Betsy McCaughey is a former lieutenant governor of New York.