President Trump’s tirade against the 9th Circuit Court of Appeals hearing on his immigration order is going to trigger the usual outrage from the Democrats claiming he’s delegitimizing the judiciary. Too bad they lack credibility.

President Barack Obama used it all up.

Trump let loose at the judges who ride the 9th Circuit after their hearing on the immigration restrictions. He told a meeting of police chiefs that he’d watched the proceedings “in amazement.”

Targets of the presidential thunderbolts included even lawyers defending his immigration order: “I listened to lawyers on both sides last night, and they were talking about things that had nothing to do with it.”

What’s so startling about the presidential protest is that the court hasn’t yet made a decision. Then again, Obama himself tried that technique in advance of the Supreme Court ruling on the ObamaCare case.

And it may have worked.

That was in April 2012, just after the nine justices heard arguments that ObamaCare was unconstitutional. Obama turned around and — in a press conference with foreign leaders, no less — lit into the court.

Obama complained about the very idea that “an unelected group of people would somehow overturn a duly constituted and passed law.” He even claimed ObamaCare was passed by “a strong majority of a democratically elected Congress.”

What bunk. The law passed the House by the thinnest of party-line margins, 219 to 212. Yet what was so outrageous is that the business about an “unelected group” seemed to question the standing, the bona fides of the court.

The power of unelected judges to invalidate laws that are crosswise with the Constitution is part of American bedrock. It was vouchsafed in the Supreme Court case known as Marbury v. Madison.

That was in 1803. Even before then, though, in the fight here in New York over ratification of the Constitution, Alexander Hamilton went through the logic of the courts having power to overturn laws.

Hamilton said the “complete independence of the courts” is “peculiarly essential in a limited Constitution.” He went on to write that “no legislative act” that was “contrary to the Constitution” can be “valid.”

Whether Obama’s attack on the Supreme Court actually worked is hard to gauge. But the chief justice, John Roberts, used such bizarre logic as to become a complete constitutional pretzel.

Nor was Obama the only Democrat to intimidate the court. When he gave his first State of the Union Address and laced into the Supreme Court for its Citizens United campaign-finance decision, the entire Democratic caucus stood up and, on live TV, jeered the justices.

Nothing Trump said is any worse than that. Or than President Andrew Jackson, who once famously said that Chief Justice John Marshall “has made his decision, now let him enforce it.”

Trump, moreover, is a lot righter than Marshall, Obama or Democrats were on the underlying issues. This was well put in a Monday Post column by Rich Lowry: The president has wide latitude on both national security and immigration, and Trump stayed within the lines.

It’s just amazing to watch the spectacle of one of our appeals courts trying to hamstring a president who, in the middle of a war, is trying to faithfully execute a law that gives him clear authority to use his judgment on immigration and refugees.

I would go further than Trump’s lawyers have. Not only does the president possess the authority to suspend immigration if he thinks it’s a threat to the nation, but he is required to do so.

This is because of a clause of the Constitution that has so far received little discussion. Much of the parchment concerns things the government can do. Other parts (such as the Bill of Rights) lists things the government can’t do.

There is, though, one part of the Constitution — Article IV, Section 4 — that lists the few things the government must do, whether it wants to or not. These are the obligations clauses.

One of the obligations is that the United States “shall protect” each state “against invasion.” Not “can” protect but “shall.” So if President Trump thinks infiltration by the Islamic State amounts to any kind of invasion, he has to act. That’s never been invoked, but, hey, there’s a first time for everything.