Pragmatism in the real world

CLI credentials for a Cloud Foundry database

If you need access to your Cloud Foundry database from the command line the easiest way to get a set of credentials is to create a service key.

This is done using the command cf create-service-key {service-name} {key-name}. You can call the key anything, so to connect to my database that I created previously:

$ cf create-service-key slim-bookshelf-db ROB_CLI
Creating service key ROB_CLI for service instance slim-bookshelf-db as rob@19ft.com...
OK

Now that we’ve set up the key, we can get the information using cf service-key {service-name} {key-name}:

$ cf service-key slim-bookshelf-db ROB_CLI
Getting key ROB_CLI for service instance slim-bookshelf-db as rob@19ft.com...

{
 "max_conns": "5",
 "uri": "postgres://mhdlrgwt:DUfbTLDwPa8aeVfNs3zd93KToPidbC9j@uoixkyek.db.elephantsql.com:5432/mhdlrgwt"
}

The format is: postgres://{user}:{password}@{host name}:{port}/{database}, so you can quite easily extract the details for use with the command line PostgreSQL client.

When you no longer need it, you can delete the key using cf delete-service-key -f {service-name} {key-name}:

$ cf delete-service-key -f slim-bookshelf-db ROB_CLI
Deleting key ROB_CLI for service instance slim-bookshelf-db as rob@19ft.com...
OK

Mapping to the bx CLI tool

IBM’s CloudFoundry instance has its own CLI tool called bx. You can do all the same things, but the commands are different. This is the mapping:

cf command bx command
cf create-service-key bx service key-create
cf service-key bx service key-show
cf delete-service-key bx service key-delete