{"id":"09a15ae9d7c80ae6","repo":"redis/redis-py","slug":"invalid-database-09a15a","errorCode":null,"errorMessage":"Invalid Database","messagePattern":"Invalid Database","errorType":"exception","errorClass":"ConnectionError","httpStatus":null,"severity":"error","filePath":"redis/connection.py","lineNumber":1240,"sourceCode":"\n        try:\n            if self.driver_info and self.driver_info.lib_version:\n                self.send_command(\n                    \"CLIENT\",\n                    \"SETINFO\",\n                    \"LIB-VER\",\n                    self.driver_info.lib_version,\n                    check_health=check_health,\n                )\n                self.read_response()\n        except ResponseError:\n            pass\n\n        # if a database is specified, switch to it\n        if self.db:\n            self.send_command(\"SELECT\", self.db, check_health=check_health)\n            if str_if_bytes(self.read_response()) != \"OK\":\n                raise ConnectionError(\"Invalid Database\")\n\n    def disconnect(self, *args, **kwargs):\n        \"Disconnects from the Redis server\"\n        # The server session is gone, so any HIMPORT fieldsets prepared on this\n        # socket no longer exist; reset the tracking.\n        self._reset_himport_state()\n        self._parser.on_disconnect()\n\n        conn_sock = self._sock\n        self._sock = None\n        # reset the reconnect flag\n        self.reset_should_reconnect()\n\n        if conn_sock is None:\n            return\n\n        if os.getpid() == self.pid:\n            try:","sourceCodeStart":1222,"sourceCodeEnd":1258,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/connection.py#L1222-L1258","documentation":"Raised as a ConnectionError when the SELECT command (to switch to a non-zero database) returns a non-'OK' response during connect. This means the server rejected the database index, almost always because the index is out of the valid range configured on the server.","triggerScenarios":"Constructing redis.Redis(db=15) against a server with fewer databases (e.g. redis-server configured with 'databases 8'); passing db=-1 or any index >= the server's database count. The check fires in on_connect after SELECT.","commonSituations":"Default db=0 works but a higher index fails on servers with a reduced 'databases' setting; cluster-mode Redis where SELECT to a non-zero DB is invalid; misconfigured db index from env vars.","solutions":["Use db=0, which is always valid.","Increase the server's 'databases' directive in redis.conf to cover your index.","On Redis Cluster, remember only db=0 is allowed."],"exampleFix":"// before\nr = redis.Redis(host=h, db=15)  # server only has 8 DBs\n// after\nr = redis.Redis(host=h, db=0)","handlingStrategy":"validation","validationCode":"def validate_db(db):\n    if db != 0 and (db < 0):\n        raise ValueError('db must be >= 0')\n    return db\ndb = validate_db(raw_db)","typeGuard":null,"tryCatchPattern":"try:\n    r = redis.Redis(host=h, db=raw_db)\n    r.ping()\nexcept redis.exceptions.ConnectionError as e:\n    if 'Invalid Database' in str(e):\n        r = redis.Redis(host=h, db=0)\n        r.ping()","preventionTips":["Use db=0 unless you have raised the server's 'databases' setting.","On Redis Cluster, always use db=0."],"tags":["connection","configuration","database"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}