Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

source driver: unknown driver postgres (forgotten import?) even though lib/pq is being imported

Tags:

postgresql

go

There are a couple of answers for this up already, but they seemingly tell me to do what I'm already doing, and don't go any deeper into what the issue could be.

A litte recon. I'm trying to get my Go application to run the database migration when it starts up. I have a Postgres database running in a docker container. I can connect to it using my db tools. I can run the migration file from its location using the the command-line. However if I run my service as it stands I get the:

source driver: unknown driver postgres (forgotten import?)

here's my code:

package main

import (
    "database/sql"
    "log"
    "os"

    "github.com/golang-migrate/migrate/v4"
    _ "github.com/golang-migrate/migrate/v4/database/postgres"
    _ "github.com/golang-migrate/migrate/v4/source/file"
    _ "github.com/lib/pq"

    "github.com/joho/godotenv"
)

func init() {
    if err := godotenv.Load(".env"); err != nil {
        log.Println("no .env file found")
    }
}

func main() {
     db_url := os.Getenv("POSTGRES_URL");
     mig_url := os.Getenv("MIGRATION_URL")
    MustMigrateUp(db_url, mig_url)

     _, err := sql.Open("postgres", db_url)
    if err != nil {
        log.Fatal(err)
    }
}

func MustMigrateUp(db_url, mig_url string) {
    m, err := migrate.New(
        db_url,
        mig_url,
    )
    println(os.Getenv("POSTGRES_URL"))
    if err != nil {
        log.Fatalln(err)
    }

    if err := m.Up(); err != nil {
        log.Fatalln(err)
    }
}

I appreciate any feedback!

like image 251
Meds Avatar asked Sep 10 '25 15:09

Meds


1 Answers

migrate.New() expects the DB url second in the arguments. https://godoc.org/github.com/golang-migrate/migrate#New

Don't only code at 3 am kids.

like image 195
Meds Avatar answered Sep 13 '25 05:09

Meds