sql: database is closed / driver: bad connection Go Origin: Application & API Last updated 4 Nov 2026

Go: sql: database is closed

The *sql.DB handle was closed (or the connection died) before the query ran.

Exact message you may see sql: database is closeddriver: bad connectionsql: connection is already closedinvalid connectionsql: transaction has already been committed or rolled back
Seen on: Go

Meaning

db.Close() was called too early — often with defer db.Close() in a function that returns while goroutines still use the handle. sql.DB is a long-lived pool and should be closed once at shutdown. driver: bad connection usually means the server dropped an idle connection; set SetConnMaxLifetime.

Common causes

  • db.Close() in a short-lived function
  • Pool shared after close
  • Server closed idle connections (timeouts, proxies)
  • Connection reset by DB restart
  • Max lifetime longer than server timeout

⚡ Quick fix

  1. Open one *sql.DB at startup and close at shutdown
  2. db.SetConnMaxLifetime(3*time.Minute) and SetMaxIdleConns
  3. Retry on driver.ErrBadConn

Detailed fix by platform

Go

  1. db.SetConnMaxLifetime(3 * time.Minute); db.SetMaxOpenConns(20); db.SetMaxIdleConns(10)

How to diagnose

  1. Close — Called when?
  2. Lifetime — Shorter than server timeout?
  3. Pool — Single shared?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

assignment mismatch: 1 variable but f returns 2 values — Go: assignment mismatch: 1 variable but os.Open returns 2 values (Go)assignment mismatch: …cannot assign to struct field m[k].f in map — Go: cannot assign to struct field users["bob"].Age in map (Go)cannot assign to stru…context canceled — Go: context canceled (rpc error: code = Canceled desc = context canceled) (Go)context canceleddial tcp: connect: connection refused — Go: dial tcp 127.0.0.1:5432: connect: connection refused (Go)dial tcp: connect: co…listen tcp :8080: bind: address already in use — Go: listen tcp :8080: bind: address already in use (Go)listen tcp :8080: bin…MySQL: SSL connection error: SSL is required but the server doesn't support it — MySQL: ERROR 2026 (HY000): SSL connection error: SSL is required but the server doesn't support it (SSL / TLS)MySQL: SSL connection…panic: assignment to entry in nil map — Go: panic: assignment to entry in nil map (Go)panic: assignment to …root certificate file "root.crt" does not exist — PostgreSQL: root certificate file "/home/user/.postgresql/root.crt" does not exist. Either provide the file or change sslmode (SSL / TLS)root certificate file…sql: data…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.