我有一个使用PostgreSQL的小型Go Web应用程序.
我的数据库驱动程序是pgx.现在,我希望在应用程序启动时自动运行migrations.我为此找到了goose个.
然而,我很难在pgx
车手上使用goose
.
当我运行我的代码时,我将得到:
Opening DB
2023/08/09 09:39:28 sql: unknown driver "pgx" (forgotten import?)
以下是主干道的缩写.开始:
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...
}
我判断了OpenDBWithDriver,而PGx是一个预期值.在这种情况下,我遗漏了什么?