On Sunday, Dec. 9, 1883, more San Franciscans were at the beach than ever before in the city’s history. A reported 18,000 people filled the northern end of Ocean Beach, packed the Cliff House and spilled south toward Golden Gate Park.

“Along the Ocean Beach a sight was presented such as has not been seen before by those who for years had made almost daily trips to the beach in hack or buggy,” the Daily Alta California reported. “In spite of the cold wind the Cliff House end of the beach was literally thronged with people. Children, playing in the sand, looked at the ocean for the first time in their lives.”

One event was responsible for this unprecedented mass excursion to the seashore: the opening of the Park and Ocean Railroad. Although the creation of the rail line was surrounded by controversy, it represented a major transportation breakthrough. For the first time, the average San Franciscan could easily and affordably go to the beach.

During the city’s first three decades, its western edge was difficult and expensive to get to. An 1863 toll road, Point Lobos Avenue, ran from Bush and Presidio to the Cliff House, but using it required owning or renting a horse and carriage, and its round-trip fare of $1 was beyond the means of most people. A decade later, paved roads to the beach were opened in the new Golden Gate Park, but the Park Commission did not allow commercial lines to use them.

The new owners of one of the city’s principal cable car companies, the Market Street Cable Railroad Co., already had a cable car line that stopped at Haight and Stanyan streets. They realized that if they could run a train line from there to the beach, they could make a killing. So they asked the Park Commission for a 50-year franchise for their new Park and Ocean Railroad.

The route would run from the cable car stop on Stanyan, across the southeast corner of the park — near where Kezar Drive is today — to H Street, which is now Lincoln Way. From there, it would head west out H Street almost all the way to the beach.

As Western Neighborhoods Project founders Woody LaBounty and David Gallagher note in one of their informative “Outside Lands” podcasts (No. 90), the line would turn north between what are now 47th and 48th avenues, then go over an overpass, cut across the far western end of the park, through a tunnel and end up at what is now Balboa and La Playa. That’s just across from the road leading up to the Cliff House and where the plaque commemorating Playland-at-the-Beach now stands.

This was, to put it mildly, an audacious request. A private company was asking for an easement for a steam train to cross San Francisco’s new crown jewel, city-owned Golden Gate Park — and cross it not once but twice. The three-member Park Commission declined to approve the project. As Raymond Clary writes in “The Making of Golden Gate Park: The Early Years 1865-1906,” the commission said it would be willing to lease the land at the southeastern corner of the park, but would not allow a railroad to be built through the western end of the park.

That did not deter the owners of the Market Street Cable Railroad Co. — plutocrats who also owned the Southern Pacific Railroad, the all-powerful monopoly that basically ran the state. “The Octopus,” as the Southern Pacific would be called by novelist Frank Norris, almost always got what it wanted — and the slick way it greased the skids for its new project demonstrates why.

Leland Stanford, one of the “Big Four” magnates who created the Southern Pacific, was a director of the Park and Ocean Railroad. Two of the three park commissioners were in the SP’s pocket and gave in. But the third, William Alvord, steadfastly held out. The other commissioners manipulated him into resigning, then appointed none other than Stanford to replace him.

Stanford never attended any Park Commission meetings and resigned as soon as the deal was ensured, to avoid the appearance of conflict of interest that would arise if he voted on it.

The casual arrogance of the Southern Pacific was illustrated by the fact that the company began work on the line before the commission even approved it. When a Chronicle reporter asked the foreman of a construction crew if the railroad had received approval, the man replied, “Mr. Stanford wanted the road. That’s all.”

The city and state sued to stop the railroad, claiming it was a “nuisance” on public property, but pending a ruling, the Park and Ocean Railroad opened Saturday, Dec. 1, 1883. The public response was overwhelming. Ten thousand people rode the train to the beach on the first day; the next weekend 18,000 supposedly did, although that figure would have required a 360-passenger train to depart every 10 minutes for more than eight hours.

“Fully one half of the people who started for the beach never got beyond the point where the cable cars meet the steam dummies at the park entrance,” the Alta wrote.

At 20 cents a round trip, the Park and Ocean line wasn’t cheap — a clerk’s average salary in 1883 was $12 a week, making the fare the equivalent of $16 for someone making $50,000 a year today. But for thousands of San Franciscans who had rarely if ever been out to the ocean, it was an irresistible splurge to take the half-hour steam train ride along the sand dunes out to the end of the continent.

The state Supreme Court eventually gave the Park and Ocean Railroad its legal seal of approval. In 1898 the trains gave way to electric streetcars, which ran for the next 50 years. Gallagher and LaBounty point out that you can still see the berms where the overpass once stood, and the foot trail that runs near the soccer fields and the Park Chalet follows the old train line.

The strangest legacy of the opening up of Ocean Beach, however, was a peculiar squatter colony.

In its report on the vast crowds that rode the train on its second weekend, the Alta noted, “The newly erected refreshment stands at the beach did a heavy business.” The press did not realize it yet, but those refreshment stands were the first structures of a weird and wonderful settlement that was about to gain national notoriety as “Mooneysville.” The short, hilarious life of that whiskey-soaked shantytown-by-the-sea will be the subject of the next two Portals.

Gary Kamiya is the author of the best-selling book “Cool Gray City of Love: 49 Views of San Francisco,” awarded the Northern California Book Award in creative nonfiction. All the material in Portals of the Past is original for The San Francisco Chronicle. Email: metro@sfchronicle.com

Trivia time

The most recent trivia question: What was the first name given to the Farallon Islands?

Answer: The Isles of St. James, so named by Sir Francis Drake, who briefly stopped there in 1579 after putting in at Point Reyes.

This week’s trivia question: Who was the “Blind Boss”?

Editor’s note

Every corner in San Francisco has an astonishing story to tell. Gary Kamiya’s Portals of the Past tells those lost stories, using a specific location to illuminate San Francisco’s extraordinary history — from the days when giant mammoths wandered through what is now North Beach to the Gold Rush delirium, the dot-com madness and beyond. His column appears every other Saturday, alternating with Peter Hartlaub’s OurSF.