36

I am used to psql which I can use by feeding it the connection string without having to break it in different arguments, that is,

psql postgres://<username>:<password>@<host>:<port>

This is useful when I have such string from Heroku, for example. Can I do something similar with redis-cli? I want to feed it directly a connection string, such as the one that is stored on Heroku as environment variable when I install a Redis add-on. Is that possible? Example of the syntax I would like to use:

redis-cli redis://<username>:<password>@<host>:<port>
Alessandro Cosentino
  • 2,268
  • 1
  • 21
  • 30

2 Answers2

59

No, at the moment (v3.2.1) redis-cli does not support the URI connection schema. If you want, you can make a feature or pull request for that in the Redis repository.

UPDATE: The -u option was released with Redis 4.0, see Release notes. For example:

redis-cli -u redis://user:pass@host:6379/0
Itamar Haber
  • 47,336
  • 7
  • 91
  • 117
12

For those who cannot wait for the next Redis release won't be able to update their redis-cli, I made a simple bash function that seems to work for my case (Heroku). This is to be added to ~/.bash_profile or ~/.bashrc:

function redis-url() {
  # Get the first argument as the URL variable
  url=$1
  # Parse and generate the command: redis-cli -h [hostname] -p [port] -a [password]
  cmd=`echo $url | sed 's_redis://\(.*\):\(.*\)@\(.*\):\(.*\)_redis-cli -h \3 -p \4 -a \2_'`
  # Run the command
  $cmd
}

I can then type the following in my terminal:

redis-url redis://rediscloud:mysupersecurepassword@hostname.com:1234
Bruno A.
  • 1,765
  • 16
  • 17
  • FYI, the `-u` option from Itamar's PR has already been released with Redis 4 and it's in redis-cli now. – Alessandro Cosentino Dec 12 '17 at 16:19
  • Really? Which version is it exactly? It's still [on the unstable branch](https://github.com/antirez/redis/compare/4.0.6...unstable#diff-12ccdbf958c39a07b5dc785c68c8ebd3R1203) in the linked repo at the moment. The download page is currently listing 4.0.6 as the latest. – Bruno A. Dec 13 '17 at 10:31
  • I am running 4.0.6 and the feature is there. Maybe the unstable branch is stale? – Alessandro Cosentino Dec 13 '17 at 11:01
  • I suggest you to change `$cmd` to `eval $cmd` – tkhuynh Feb 12 '21 at 22:44