golang-migrate/migrate · error

no database name

Error message

no database name

What it means

CockroachDB driver's ErrNoDatabaseName ("no database name") is returned by WithInstance, Open, and WithConnection when the resolved database name is the empty string. Migrations need a target database to place the schema_migrations and schema_lock tables; without a database name the driver cannot proceed.

Source

Thrown at database/cockroachdb/cockroachdb.go:32

	"github.com/cockroachdb/cockroach-go/v2/crdb"
	"github.com/golang-migrate/migrate/v4"
	"github.com/golang-migrate/migrate/v4/database"
	"github.com/lib/pq"
)

func init() {
	db := CockroachDb{}
	database.Register("cockroach", &db)
	database.Register("cockroachdb", &db)
	database.Register("crdb-postgres", &db)
}

var DefaultMigrationsTable = "schema_migrations"
var DefaultLockTable = "schema_lock"

var (
	ErrNilConfig      = fmt.Errorf("no config")
	ErrNoDatabaseName = fmt.Errorf("no database name")
)

type Config struct {
	MigrationsTable string
	LockTable       string
	ForceLock       bool
	DatabaseName    string
}

type CockroachDb struct {
	db       *sql.DB
	isLocked atomic.Bool

	// Open and WithInstance need to guarantee that config is never nil
	config *Config
}

func WithInstance(instance *sql.DB, config *Config) (database.Driver, error) {

View on GitHub (pinned to 01a9643f14)

Solutions

  1. Include the database in the DSN path: "cockroachdb://root@host:26257/mydb?sslmode=disable".
  2. Set cfg.DatabaseName explicitly when using WithInstance/WithConnection.
  3. Verify any env interpolation (e.g. ${DB_NAME}) is actually populated before composing the DSN.

Example fix

// before
dsn := "cockroachdb://root@localhost:26257/?sslmode=disable"
// after
dsn := "cockroachdb://root@localhost:26257/migrations?sslmode=disable"
Defensive patterns

Strategy: validation

Validate before calling

u, err := url.Parse(dsn)
if err != nil { return err }
if strings.Trim(u.Path, "/") == "" {
    return fmt.Errorf("cockroachdb DSN must include a database name, e.g. cockroachdb://host:26257/db")
}

Try / catch

d, err := cockroachdb.Open(dsn)
if err != nil {
    if errors.Is(err, cockroachdb.ErrNoDatabaseName) {
        return fmt.Errorf("DSN %q is missing the database path component", dsn)
    }
    return err
}

Prevention

When it happens

Trigger: Passing a Config with DatabaseName == "" to WithInstance; opening a DSN with no database path component, e.g. "cockroachdb://root@localhost:26257/?sslmode=disable".

Common situations: DSN copied from a tool that omitted the database segment; env-var interpolation that produced an empty database name; programmatically built Config with only MigrationsTable set.

Related errors


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