Head in the clouds

Head in the clouds

Creating a Postgres database in AWS

All right! I have my first goal in sight!

Giving you some context: I've completed a few courses to learn Go and Postgres. And in my machine, they work beautifully. But it's all local hosting. How do they work in the cloud? Because there is absolutely no way I'm building a server at home.

Do I have to pay for two separate machines in a Cloud service, one for the database and one for the Go code? I have no clue! So I'll spend the next few days trying to figure it out!

Well, the first step is to create our Postgres Database in a cloud service and I'm not going to put too much effort into choosing. Let's go with AWS.

During my research, I found a youtube video by Code For All in Portuguese with a quick tutorial on how to create a Postgres database in AWS.

Oh, the joy! Look at that beautiful green check! now the difficult part begins. We have to connect to the database using our Go code and I'll be doing that with my very good friend Gorm. I don't want to make a mess out of my code, so I created a directory in the root folder called database, and a db.go file inside.

package database

import (

var (
    DB  *gorm.DB
    err error

func ConnectToDb() {
    dsn := "host=HOSTNAME user=USERNAME password=PASSWORD dbname=spintrack_db_1 port=5432 sslmode=disable"
    DB, err = gorm.Open(postgres.Open(dsn))
    if err != nil {
        log.Panic("Error connecting to database")

Now we just have to import this into our main.go file

package main

import (

func main() {
    fmt.Println("Hello SpinTrack API")

So let's run go run main.go

Well, that tells me a whole bunch of nothing. At least the error wasn't printed... maybe it worked. Let's build something so we can make sure.

In the next post, I'll be talking about GORM and Gin to start creating our API.

I'll see you the next time. Until then, stay curious.

Check out my project repository!

Cover photo by engin akyurt on Unsplash