WASHINGTON — The Senate on Tuesday blocked a bill to end bulk collection of American phone records by the National Security Agency, dealing a blow to President Obama’s primary proposal to rein in domestic surveillance.

The 58-42 vote was two short of the 60 needed to proceed with debate. Voting was largely along party lines, with most Democrats supporting the bill and most Republicans voting against it.

The Maine delegation was split with Republican Sen. Susan Collins voting to block the bill and Sen. Angus King, an independent, voting for it.

The Republican-controlled House had previously passed its own NSA bill.

The legislation would have ended the NSA’s collection of domestic calling records, instead requiring the agency to obtain a court order each time it wanted to analyze the records in terrorism cases, and query records held by the telephone companies. In many cases the companies store the records for 18 months.

The revelation that the spying agency had been collecting and storing domestic phone records since shortly after the terrorist attacks of Sept. 11, 2001, was among the most significant by Edward Snowden, a former agency network administrator who turned over secret NSA documents to journalists. The agency collects only so-called metadata – numbers called, not names – and not the content of conversations. But the specter of the intelligence agency holding domestic calling records was deeply disquieting to many Americans.