I recall reading that Goldberg commissioned them from Bach to help him sleep.
I've heard that, too. But when I listen to all of them, some seem more like songs to wake a person up.
Supposedly they were written to entertain someone who could NOT sleep:
"The urban myth surrounding the variations is that they were played by Goldberg to Count Kaiserling, former Russian ambassador to the electoral court of Saxony, who was often ill and suffered from insomnia as a consequence. Goldberg played Bach’s variations to the Count to assuage his sleeplessness and to entertain him during the wee small hours."
Then again, it's all just fun gossip because there's not enough evidence. But it's a great piece.
https://bachtrack.com/baroque-month-portrait-bach-goldberg-variations