{"record":{"id":"71db41cfc00d5960","repo":"micro/go-micro","slug":"database-connection-not-initialized","errorCode":null,"errorMessage":"Database connection not initialized","messagePattern":"Database connection not initialized","errorType":"exception","errorClass":"ErrNoConnection","httpStatus":null,"severity":"error","filePath":"store/postgres/postgres.go","lineNumber":43,"sourceCode":"\t\"net/url\"\n\t\"regexp\"\n\t\"strings\"\n\t\"sync\"\n\t\"syscall\"\n\t\"time\"\n\n\t\"github.com/lib/pq\"\n\t\"github.com/pkg/errors\"\n\t\"go-micro.dev/v6/logger\"\n\t\"go-micro.dev/v6/store\"\n)\n\n// DefaultDatabase is the namespace that the sql store\n// will use if no namespace is provided.\nvar (\n\tDefaultDatabase = \"micro\"\n\tDefaultTable    = \"micro\"\n\tErrNoConnection = errors.New(\"Database connection not initialized\")\n)\n\nvar (\n\tre = regexp.MustCompile(\"[^a-zA-Z0-9]+\")\n\n\t// the sql statements we prepare and use\n\tstatements = map[string]string{\n\t\t\"list\":          \"SELECT key, value, metadata, expiry FROM %s.%s WHERE key LIKE $1 ORDER BY key ASC LIMIT $2 OFFSET $3;\",\n\t\t\"read\":          \"SELECT key, value, metadata, expiry FROM %s.%s WHERE key = $1;\",\n\t\t\"readMany\":      \"SELECT key, value, metadata, expiry FROM %s.%s WHERE key LIKE $1 ORDER BY key ASC;\",\n\t\t\"readOffset\":    \"SELECT key, value, metadata, expiry FROM %s.%s WHERE key LIKE $1 ORDER BY key ASC LIMIT $2 OFFSET $3;\",\n\t\t\"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;\",\n\t\t\"delete\":        \"DELETE FROM %s.%s WHERE key = $1;\",\n\t\t\"deleteExpired\": \"DELETE FROM %s.%s WHERE expiry < now();\",\n\t\t\"showTables\":    \"SELECT schemaname, tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema';\",\n\t}\n)\n","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/store/postgres/postgres.go#L25-L61","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\ns := postgres.NewStore()\ndata, err := s.Read(\"key\") // ErrNoConnection\n// after\ns := postgres.NewStore()\nif err := s.Init(store.Nodes(\"postgres://user:pass@localhost:5432/micro\"), store.Database(\"micro\")); err != nil {\n\tlog.Fatal(err)\n}\ndata, err := s.Read(\"key\")","handlingStrategy":"validation","validationCode":"if s == nil {\n\treturn store.ErrNoConnection\n}\n// or check before first use:\nif err := st.Init(store.Nodes(dsn)); err != nil { return err }","typeGuard":null,"tryCatchPattern":"if err := st.Read(key); err != nil {\n\tif errors.Is(err, store.ErrNoConnection) {\n\t\t// re-init store, then retry\n\t}\n\treturn err\n}","preventionTips":["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"],"tags":["database","postgres","initialization"],"backgroundTag":"db-connection-not-initialized","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}