redis/redis-py · error · ConnectionError

Invalid Database

Error message

Invalid Database

What it means

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.

Source

Thrown at redis/connection.py:1240

        try:
            if self.driver_info and self.driver_info.lib_version:
                self.send_command(
                    "CLIENT",
                    "SETINFO",
                    "LIB-VER",
                    self.driver_info.lib_version,
                    check_health=check_health,
                )
                self.read_response()
        except ResponseError:
            pass

        # if a database is specified, switch to it
        if self.db:
            self.send_command("SELECT", self.db, check_health=check_health)
            if str_if_bytes(self.read_response()) != "OK":
                raise ConnectionError("Invalid Database")

    def disconnect(self, *args, **kwargs):
        "Disconnects from the Redis server"
        # The server session is gone, so any HIMPORT fieldsets prepared on this
        # socket no longer exist; reset the tracking.
        self._reset_himport_state()
        self._parser.on_disconnect()

        conn_sock = self._sock
        self._sock = None
        # reset the reconnect flag
        self.reset_should_reconnect()

        if conn_sock is None:
            return

        if os.getpid() == self.pid:
            try:

View on GitHub (pinned to da03cdc7e8)

Solutions

  1. Use db=0, which is always valid.
  2. Increase the server's 'databases' directive in redis.conf to cover your index.
  3. On Redis Cluster, remember only db=0 is allowed.

Example fix

// before
r = redis.Redis(host=h, db=15)  # server only has 8 DBs
// after
r = redis.Redis(host=h, db=0)
Defensive patterns

Strategy: validation

Validate before calling

def validate_db(db):
    if db != 0 and (db < 0):
        raise ValueError('db must be >= 0')
    return db
db = validate_db(raw_db)

Try / catch

try:
    r = redis.Redis(host=h, db=raw_db)
    r.ping()
except redis.exceptions.ConnectionError as e:
    if 'Invalid Database' in str(e):
        r = redis.Redis(host=h, db=0)
        r.ping()

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of redis/redis-py@da03cdc7e8 (2026-08-04). Data as JSON: /data/errors/09a15ae9d7c80ae6.json. Report an issue: GitHub.