Only about 1 in 10,000 people has perfect pitch (also called absolute pitch). It is interesting that a significant portion of famous composers had perfect pitch, but many successful composers didn't have it. And certainly most songwriters don't have it.
The ability to write a good, memorable tune is a very interesting muscle to flex. It does get stronger as you learn to flex it, but it's hard to describe where to begin. I recommend repetition: try creating lots of melodies. I find I do this almost automatically when the creative portion of my mind is bored while doing mechanical things, like driving or showering. I'm not great at it, but I'm learning.
There's nothing magical about keeping the melody you created in exactly the pitch you invented it in your mind. It is entirely normal for bands to transpose their songs up or down, sometimes for instrument range or ease of keys, but almost always for the singer's range.
It does take some understanding and knowing of pitch to be able to play the melody you just invented on an instrument. This is called "playing by ear", which is very different and exponentially easier than developing perfect pitch. All musicians (that play tonal instruments) develop this, but you can speed it up by specifically practicing matching pitches from songs as you hear them on an instrument, or by using the musician's definition of ear training software (which helps you train your hearing of intervals and chords).
P.S.
An exponentially larger share of the population in Asian countries that use tonality in their languages (including Mandarin and Vietnamese) have perfect pitch. It's not a genetic thing: it can be learned, certainly influenced. Though the bulk of the easy training is from ages 4-6. If you combine early learning of tonal languages with early learning of music, a child has a significantly better chance. At the Central School of Music in Bejing, about 60% of children that begin musical instruction by age 4 develop perfect pitch.
If this interests you, listen to podcasts RadioLab, with their episode Behaves So Strangely, and Freakonomics, with their episode How to Become Great at Just About Anything.