The error occurs because you are executing a query on a *sql.DB connection that has already been closed. Ensure you call db.Close() only after all queries are finished and the program is shutting down.
// Incorrect: Closing immediately after opening
db, _ := sql.Open("postgres", "conn")
db.Close()
rows, _ := db.Query("SELECT 1") // Error: database is closed
// Correct: Close only at the end of the program
db, _ := sql.Open("postgres", "conn")
rows, _ := db.Query("SELECT 1")
rows.Close()
db.Close()
If you are using a connection pool or multiple goroutines, ensure no goroutine calls db.Close() while others are still using the connection.