api-go
	|
	|-------> internal 
	|							|------> db
	|							|         |-----> postgres.go
	|							|
	|							|------> domain 
	|							|         |-----> website.go
	|							|
	|							|------> dto
	|							|					|-----> websites.go
	|							|
	|							|------> handlers
	|							|					|-----> websites.go
	|							|
	|							|------> repository
	|							|         |-----> website_repository_pg.go
	|							|         |-----> website_repository.go
	|							|
	|							|
	|							|------> routes
	|							|					|-----> websites.go
	|							|
	|							|------> service
	|												|-----> websites.go		
	|
	|------> migrations
	|							|------> 20251226095007_create_websites_table.down.sql		
	|							|------> 20251226095007_create_websites_table.up.sql	
	|
	|------> .env	

Step 1 : Start postgres via docker

docker run -p 5432:5432 -e POSTGRES_PASSWORD=postgres -d postgres

Step 2 : Install golang-migrate

brew install golang-migrate

Step 3 : Create migration files inside migration folder

# STEP 1
cd apps/api-go

# STEP 2
migrate create -ext sql -dir migrations create_websites_table

image.png

Step 3 : Write SQL migrations

20251226095007_create_websites_table.up.sql

CREATE EXTENSION IF NOT EXISTS "pgcrypto";

CREATE TABLE website (
    id UUID NOT NULL DEFAULT gen_random_uuid(),
    url TEXT NOT NULL UNIQUE,
    time_added TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "Website_pkey" PRIMARY KEY ("id")
);

apps/api-go/migrations/20251226095007_create_websites_table.down.sql

DROP TABLE website;

Step 4 : Run the migrations

cd apps/api-go

migrate -database "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable" -path migrations up

OR

# CLI is unable to read DATABASE_URL variable from .env unless its exported
export DATABASE_URL="postgres://postgres:postgres@localhost:5432/postgres_go?sslmode=disable"

# Check if DATABASE_URL is set
echo $DATABASE_URL

cd apps/api-go # (Run from root of project )
migrate -database "$DATABASE_URL" -path migrations up

In case you need to update your migrations file do this

Rollback & reapply (simplest)