Node Redis: Getting All Databases (Detailed Guide w/ Code Examples)

Use Case(s)

In Node.js, using the Redis package, you may need to interact with multiple databases. A common use case is to list all available databases in your Redis instance, for example, during debugging or while handling multi-tenant applications.

Code Examples

Unfortunately, Redis does not directly support a command to get all databases. The number of databases is configured in the Redis configuration file, and by default, it's set to 16. However, you can check the number of existing databases in the redis.conf file.

Alternatively, you could attempt to switch to each database (up to an arbitrary limit like 16) and see if it succeeds. Here's how to do this in Node.js:

const redis = require('redis'); const client = redis.createClient(); for(let i=0; i<16; i++) {, (err) => { if(!err) console.log(`Database ${i} exists`); }); }

This script tries to select each database from 0 to 15 and logs which ones exist. Note that this doesn't necessarily tell you if the database is 'empty' or not.

Best Practices

  • Stick to using a single Redis database unless there's a compelling reason to use more. Multiple databases are not intended as a way to separate different entities (like tables in SQL), and many Redis clients only support database 0.

Common Mistakes

  • Assuming that Redis supports the same database concepts as traditional relational databases. Redis is a data structure server and it has quite different semantics.


Q: Can I increase the number of databases in Redis? A: Yes, you can change this in your redis.conf file by setting the databases directive to a higher value. However, be aware that Redis doesn't intend for these to be used as separate 'tables' – it's often better to use a different key prefix or a completely separate Redis instance.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.