President Obama rejected the Keystone XL pipeline application on Wednesday. It would have connected the Canadian oil sands to the Gulf coast in Texas. Newt Gingrich called the decision “stupid” and said it was made to please “left-wing extremists sitting in San Francisco.” Why is San Francisco so liberal?

It began with sailors and booze. As a former mining boomtown and a bustling port, San Francisco has always been slightly more permissive than the average American metropolis. Sailors loved to visit the city’s bars and bawdy houses in the late 1800s and early 1900s. But San Francisco’s national reputation for liberalism and libertinism really got going after Prohibition ended in 1933. California was one of the few states that did not establish an alcoholic beverage control board, which meant there was little regulation of bars and nightclubs. San Francisco, with its large population of Irish and German Catholics, tended to be more lax about drinking than cities farther south, where Protestants were more common and there was a strong, permanent military presence. The local government was also weak by comparison and largely controlled by labor unions with little interest in suppressing the burgeoning bar scene. The “Bohemian clubs” around the city’s North Beach neighborhood became gathering places for marginalized groups—communists, anarchists, homosexuals, and African-Americans, among others—and word spread that the city was safe for freethinkers and other black sheep.

When World War II broke out, San Francisco was the last stop for many sailors before shipping out to the Pacific theater, and these men, too, took advantage of the city’s looser moral code. The city’s reputation for permissiveness snowballed after the war. Returning soldiers who fondly recalled their wild days and nights by the Bay made it their permanent home, and others seeking a freer set of morals joined them. Population surged 22 percent during the 1940s. Influential San Franciscans began to trumpet the city’s openness to all people and all lifestyles, and newspaper columnist Herb Caen labeled the exotic city “Baghdad by the Bay.”

While San Francisco became known for its laissez-faire style, the influx of freethinkers eventually pushed the limits. The Catholic old guard began to resist public expressions of sexuality, tolerance of illicit drugs, and un-American political views. The conflict brought national attention to the city as a battleground for traditional values. Police arrested Lawrence Ferlinghetti and Shigeyoshi Murao for obscenity in 1957 after they sold Allen Ginsberg’s Howl. (The two were exonerated at trial.) After mass student protests in favor of free speech, gubernatorial candidate Ronald Reagan campaigned on a promise to “clean up the mess in Berkeley.” The 1967 Summer of Love, with its “free store” and even freer morals, angered many longtime residents. San Francisco was the home base of gay rights advocates who halted Anita Bryant’s anti-homosexuality campaign.

While San Francisco has been considered socially liberal for the better part of a century, it didn’t become political shorthand for extreme leftism until the mid-1980s. At the 1984 Republican National Convention, Ronald Reagan’s ambassador to the United Nations, Democrat Jeane Kirkpatrick, accused “San Francisco Democrats” of weakening the country’s foreign policy and constantly blaming the United States for the success of Central American leftists. Bill O’Reilly and others picked up on the theme, summing up left-wing ideology as “San Francisco values.”

Got a question about today’s news? Ask the Explainer.

Explainer thanks Nan Alamilla Boyd of San Francisco State University and author of Wide Open Town: A History of Queer San Francisco to 1965, Richard DeLeon of San Francisco State University and author of Left Coast City: Progressive Politics in San Francisco, 1975-1991, Bill Issel of San Francisco State University and author of For Both Cross and Flag: Catholic Action, Anti-Catholicism, and National Security Politics in World War II San Francisco, and Michael J. Kramer of Northwestern University.

Video Explainer: Are Shipwreck Evacuation Rules Really “Women and Children First”?

This video was produced from an original Explainer by Brian Palmer

Want more questions answered? You can now watch video Explainers at Slate’s News Channel on YouTube.