Skip to main content
0 reactions

How to Switch Databases in Valkey with SELECT

valkey-cli session showing CONFIG GET databases returning 16, SELECT 1 changing the prompt to 127.0.0.1:6379[1], and a key saved in database 1 returning nil from database 0.
Each database keeps its own keys.

A Valkey® server holds several separate databases, numbered from 0. Each one has its own keys. SELECT switches the database your connection uses.

Syntax

Plain text
SELECT <index>
  • <index>: the database number, from 0 up to one less than the databases setting. With the default of 16, that’s 0 to 15.

From the SELECT documentation (opens in a new tab).

Prerequisites

  • Valkey installed and running, for example with How to Install Valkey on macOS.
  • A terminal. The valkey-cli commands are the same on macOS, Ubuntu and Windows (inside WSL).

Step 1: See How Many Databases There Are

Bash
valkey-cli
Plain text
CONFIG GET databases
Plain text
1) "databases"
2) "16"

This Valkey has 16 databases. valkey-cli starts in database 0.

Step 2: Switch to Database 1

Plain text
SELECT 1
Plain text
OK

The prompt changes to 127.0.0.1:6379[1]>, so you can always see which database you’re in.

Step 3: See That Each Database Keeps Its Own Keys

Plain text
SET cart:42 "book:4"
Plain text
OK
Plain text
GET cart:42
Plain text
"book:4"
Plain text
SELECT 0
Plain text
OK
Plain text
GET cart:42
Plain text
(nil)

Database 0 doesn’t see the key you saved in database 1.

valkey-cli session showing CONFIG GET databases returning 16, SELECT 1 changing the prompt to 127.0.0.1:6379[1], and a key saved in database 1 returning nil from database 0.
Each database keeps its own keys.

Clean Up

Plain text
SELECT 1
Plain text
OK
Plain text
DEL cart:42
Plain text
(integer) 1
Plain text
exit

Common Mistakes

MistakeWhat happensDo this instead
Forgetting which database you’re inA key you saved seems to be missing, and GET answers (nil)Check the [1] in the prompt, and SELECT the right database
Expecting the database to stay selected after exitA new valkey-cli starts in database 0 againStart it with valkey-cli -n 1

FAQ

Can I open valkey-cli straight into another database?

Yes. valkey-cli -n 1 starts in database 1, and its prompt shows [1].

Should different apps share one Valkey with different databases?

Valkey’s SELECT documentation suggests using databases to separate data that belongs to the same app, and a separate Valkey server for unrelated apps.

Does Valkey Cluster support SELECT?

Yes. Valkey 9.0 and later support several databases in cluster mode, and SELECT switches between them there too.

Conclusion

SELECT moves your connection to another numbered database, and the prompt shows where you are. Each database keeps its own keys.

Additional Resources

Valkey® is a registered trademark of LF Projects, LLC. Redis® is a registered trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with the Valkey project, LF Projects or Redis Ltd., and isn’t endorsed, supported or certified by them.

0 comments