Adding a Database to Your Application

The main focus of this quick introduction is adding one of the OpenShift supported datastores, that is one of the datastore cartridges that runs on an OpenShift gear and that writes its data to the filesystem on the gear.

Adding the Database

You can either add the database cartridge at application creation time or after the application is created. To add it at application creation time, just add it to the list of cartridges for your application:

$ rhc app-create <myappname> <webcartridge> <dbcartridge>

In the example above you can replace <webcartridge> with any web cartridge, like php-5.4 or tomcat7, and <dbcartridge> with any database cartridge, such as postgresql-9.2 or mongodb-2.4. This will spin up your database server for your application and put environment variables in your application for the database connection parameters. If you want to use a downloadable cartridge for your datastore, you would replace <dbcartridge> with the URL used to download the cartridge.

If you want to add a database to an existing application the command is similar:

$ rhc cartridge-add <dbcartridge> --app <myappname>

This command will also spin up your database server and insert the environment variables.

Adding the database after application creation gives you more control over the gear size and environment variables for the cartridge.

To use the datastore in your code, you just need to read in the environment variables for the database connection parameters. The name of the environment variables will be different based on the cartridge you choose.

To see the username and password for your datastore you can use the command line tools:

$ rhc app show <myappname> -v

Database-specific Environment Variables

These variables pertain to your database (if you have one) and are used to connect your application to your database. The exact variable names depend upon the database type; the value of <database> will be MONGODB, MYSQL, or POSTGRESQL as appropriate.

Variable Name Example Purpose


The hostname or IP address to use to connect to your database



The port your database server is listening on



Your database administrative username



Your database administrative user’s password



A AF socket you can use to connect to your database



Database connection URL you can use to connect to your database

Read more about all of the available OpenShift Environment Variables here.