gopgx

How to use goose migrations with pgx?


I have a small go web application that is using PostgreSQL.

My database driver is pgx. Now I am at a point where I want to run migrations automatically at applications start. I found goose for that.

However, I am struggling to use goose with the pgx driver.

When I run my code I will get:

Opening DB
2023/08/09 09:39:28 sql: unknown driver "pgx" (forgotten import?)

Here is the shortened main.go:

package main

import (
"context"
"flag"
"fmt"
"log"
"os"

    _ "github.com/jackc/pgx/v5"
    "github.com/pressly/goose/v3"

)

func main() {

    // DSN is the PostgreSQL Data Source Name, Database Source Name.
    // Often the same as connection string.
    dsn := flag.String("dsn", "postgres://postgres:password@localhost:5555/plazet", "PostgreSQL data source name")
    flag.Parse()
    
    db, err := openDbConnectionPool(*dsn)
    if err != nil {
        fmt.Printf("Unable to connect to database: %v\n", err)
        os.Exit(1)
    }
    
    // Ping the database to check if the connection is working
    connection, err := db.Acquire(context.Background())
    if err != nil {
        fmt.Printf("Unable to acquire connection: %v\n", err)
        os.Exit(1)
    }
    
    println("Opening DB")
    
    sql, err := goose.OpenDBWithDriver("pgx", *dsn)
    if err != nil {
        log.Fatalf(err.Error())
    }
    
    println("Migrating")
    
    err = goose.Up(sql, "./migrations")
    if err != nil {
        log.Fatalf(err.Error())
    }
    
    defer connection.Release()
    
    defer db.Close()
    
    // Code for router and stuff...

}

I checked the OpenDBWithDriver and pgx is an expected value. What am I missing in this case?


Solution

  • The driver pgx is registered by the github.com/jackc/pgx/v5/stdlib package (source code):

    package stdlib
    
    // ...
    
    func init() {
        pgxDriver = &Driver{
            configs: make(map[string]*pgx.ConnConfig),
        }
    
        // if pgx driver was already registered by different pgx major version then we
        // skip registration under the default name.
        if !contains(sql.Drivers(), "pgx") {
            sql.Register("pgx", pgxDriver)
        }
        sql.Register("pgx/v5", pgxDriver)
        // ...
    

    Try importing that package instead:

    import (
        _ "github.com/jackc/pgx/v5/stdlib"
        "github.com/pressly/goose/v3"
    )