Fix Redis 'ERR DB index is out of range' When Using SELECT

beginner๐Ÿ”ด Redis2026-04-21| Redis 5.x / 6.x / 7.x on Linux, macOS, Docker โ€” any environment using the SELECT command

Error Message

ERR DB index is out of range
#redis#select#database#config#databases

The Error

You run a SELECT command in Redis โ€” maybe in your app code, a client library, or directly in redis-cli โ€” and get this:

127.0.0.1:6379> SELECT 16
(error) ERR DB index is out of range

Or in your application logs:

ERR: Redis error: ERR DB index is out of range

Usually this surfaces after a config change, a migration to a new Redis server, or when someone bumped up the database index in the codebase without realizing the server wasn't configured to match.

Why This Happens

Redis supports multiple logical databases within a single instance. The default is 16 databases, numbered 0 through 15. SELECT switches between them.

The error fires the moment you pass an index equal to or greater than the configured database count. With the default of 16, SELECT 16 fails โ€” valid indices stop at 15.

A few common triggers:

  • Your code references database index 16 or higher, but Redis caps out at index 15 by default
  • A new Redis server was provisioned with a lower databases value โ€” sometimes just databases 1
  • A managed service like Redis Cloud, ElastiCache, or Upstash only exposes database 0
  • Redis is running in Cluster mode, which hard-limits you to database 0

Quick Fix: Check What Index You're Selecting

Start by confirming exactly where the boundary is on your instance:

redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> SELECT 0   # works
127.0.0.1:6379> SELECT 15  # works (last valid with default config)
127.0.0.1:6379> SELECT 16  # fails โ†’ ERR DB index is out of range

Then check the configured database count directly:

127.0.0.1:6379> CONFIG GET databases
1) "databases"
2) "16"

Output of 1 means only database 0 exists. Output of 16 means valid indices run from 0 to 15.

Using a client library? Track down where the db parameter is set:

# Python (redis-py)
import redis
r = redis.Redis(host='localhost', port=6379, db=16)  # โ† this will fail
// Node.js (ioredis)
const redis = new Redis({ host: 'localhost', port: 6379, db: 16 }); // โ† same issue

Change db to something within range โ€” usually 0 to 15 is all you have.

Fix the Redis Config: Increase the Number of Databases

Need an index higher than 15? Raise the databases setting in redis.conf.

Step 1 โ€” Find your redis.conf

# Common locations
/etc/redis/redis.conf
/etc/redis.conf
/usr/local/etc/redis.conf

# Or find it via the running process
ps aux | grep redis

Step 2 โ€” Edit the databases directive

sudo nano /etc/redis/redis.conf

# Find this line:
databases 16

# Bump it to whatever you need:
databases 32

Step 3 โ€” Restart Redis

# systemd
sudo systemctl restart redis

# older init
sudo service redis-server restart

Step 4 โ€” Verify the change took effect

redis-cli CONFIG GET databases
# Expected:
# 1) "databases"
# 2) "32"

# Test the previously failing command
redis-cli SELECT 16
# Expected: OK

No Restart Option? Doesn't Exist for This Setting

CONFIG SET lets you change many Redis settings live, but databases isn't one of them. Don't waste time trying:

127.0.0.1:6379> CONFIG SET databases 32
(error) ERR Unknown option or number of arguments for CONFIG SET - 'databases'

A restart is unavoidable. Schedule a brief maintenance window, or promote a replica first to keep things running while the primary reboots.

Docker: Pass the Setting as a Command Argument

Running Redis in Docker without a mounted config file? Skip the config file altogether and pass the flag directly:

docker run -d \
  --name redis \
  -p 6379:6379 \
  redis:7 redis-server --databases 32

For Compose:

services:
  redis:
    image: redis:7
    command: redis-server --databases 32
    ports:
      - "6379:6379"

Managed Redis (ElastiCache, Upstash, Redis Cloud)

Managed services lock you to database 0. Full stop. There's no config panel to change this โ€” it's a platform-level constraint baked into how these services are architected.

The practical workaround is key namespacing. Instead of splitting data across databases, prefix your keys:

# Old approach โ€” won't work on managed Redis:
SELECT 2
SET user:123 "alice"

# Managed-Redis-friendly approach:
SET app2:user:123 "alice"

Keys like app1:user:123 and app2:user:123 coexist in database 0 without conflict. It's a minor adjustment in naming convention, not a redesign.

Redis Cluster: SELECT Is Off-Limits

Cluster mode doesn't support multiple databases at all โ€” only database 0 works. The reason: Cluster shards data across nodes using hash slots, and supporting multiple databases would break that model entirely. Any SELECT N where N is anything other than 0 will throw this error.

On Cluster, remove all SELECT calls and switch to key namespacing (same pattern as managed Redis above).

Verification

Once you've applied a fix, run through this quickly:

# 1. Confirm databases count
redis-cli CONFIG GET databases

# 2. Test the SELECT that was failing
redis-cli SELECT 16
# Should return: OK

# 3. Write and read a key in the new database
redis-cli -n 16 SET test_key "hello"
redis-cli -n 16 GET test_key
# Should return: "hello"

# 4. Restart your app and confirm the error is gone from the logs

Related Error Notes