US President Donald Trump has ended his quest to add a contentious citizenship question to the 2020 census, but insisted he was not retreating from his fight against illegal immigration and said the government would obtain the data by combing through federal records.

"We are not backing down on our effort to determine the citizenship status of the United States population," Mr Trump said in an announcement at the White House.

He said he was ordering every government agency to provide the Department of Commerce with all requested records regarding the number of citizens and non-citizens in the country.

"We will utilise these vast federal databases to gain a full, complete and accurate count of the non-citizen population, including databases maintained by the Department of Homeland Security and the Social Security Administration.

"We have great knowledge in many of our agencies," Mr Trump said.

"We will leave no stone unturned," he said.

Mr Trump's plan to add the question to the census ran into a roadblock two weeks ago when the Supreme Court ruled against the administration, which had said new data on citizenship would help to better enforce the Voting Rights Act, which protects minority rights.

The court ruled, in considering the litigation by challengers, that that rationale was "contrived".

Mr Trump had been expected until a few hours before his remarks in the White House Rose Garden to go ahead despite that ruling by using an executive order to include the question, prompting some analysts to say he risked a constitutional crisis.

The US Census Bureau is part of the Commerce Department.

The Constitution specifically assigns the job of overseeing the census to Congress, which complicated adding the question to the once-a-decade nationwide survey via a presidential order.

Trump and other administration officials said they had changed strategy to avoid delaying the census. The Constitution mandates that everyone living in the United States be counted every ten years. Printing of the forms is already under way.

"As a practical matter, the Supreme Court's decision closed all paths to adding the question to the 2020 census. Put simply, the impediment was a logistical impediment, not a legal one," Attorney General William Barr said at the event.

The high court had also ruled that asking about citizenship on the census was constitutional.

Critics of the effort said that asking about citizenship in the census would discriminate against racial minorities and was aimed at giving Republicans an unfair advantage in elections by lowering the number of responses from residents of areas more likely to vote Democratic.

Mr Trump and his supporters say it makes sense to know how many non-citizens are living in the country.

"The data-sharing scheme described by the president…will be something that we watch with tremendous vigilance," said Kristen Clarke, president and executive director of Lawyers' Committee for Civil Rights Under Law.

"Trump may claim victory today, but this is nothing short of a total, humiliating defeat for him and his administration,"said Dale Ho, an attorney with the American Civil Liberties Union, which helped represent immigrant rights groups in the legal case challenging the census question.

"He lost in the Supreme Court, which saw through his lie about needing the question for the Voting Rights Act. It is clear he simply wanted to sow fear in immigrant communities and turbocharge Republican gerrymandering efforts by diluting the political influence of Latino communities," Mr Ho said.

The census is used to determine how many seats each state gets in the House of Representatives and also affects how billions of dollars in federal funds are doled out.