golang-migrate/migrate · error

no database name

Error message

no database name

What it means

ErrNoDatabaseName means the driver could not determine which PostgreSQL database migrations apply to. pgx's ErrNoDatabaseName is raised by WithInstance/WithConnection/Open when config.DatabaseName is empty and the connected session reports an empty CURRENT_DATABASE(); the identical message also exists in cockroachdb, where Open returns it when the database name parsed from the URL is empty (database/cockroachdb/cockroachdb.go:67).

Source

Thrown at database/pgx/pgx.go:49

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

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

	DefaultMigrationsTable       = "schema_migrations"
	DefaultMultiStatementMaxSize = 10 * 1 << 20 // 10 MB
	DefaultLockTable             = "schema_lock"
	DefaultLockStrategy          = LockStrategyAdvisory
)

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
	DatabaseName          string
	SchemaName            string
	LockTable             string
	LockStrategy          string
	migrationsSchemaName  string
	migrationsTableName   string
	StatementTimeout      time.Duration
	MigrationsTableQuoted bool
	MultiStatementEnabled bool
	MultiStatementMaxSize int
}

View on GitHub (pinned to 01a9643f14)

Solutions

  1. Include the database name in the connection URL path: cockroachdb://user:pass@host:26257/mydb
  2. Or set config.DatabaseName explicitly when using WithInstance
  3. Verify with SELECT CURRENT_DATABASE() that your connection actually has a current database

Example fix

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

Strategy: validation

Validate before calling

u, _ := url.Parse(dsn)
if strings.Trim(u.Path, "/") == "" {
    return errors.New("connection URL must include the database name in the path")
}
if cfg != nil && cfg.DatabaseName == "" {
    cfg.DatabaseName = strings.Trim(u.Path, "/")
}

Try / catch

drv, err := pgx.WithInstance(db, cfg)
if errors.Is(err, pgx.ErrNoDatabaseName) {
    return fmt.Errorf("no database name: set cfg.DatabaseName or use a URL path like postgres://host/db: %w", err)
}

Prevention

When it happens

Trigger: Calling cockroachdb Open with a URL missing the database path (e.g. cockroachdb://user@host:26257/); or pgx WithInstance with an empty config.DatabaseName while the connection has no current database set.

Common situations: DSN templates that drop the trailing /dbname; connecting through a proxy/URL rewrite that strips the path; forgetting that the database must exist and be named in the URL rather than only in query params.

Related errors


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