We’re failing in Afghanistan. Confirmed yesterday as our new commander there, Gen. David Petraeus has the unenviable task of producing something President Obama can call a success.

If Petraeus can salvage the situation so that our minimum needs are met, he’ll confirm his reputation as the greatest American soldier of our time. Should he falter, he’ll go down as the general who sold the Pentagon a disastrous doctrine.

Petraeus did not want this job. But our president didn’t know to whom else to turn in the wake of last week’s debacle with Gen. Stanley McChrystal. Petraeus did his duty, saluted and said, “Yes, sir.”

Politically, Obama couldn’t have made a shrewder choice. Petraeus enjoys cult status on both the right and the moderate left. The question is whether he’s the right man militarily.

Petraeus is a man of integrity. Now we’ll see if he’s also a man of character. Will he have the wherewithal to accept that our current approach — based on the counterinsurgency doctrine he authored — doesn’t work in Afghanistan? Or will he continue trying to win unwinnable hearts and minds?

His counterinsurgency doctrine assumes that doing good unto others will lead to us doing well. But we’re lashed to a hated Kabul government, the enemy’s strength is growing, our allies are bailing, our showpiece Kandahar operation’s on hold, corruption runs into the billions, druglords prosper, the population’s increasingly wary of us — and our casualty rates are rising, as our troops are prevented from pre-empting or even pursuing the enemy. Other than that, things are fine.

Petraeus does assume command with unprecedented advantages, but with daunting challenges, as well. First, the factors in his favor:

* Thanks to his platinum reputation, he’ll have more leeway than any prior commander in Afghanistan. If he wants to delay the troop withdrawals scheduled for next summer, Obama won’t dare tell him no.

* Petraeus will be impossible to fire. McChrystal — not an ambitious journalist — was the author of his own downfall. But in relieving him, Obama used up his entire stack of blame-the-generals cards. Obama needs Petraeus to succeed.

* Petraeus understands team-building. He’ll be more effective at manipulating allies, appeasing diplomats and working the Afghans than the earnest but clumsy McChrystal.

* Petraeus starts with more US troops and more money at his disposal than any of his predecessors.

That said, he’ll need every advantage, since he faces these grave challenges:

* His counterinsurgency doctrine assumes that we’re partnered with a worthy host government that deserves our support and can build sufficient popular appeal to convince its people to defend it. But the Karzai regime is corrupt, inept, duplicitous and despised. (It’s quite an achievement to have made so many Afghans prefer the once-hated Taliban.)

* Our grandiose ambition to recreate Afghanistan in our own image is unworkable. Petraeus must convince Obama and the rest of Washington to reduce our goals to those (such as continuing to strike terrorists) that directly benefit the United States — and are attainable.

* The strategic problem isn’t Afghanistan, but Pakistan. This truculent, anti-American state continues to play double and triple games, taking our money while supporting the Afghan Taliban, the grisly Haqqani network and anti-India terrorist groups. Afghanistan can’t make progress as long as Pakistan harbors and helps the insurgents.

* Petraeus needs to fight clichés (some of which he promoted) as well as the physical enemy. The worst one is, “We can’t kill our way out of this.”

You can concoct all the highfalutin’ theories you want, but the facts of 2,000 years of history make it clear that the only way to defeat an insurgency that includes religious zealots is to kill your enemies with greater ruthlessness than they can kill you. No exceptions.

We are not exceptions to history. Our reluctance to shed blood may make us virtuous, but doesn’t necessarily make us wise.

* Related to the last point: Our troops continue to do everything asked of them remarkably well, but frontline fighters have a morale problem with rules of engagement that protect our enemies, while exposing them needlessly to mortal danger.

We can’t be so protective of gray-area civilians that we kill our own kind. We need to stop apologizing and put the blame for civilian casualties where it belongs — on the Taliban.

To his credit, Petraeus already has said that he’ll reconsider our rules of engagement. Now he has to follow through: Our soldiers and Marines are not the enemy.

As Petraeus assumes command, he faces hundreds of other challenges, too. Afghanistan’s never been the “graveyard of empires” pundits have declared it. But it certainly has been the graveyard of reputations.

Perhaps the greatest advantage Petraeus has is that he doesn’t like to lose. He now has the closest thing to a free hand any post-modern general can expect. But what has been Obama’s War for the last 18 months is now Petraeus’ War.