Redis How to get current database name
Asked Answered
L

2

11

Is there any command in Redis to get the database name that I am using right now? I know select is to switch the database. I am looking for "get database name" kind of a command.

Lupus answered 25/5, 2018 at 17:48 Comment(2)
How about - redis.io/commands/infoContainerize
@sricheta ThankYou! Is there any command to get #Keyspace from Info? Then parsing inside my application to fetch db name becomes simpleLupus
W
17

First of all, there's NO name for Redis database. Instead, it has an index.

You can use the CLIENT SETNAME and CLIENT LIST commands to get the database index dynamically:

  1. call CLIENT SETNAME a-unique-name to set a unique name for the current connection.
  2. call CLIENT LIST to get info of all clients that connecting to Redis.
  3. find the connection info with the unique name that we set in step 1.
  4. parse the client info the get the database index.

You can get the format of client info from the doc.

NOTE: If anyone has a simpler solution, please let me know :)

UPDATE: Since Redis 6.2.0, you can use CLIENT INFO to get the information of current connection. So that you don't need to run step 1 - 3 mentioned above.

Wintertide answered 27/5, 2018 at 8:2 Comment(0)
E
5

Since Redis version 6.2.0 you can use the CLIENT INFO command, the response of which contains both the name and the db number of the current client connection.

Elswick answered 7/7, 2021 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.