micro/go-micro · error · ErrNoConnection
Database connection not initialized
Error message
Database connection not initialized
What it means
ErrNoConnection is the sentinel error of the postgres store indicating the underlying *sql.DB handle is nil. The sqlStore only opens its database connection lazily via initDB; any operation that reaches the DB before initialization (or after a failed init) finds no connection and returns this error. It signals a store lifecycle problem, not a query problem.
Source
Thrown at store/postgres/postgres.go:43
"net/url"
"regexp"
"strings"
"sync"
"syscall"
"time"
"github.com/lib/pq"
"github.com/pkg/errors"
"go-micro.dev/v6/logger"
"go-micro.dev/v6/store"
)
// DefaultDatabase is the namespace that the sql store
// will use if no namespace is provided.
var (
DefaultDatabase = "micro"
DefaultTable = "micro"
ErrNoConnection = errors.New("Database connection not initialized")
)
var (
re = regexp.MustCompile("[^a-zA-Z0-9]+")
// the sql statements we prepare and use
statements = map[string]string{
"list": "SELECT key, value, metadata, expiry FROM %s.%s WHERE key LIKE $1 ORDER BY key ASC LIMIT $2 OFFSET $3;",
"read": "SELECT key, value, metadata, expiry FROM %s.%s WHERE key = $1;",
"readMany": "SELECT key, value, metadata, expiry FROM %s.%s WHERE key LIKE $1 ORDER BY key ASC;",
"readOffset": "SELECT key, value, metadata, expiry FROM %s.%s WHERE key LIKE $1 ORDER BY key ASC LIMIT $2 OFFSET $3;",
"write": "INSERT INTO %s.%s(key, value, metadata, expiry) VALUES ($1, $2::bytea, $3, $4) ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, metadata = EXCLUDED.metadata, expiry = EXCLUDED.expiry;",
"delete": "DELETE FROM %s.%s WHERE key = $1;",
"deleteExpired": "DELETE FROM %s.%s WHERE expiry < now();",
"showTables": "SELECT schemaname, tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema';",
}
)
View on GitHub (pinned to 24529f1404)
Solutions
- Call Init(store.Database(...), store.Table(...), store.Nodes(postgres://user:pass@host/db)) before any Read/Write/Delete/List call, or use store.NewStore with the URI option so the connection opens eagerly
- Verify the DSN and that Postgres is reachable; if Init returned an error, fix it rather than ignoring it
- Compare errors.Is(err, store.ErrNoConnection) (or the postgres package's ErrNoConnection) and re-initialize or recreate the store before retrying
Example fix
// before
s := postgres.NewStore()
data, err := s.Read("key") // ErrNoConnection
// after
s := postgres.NewStore()
if err := s.Init(store.Nodes("postgres://user:pass@localhost:5432/micro"), store.Database("micro")); err != nil {
log.Fatal(err)
}
data, err := s.Read("key") Defensive patterns
Strategy: validation
Validate before calling
if s == nil {
return store.ErrNoConnection
}
// or check before first use:
if err := st.Init(store.Nodes(dsn)); err != nil { return err } Try / catch
if err := st.Read(key); err != nil {
if errors.Is(err, store.ErrNoConnection) {
// re-init store, then retry
}
return err
} Prevention
- Always call Init() with a valid DSN before the first Read/Write
- Propagate and log the error from Init instead of ignoring it
- Centralize store construction in one factory that guarantees initialization
When it happens
Trigger: Calling store Read/Write/Delete/List on a store/postgres-backed store whose Init() was never called or whose sql.DB failed to open, so s.db is nil when db() dereferences it.
Common situations: Constructing the store with store/store.go NewStore without passing the URI/nodes option; a bad Postgres DSN causing Open() to fail silently and subsequent calls to hit the nil connection; swapping store implementations in tests where the default store was used instead of the initialized postgres one.
Related errors
- model/postgres: create table: %w
- model/postgres: create index: %w
- model/postgres: create: %w
- model/postgres: update: %w
- Couldn't create table
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/71db41cfc00d5960.
Report an issue: GitHub.