A federal judge temporarily blocked construction of the controversial Keystone XL pipeline, ruling late Thursday that the Trump administration had failed to justify its decision granting a permit for the 1,200-mile long project designed to connect Canada’s tar sands crude with refineries on the Texas Gulf Coast.

It was a major defeat for President Donald Trump, who attacked the Obama administration for failing to move ahead in the face of protests based largely on environmental concerns. Trump signed an executive order two days into his presidency setting in motion a course reversal on the Keystone XL pipeline and the Dakota Access pipeline.

The decision, issued by Judge Brian Morris of the U.S. District Court for the District of Montana, does not permanently block a permit but requires the administration to conduct a more complete review of potential adverse impacts related to climate change, cultural resources and endangered species. It basically ordered a do-over.

Morris hit the administration with a familiar charge, that it disregarded facts, facts established by experts during the Obama administration about "climate-related impacts" from Keystone XL. The Trump administration claimed, with no supporting information, that those impacts "would prove inconsequential." The State Department "simply discarded prior factual findings related to climate change to support its course reversal."

It also used "outdated information" about the impact of potential oil spills on endangered species, he said, rather than "'the best scientific and commercial data available.'"

"Today's ruling makes it clear once and for all that it's time for TransCanada to give up on their Keystone XL pipe dream," said Sierra Club Senior Attorney Doug Hayes in a statement. The lawsuit prompting Thursday's order was brought by a collection of opponents, including the indigenous Environmental Network and the Northern Plains Resource Council, a conservation coalition based in Montana.

"The Trump administration tried to force this dirty pipeline project on the American people, but they can't ignore the threats it would pose to our clean water, our climate, and our communities," Hayes said.

Hayes told The Washington Post that the company had already been moving equipment into place and started pre-construction in Montana and South Dakota with the intent of beginning construction in early 2019.

"It's clear that this decision tonight will delay the pipeline significantly," said Hayes, who noted that a proper environmental impact statement of this scope usually takes about a year to complete. "TransCanada does not have an approved pipeline at this point."

Morris, a former clerk to the late Chief Justice William Rehnquist, was appointed to the bench by President Barack Obama. His decision was one of scores of court rebukes to the Trump administration for decisions on the environment, immigration and transgender service in the military, among other issues, made hastily and, in the opinions of dozens of judges, without the "reasoned consideration" required by various federal laws, particularly the Administrative Procedure Act. Also on Thursday, a federal appeals court ruled that President Trump cannot immediately end the program, DACA, that shields from deportation young undocumented immigrants who were brought to the country as children.

The administration is appealing many of the rulings and may appeal Thursday's decision as well. No immediate comment came from the administration after the pipeline order. TransCanada, the Calgary-based group behind the project, did not respond to request for comment early Friday morning.

The Keystone XL pipeline permit decision was largely in the hands of the State Department, by virtue of its authority to issue "presidential permits" for cross-border infrastructure projects.

The massive project remains one of the most controversial infrastructure proposals in modern American history.

It's intended to be an extension of TransCanada's existing Keystone Pipeline, which was completed in 2013. Keystone XL would transport up to 830,000 barrels of crude oil per day from Alberta, Canada and Montana to Oklahoma and the Gulf Coast. In the U.S., the pipeline would stretch 875 miles through Montana, South Dakota and Nebraska, with the rest continuing into Canada.

It met sustained opposition from environmental advocacy groups, as well as from Obama, who worried about the contribution it would make toward climate change.

As The Post's Steven Mufson reported, activists say the pipeline would be especially damaging to the climate because it would mean extracting thick, low-quality oil from Canada's oil sands, with lots of tree-cutting and energy consumption in the process which would increase greenhouse gas-emissions. Native American groups in Montana and elsewhere fought the Keystone project as well, saying its route failed to adhere to historical treaty boundaries and would impinge on their water systems and sacred lands.

In 2015, on the eve of the international climate talks in Paris, the Obama administration appeared to bring an end to the seven-year-long saga when it announced it was halting construction of the pipeline, arguing that approval would compromise the country's effort to reduce its greenhouse gas emissions. The U.S.. Obama said, was now a "global leader when it comes to taking serious action to fight climate change."

"And frankly, approving this project would have undercut that global leadership," he said, adding that the "biggest risk" the U.S. faced was "not acting."

The decision to deny the pipeline permit came after the completion of a long-awaited final environmental impact statement -- 11 volumes of analysis released in 2014.

It was this 2014 assessment that the State Department, under the direction of Trump's January 2017 presidential memorandum, used to make their decision to approve the pipeline, The Post reported. According to the department, "there are no substantial changes or significant new information which would affect the continued reliability" of the report.

Morris said, however, that there have indeed been changes since the 2014 assessment and that the Trump administration failed to consider them. He included pipeline leaks, the expansion of another pipeline called the Alberta Clipper and shifts in oil markets. Those could alter the overall impact of Keystone XL and should have been considered by the government.

Among the judge's findings:

- The State Department, in issuing the permit, failed to "analyze the cumulative greenhouse gas emissions" of the Keystone project and the expanded Alberta Clipper pipeline. It "ignored its duty to take a 'hard look' at these two connected actions.

- The department "acted on incomplete information regarding" the potential damage to cultural resources in Indian territory along the route. "The Department appears to have jumped the gun."

- The department failed to make a fact-based explanation for its course reversal, "let alone a reasoned explanation....'An agency cannot simply disregard contrary or inconvenient factual determinations that it made in the past, any more than it can ignore inconvenient facts'" in the present," he wrote, quoting judicial precedents.

- The department's analysis that "climate-related impacts" from Keystone "would prove inconsequential" needed a "reasoned explanation." It did not provide one.

Jackie Prange, senior attorney with the Natural Resources Defense Council, called the ruling a "huge win" not just for the environmental activists and tribal groups who have been fighting the pipeline, but for "anyone who cares about the rule of law and holding this administration to the facts."

“It’s emblematic of what we’re seeing with the Trump administration, which is a very fast and sloppy reversal of prior decisions . . . in a way that doesn’t adhere to the rule of law,” Prange told The Post. “That’s why we keep winning in the court.”