AKRABAT

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 [email protected]...
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 [email protected]...

{
 "max_conns": "5",
 "uri": "postgres://mhdlrgwt:[email protected]: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 [email protected]...
OK

Source: AKRABAT

By Rob