golang-migrate/migrate · error

no database name

Error message

no database name

What it means

ErrNoDatabaseName indicates a Config (or parsed URL) without a database name, which golang-migrate needs to track migrations per database. WithInstance, Open, and WithConnection return it before touching the database. It is exported so callers can match it with errors.Is.

Source

Thrown at database/postgres/postgres.go:39

	"github.com/lib/pq"
)

func init() {
	db := Postgres{}
	database.Register("postgres", &db)
	database.Register("postgresql", &db)
}

var (
	multiStmtDelimiter = []byte(";")

	DefaultMigrationsTable       = "schema_migrations"
	DefaultMultiStatementMaxSize = 10 * 1 << 20 // 10 MB
)

var (
	ErrNilConfig      = fmt.Errorf("no config")
	ErrNoDatabaseName = fmt.Errorf("no database name")
	ErrNoSchema       = fmt.Errorf("no schema")
	ErrDatabaseDirty  = fmt.Errorf("database is dirty")
)

type Config struct {
	MigrationsTable       string
	MigrationsTableQuoted bool
	MultiStatementEnabled bool
	DatabaseName          string
	SchemaName            string
	migrationsSchemaName  string
	migrationsTableName   string
	StatementTimeout      time.Duration
	MultiStatementMaxSize int
}

type Postgres struct {
	// Locking and unlocking need to use the same connection

View on GitHub (pinned to 01a9643f14)

Solutions

  1. Set Config.DatabaseName explicitly when using WithInstance/WithConnection
  2. Include a database name in the URL path: postgres://user:pass@host:5432/mydb?sslmode=disable
  3. Fail fast with errors.Is(err, postgres.ErrNoDatabaseName) to give a clear message at startup

Example fix

// before
url := "postgres://user:pass@localhost:5432?sslmode=disable"
// after
url := "postgres://user:pass@localhost:5432/mydb?sslmode=disable"
Defensive patterns

Strategy: validation

Validate before calling

if cfg != nil && cfg.DatabaseName == "" {
    return errors.New("Config.DatabaseName must be set")
}
if u, _ := url.Parse(dsn); strings.Trim(u.Path, "/") == "" {
    return errors.New("DSN must include a database name path")
}

Try / catch

if err := openMigrate(); err != nil {
    if errors.Is(err, postgres.ErrNoDatabaseName) {
        log.Fatal("database name missing: set it in the URL path or Config.DatabaseName")
    }
    panic(err)
}

Prevention

When it happens

Trigger: postgres.WithInstance with Config{DatabaseName: ""}; cockroachdb.Open with a URL whose path is empty (e.g. postgres://host:26257?sslmode=disable); building Config dynamically where the database name resolved to an empty string.

Common situations: URL missing the /dbname path segment; environment variable for DB name unset and interpolated as empty; hardcoding a config struct and forgetting DatabaseName; moving from lib/pq to cockroachdb driver which enforces the same check.

Related errors


AI-assisted analysis of golang-migrate/migrate@01a9643f14 (2026-09-02). Data as JSON: /api/errors/15303bd2a43c992d. Report an issue: GitHub.