2

I was trying to migrate database schema to POSTGRESQL using the code below BUT it does not work, it returned an error

"source driver: unknown driver file (forgotten import?)"

And it worked well using CLI

Up.sql and down.sql files are in dbMigration folder

package main

import (
    "log"
    "github.com/golang-migrate/migrate"
)

func main() {
    mg, err := migrate.New(
        "file://dbMigration",
        "postgres://username:localhost:5432/databasename?sslmode=disable",
    )
    if err != nil {
        log.Fatal(err)
    }
    if err = mg.Up; err != nil {
        log.Fatal(err)
    }
}

1 Answer 1

3

If you use database packages in go, you usually need to import the driver separately.

That's also what the error is trying to tell you.

source driver: unknown driver file (forgotten import?)

In golang-migrate, you can import the driver from their repository.

import (
    "github.com/golang-migrate/migrate/v4"
    _ "github.com/golang-migrate/migrate/v4/database/postgres"
    _ "github.com/golang-migrate/migrate/v4/source/github"
)

This is also documented in their readme.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.