Billionmail/BillionMail · critical

database connection test failed after 10 attempts

Error message

database connection test failed after 10 attempts

What it means

After SetConfig, InitDatabase probes the DB by running SELECT 1 up to 10 times. If none of the 10 attempts succeed, it concludes the database is unreachable and returns this fixed error instead of the last driver error.

Source

Thrown at core/internal/service/database_initialization/database_initialization.go:61

	// Testing database connection until successful
	connectionOK := false
	for i := 0; i < 10; i++ {
		_, err = g.DB().Exec(context.Background(), "SELECT 1")

		if err != nil {
			// Wait for 5 seconds before retrying
			g.Log().Debug(context.Background(), "Database connection failed, retrying in 5 seconds...")
			time.Sleep(time.Second * 5)
			continue
		}

		connectionOK = true
		g.Log().Debug(context.Background(), "Database connection successful")
		break
	}

	if !connectionOK {
		return fmt.Errorf("database connection test failed after 10 attempts")
	}

	// Execute registered handlers
	for _, handler := range registeredHandlers {
		if handler != nil {
			handler()
		}
	}

	// Empty the registered handlers
	registeredHandlers = registeredHandlers[:0]

	return nil
}

// registerHandler registers a handler for the database initialization
func registerHandler(handler func()) {
	if handler == nil {

View on GitHub (pinned to fc36c76c05)

Solutions

  1. Add a healthcheck + depends_on condition so the app waits for postgres before InitDatabase runs
  2. Verify DBHOST/DBPORT/DBUSER/DBPASS match the postgres container's settings
  3. Check postgres container logs for startup/auth errors (docker logs postgres)
  4. Increase retry count/backoff if the DB legitimately takes longer than 10 attempts to become ready

Example fix

// before (docker-compose.yml)
app:
  depends_on: [postgres]
// after
app:
  depends_on:
    postgres:
      condition: service_healthy
Defensive patterns

Strategy: retry

Validate before calling

conn, err := net.DialTimeout("tcp", net.JoinHostPort(dbHost, dbPort), 3*time.Second)
if err != nil { return fmt.Errorf("postgres not reachable yet: %w", err) }

Try / catch

if err := database_initialization.InitDatabase(); err != nil {
    if strings.Contains(err.Error(), "connection test failed after 10 attempts") {
        // back off and retry whole init
        time.Sleep(10 * time.Second)
        return retryInit()
    }
}

Prevention

When it happens

Trigger: PostgreSQL container not yet listening on its port within the ~10-retry window; wrong DBHOST/DBPORT; wrong credentials; DB volume uninitialized so postgres keeps restarting.

Common situations: First boot where the app starts before postgres is healthy (missing depends_on/healthcheck); credentials mismatch after changing DBPASS; network misconfiguration between containers; postgres crash-looping.

Related errors


AI-assisted analysis of Billionmail/BillionMail@fc36c76c05 (2026-09-05). Data as JSON: /api/errors/2a9f96cb1363c4be. Report an issue: GitHub.