skip to Main Content

I am trying to run migrations in my go-fiber rest API using golang-migrate.

I added the commands for running the migrations in a makefile. However, when I run make migrateup, I get the following error:

migrate -path database/postgres/migrations -database "postgresql://postgres:postgres@localhost:5400/property?sslmode=disable" -verbose up
2022/11/10 18:00:17 error: database driver: unknown driver postgresql (forgotten import?)
make: *** [Makefile:15: migrateup] Error 1

This is the make file I am using.

#### IMPORT ENV
include .env
DB_URL=postgresql://$(DB_USER):$(DB_PASSWORD)@$(DB_HOST):$(DB_PORT)/$(DB_NAME)?sslmode=disable

postgres:
    docker run --name postgres -p $(DB_PORT):5432 -e POSTGRES_USER=$(DB_USER) -e POSTGRES_PASSWORD=$(DB_PASSWORD) -d postgres:alpine

createdb:
    docker exec -it postgres createdb --username=$(DB_USER) --owner=$(DB_OWNER) $(DB_NAME)

dropdb:
    docker exec -it postgres dropdb --username=$(DB_USER) $(DB_NAME)

migrateup:
    migrate -path database/postgres/migrations -database "$(DB_URL)" -verbose up

migratedown:
    migrate -path database/postgres/migrations -database $(DB_URL) -verbose down

.PHONY: postgres createdb dropdb

Please can anyone help me understand why this is not working?

3

Answers


  1. Chosen as BEST ANSWER

    This issue was resolved by uninstalling golang migrate and installing it again. After that, it worked.


  2. You need to install golang-migrate with special tag to have support of particular driver.

    It’s written in documentation of command line tool: https://github.com/golang-migrate/migrate/tree/master/cmd/migrate#with-go-toolchain

    Login or Signup to reply.
  3. first you install driver database.

    database migration go install -tags "postgres,mysql" github.com/golang-migrate/migrate/v4/cmd/migrate@latest

    im use code makefile

    table = $(table)
    name = $(name)
    url=postgres://postgres:[email protected]:5432/school?sslmode=disable
    version=$(version)
    migration-up :
        migrate -database "$(url)" -path ./migrations/ up $(version)
        
    migration-down :
        migrate -database "$(url)" -path ./migrations/ down $(version)
        
    migration-create:
        migrate create -ext sql -dir ./migrations/ -seq $(name)
    
    migration-force:
        migrate -database "$(url)" -path ./migrations/ force $(version)
    
    migration-version:
        migrate -database "$(url)" -path ./migrations/ version
    

    make migration-create name=users create file migration

    make migration-up version=1 run migration up to running file sql

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search