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
This issue was resolved by uninstalling golang migrate and installing it again. After that, it worked.
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
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
make migration-create name=users
create file migrationmake migration-up version=1
run migration up to running file sql