The city was founded in 1776, when a Spanish presidio and a mission were established at a location chosen by Juan Bautista de Anza. The little settlement called Yerba Buena was still a village when the Mexican War broke out and a naval force under Commodore John D. Sloat took it (1846) in the name of the United States. It was then named San Francisco.
When gold was discovered in California in 1848, San Francisco had a population of c.800; two years later it was incorporated with a population of c.25,000. The rush of gold seekers, adventurers, and settlers brought a period of lawlessness, when the Barbary Coast flourished and the vigilantes were organized to keep peace. The city took on a cosmopolitan air, with newcomers arriving from all over the world. In this period the first Chinese settled in the city. In the years after the gold rush, San Francisco continued to grow as California became linked overland with the East, by the pony express in 1860 and by the transcontinental railroad in 1869.
On the morning of Apr. 18, 1906, the great San Andreas fault, which extends up and down the California coast, shifted violently, and San Francisco was shaken by an earthquake that, together with the sweeping three-day fire that followed, all but destroyed the city. Earthquakes have since continued to plague the city and its environs.
The opening of the Panama Canal, a boon to the city's trade, was celebrated by the Panama-Pacific Exposition of 1915. The spectacular San Francisco–Oakland Bay Bridge was opened in 1936 and the Golden Gate Bridge in 1937. By the time of the Golden Gate International Exposition (1939–40) the whole San Francisco Bay area was heavily industrialized; it had become the leading commercial center of the West Coast. During World War II, San Francisco was the major mainland supply point and port of embarkation for the war in the Pacific. The United Nations Charter (1945) was drafted at San Francisco, and the Japanese Peace Treaty (1951) was signed there.
San Francisco's natural beauty and mild climate have made it attractive as a residential city, but it is increasingly split between areas of wealth and of urban impoverishment. Among the more well-known contemporary neighborhoods are Haight-Ashbury, famous in the 1960s and 70s for its youth ("flower children"), music, and drug cultures; and a large homosexual community that has principally grown around Castro Street.
George Moscone, the city's mayor, and Harvey Milk, the first openly gay city supervisor, were assassinated in 1978. A severe earthquake hit the Bay Area in Oct., 1989,; the Marina district was the site of the most severe damage in San Francisco. In 1995 the city elected its first African-American mayor, Willie Brown, Jr., a former speaker of the state assembly.