Using Postgres on Cloud Foundry |

When the new open source Platform-as-a-Service (PaaS) offering Cloud Foundry from VMware launched earlier this year, it included a relational database service powered by MySQL along with the NOSQL options of MongoDB and Redis. One of the promises of the Open PaaS is to provide choice both in languages and frameworks you can develop with and in the database services that are available to use. We now have a new relational database service using PostgreSQL available. This is great since we can now choose between the two most popular open source relational databases. PostgreSQL is is a very robust and reliable database that has been around for a long time so it definitely has been battle tested.
vFabric Postgres on Cloud Foundry
The new PostgreSQL service is an excellent option for our relational database needs. PostgreSQL was originally developed and optimized to run on physical machines. Now that we are running our databases in the cloud it makes sense to optimize them for that environment. This is exactly what VMware has done for Postgres by creating a version that is optimized for the virtualized cloud environment. To power the PostgreSQL service on cloudfoundry.com the Cloud Foundry team is using vFabric Postgres 9.0, a vSphere-optimized version of Postgres. The vFabric Postgres product is part of the recently announced vFabric Data Director, which is a new database provisioning and operations solution designed to deliver a Database-as-a-Service model for the enterprise. The first database supported on Data Director is vFabric Postgres.
So, is there any difference between vFabric Postgres and regular PostgreSQL from a developers perspective? No, they are functionally identical. You use the same JDBC driver and SQL syntax. The changes made are internal and related to delivering the elasticity and performance required for the cloud.
Building a Book Shelf sample app with Roo
To get started using PostgreSQL on Cloud Foundry you first need an account on Cloud Foundry. Once that is taken care of we can start developing our first database application. The fastest way to write a Spring application is by using Spring Roo which is Spring’s rapid application development tool for Java developers. This of course means that you need to install Roo and the Cloud Foundry add-on.
Now that we have all the prerequisites in place we can get started. First create a directory for the application and open the Roo shell. Once that is opened we can create our project.
roo> project --topLevelPackage org.springsource.data.demo.bookshelf
Now we are ready to configure the persistence options for our Roo app. I'll select Hibernate as the JPA provider and Postgres as the database. There is no need to provide any custom connection properties since we will be running this application in Cloud Foundry. The connection details are automatically managed for us.
org.springsource.data.demo.bookshelf roo> persistence setup --provider HIBERNATE --database POSTGRES
Next we need to create our Entity class for this brief example. I'll create a Book class that will be part of my new BookShelf application. I'm just going to create the Book class for now and will add the Author and any other classes some time in the future.
org.springsource.data.demo.bookshelf roo> entity --class ~.domain.Book ~.domain.Book roo> field string --fieldName title --sizeMax 200 ~.domain.Book roo> field string --fieldName isbn --sizeMax 20 ~.domain.Book roo> field date --fieldName published --type java.util.Date ~.domain.Book roo> field number --fieldName price --type java.math.BigDecimal
Once this is taken care of we are ready to create the web application with a controller for our Book class.
~.domain.Book roo> controller all --package ~.web
We are done, pretty painless if you ask me. Now we need to package everything up, connect to Cloud Foundry and deploy the app.
~.web roo> perform package ~.web roo> cloud foundry login ~.web roo> cloud foundry deploy --appName bookshelf --path /target/bookshelf-0.1.0.BUILD-SNAPSHOT.war
Once the deploy completes we should be able to list our current set of applications
~.web roo> cloud foundry list apps ================================================ Applications ================================================ Name Status Instances Services URLs ---- ------ --------- -------- ---- bookshelf STOPPED 1 bookshelf.cloudfoundry.com
Connecting the app to PostgreSQL on Cloud Foundry
Now we get to the fun part. As you saw above, the application is in a stopped status. We can't start it now since we have not created our database and bound it to the application yet. So let's do that now, but first let's see what data services are available.
~.web roo> cloud foundry list services =================== System Services ==================== Service Version Description ------- ------- ----------- rabbitmq 2.4 RabbitMQ messaging service mongodb 1.8 MongoDB NoSQL store redis 2.2 Redis key-value store service postgresql 9.0 PostgreSQL database service (vFabric) mysql 5.1 MySQL database service
Great, we do have PostgreSQL available as an option. So, let's create a database service instance, bind it to the app and start the app.
~.web roo> cloud foundry create service --serviceName books --serviceType postgresql ~.web roo> cloud foundry bind service --serviceName books --appName bookshelf ~.web roo> cloud foundry start app --appName bookshelf
Let's see what we get when visiting http://bookshelf.cloudfoundry.com
So the app is up and running and we can add and view the books on our bookshelf.
More about PostgreSQL on Cloud Foundry
We saw in the example above that we can connect our applications to PostgreSQL. But how do we know if we are running against a regular PostgreSQL install or vFabric Postgres? One way of determining this is to examine the output of the version() function. It typically says something like "PostgreSQL 9.0.4 on x86_64-pc-linux-gnu, compiled by GCC gcc-4.4.real (Ubuntu 4.4.3-4ubuntu5) 4.4.3, 64-bit". Since the Postgres database running on cloudfoundry.com is based on vFabric Postgres 9.0 we actually see a return value saying "[PostgreSQL 9.0.4 ] vPostgres 1.0 release-v build". Let's see if we can easily add a page to our web app to display some database information. I'll create a DatabaseInfo controller that I can modify.
~.web roo> controller class ~.web.DatabaseInfoController
This creates a controller (bookshelf/src/main/java/org/springsource/data/demo/bookshelf/web/DatabaseInfoController.java) that I've added a DataSource and some database info retrieval code to in the index method.
@RequestMapping("/databaseinfo/**")
@Controller
public class DatabaseInfoController {
@Autowired
DataSource dataSource;
@RequestMapping
public void get(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
}
@RequestMapping(method = RequestMethod.POST, value = "{id}")
public void post(@PathVariable Long id, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
}
@RequestMapping
public String index(ModelMap modelMap) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String userInfo = jdbcTemplate.queryForObject("select user", String.class);
String urlInfo = "?";
if (dataSource instanceof BasicDataSource) {
urlInfo = ((BasicDataSource) dataSource).getUrl();
}
String versionInfo = jdbcTemplate.queryForObject("select version()", String.class);
modelMap.put("userInfo", userInfo);
modelMap.put("urlInfo", urlInfo);
modelMap.put("versionInfo", versionInfo);
return "databaseinfo/index";
}
}
There is also a JSP file (bookshelf/src/main/webapp/WEB-INF/views/databaseinfo/index.jspx) that is used by this controller and that needs to have some code added.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" version="2.0">
<jsp:directive.page contentType="text/html;charset=UTF-8"/>
<jsp:output omit-xml-declaration="yes"/>
<spring:message code="label_databaseinfo_index" htmlEscape="false" var="title"/>
<util:panel id="title" title="${title}">
<spring:message code="application_name" htmlEscape="false" var="app_name"/>
<h3>
<spring:message arguments="${app_name}" code="welcome_titlepane"/>
</h3>
<p> The database user is ${userInfo}. </p>
<p> The dataSource URL is ${urlInfo}. </p>
<p> The database version is ${versionInfo}. </p>
</util:panel>
</div>
Now we can build and deploy the modified application to the cloud.
~.web roo> perform package ~.web roo> cloud foundry deploy --appName bookshelf --path /target/bookshelf-0.1.0.BUILD-SNAPSHOT.war
Accessing the application we can see a new link to the Database Info Controller View that returns the following page:
As you can see we are using vFabric Postgres in our cloud deployed app.
Can I run vFabric Postgres in my own server or private cloud?
Yes, you can get vFabric Postgres for development or production. You will also need the companion product vFabric Data Director to manage the database instances. vFabric Data Director is a software solution that enables you to provide Database-as-a-Service (DBaaS) for your cloud. It is designed for the cloud environment and is capable of managing thousands of databases while offering application developers self-service database management.
See Jignesh Shah's blog for more info on this new product.
Similar Posts
- Using Cloud Foundry Services with Spring: Part 1 – The Basics
- Using Cloud Foundry Services with Spring: Part 2 – Auto-reconfiguration
- Using Micro Cloud Foundry from Grails
- Using Cloud Foundry Services with Spring: Part 4 – Spring Profiles
- Micro Cloud Foundry for Spring Developers






Jan says:
Added on August 31st, 2011 at 3:01 amDoe the vFabric PostgreSQL installation support PostGIS (http://postgis.refractions.net)?
Pavla says:
Added on September 1st, 2011 at 3:41 pmGreat work, … I'm also very interested in PostGIS extension support – is it available for now or is it planned to be supported in near future?
bennet says:
Added on September 3rd, 2011 at 3:22 amGreat work there,Very much interested to see more and detailed extension support on PostGIS. http://webinarkings.com/
crusher says:
Added on September 6th, 2011 at 8:57 pmQuality products
good work.http://www.crusher.so
Eyeliner says:
Added on September 8th, 2011 at 3:18 pmNoch Mehr zum Thema wäre super.
moncler jackets outlets says:
Added on September 9th, 2011 at 12:06 amYour point of view very creative, very exciting article.
cheap canada goose jackets says:
Added on September 9th, 2011 at 12:10 amYour point of view very creative, very exciting article.
north face jackets discount says:
Added on September 17th, 2011 at 3:10 amThanks for writing this post.I think it'll be helpful to me
north face jackets discount says:
Added on September 17th, 2011 at 3:22 amYour point of view very creative, very exciting article.
Gordon Dickens says:
Added on September 20th, 2011 at 4:51 pmTom,
Is the Cloud Foundry addon available for Roo 1.2? It is not in the default install for 1.2.M1.
Regards,
Gordon Dickens
2023xqp says:
Added on September 21st, 2011 at 8:53 pmNike produces a wide range of sports equipment. Their first products were track running shoes. They currently also make shoes, jerseys, shorts, baselayers, etc. for a wide range of sports including track and field, basketball,ice hockey, tennis, association football (soccer), lacrosse, basketball, and cricket. Nike Air Max is a line of shoes first released by Nike, Inc. in 1987. The most recent additions to their line are the Nike 6.0, Nike NYX, andNike SB shoes, designed for skateboarding. Nike has recently introduced cricket shoes, called Air Zoom Yorker, designed to be 30% lighter than their competitors'.[16] In 2008, Nike introduced the Air Jordan XX3, a high-performance basketball shoe designed with the environment in mind.
Data Entry India says:
Added on September 22nd, 2011 at 4:33 amThank you for nice article.
tk says:
Added on September 23rd, 2011 at 12:39 pmWe have been looking at cloud foundry and PostGIS is our limitation as well.
Gordon Dickens says:
Added on September 23rd, 2011 at 2:34 pmI was able to get it working using the dev build from GitHub. I posted a blog at: http://gordondickens.com/wordpress/2011/09/23/rocket-to-the-cloud-fast-with-roo/ that might be helpful.
Regards,
Gordon Dickens
jbbarquero (Javier) says:
Added on October 3rd, 2011 at 1:45 pmNice tutorial.
I've followed it but using STS and the VMC command line instead of Roo.
In any case, I have a doubt, how is the database created? I mean, how can I manage the database? (mainly, creation of tables)
I deployed the application with "hibernate.hbm2ddl.auto" to "create" within the persistence.xml file, and then re-deploy with "validate" value, but I don't think it should a good idea for production environments. Besides, I don't know how to add more tables/columns/whatever I need when the application grows.
Thanks, and good job with Cloud Foundry.
moncler online says:
Added on November 25th, 2011 at 10:14 pmExcellent site. Lots of useful info here. I'm sending it to several pals ans also sharing in delicious. And naturally, thanks in your sweat!
Cheap Uggs Boots says:
Added on December 3rd, 2011 at 7:31 amIt's actually a cool and helpful piece of information. I am glad that you shared this helpful information with us. Please stay us informed like this. Thank you for sharing.
Phentermine-Urine-Dr says:
Added on December 16th, 2011 at 10:38 pm|
Fluoxetine-Price says:
Added on December 17th, 2011 at 7:06 am|
Zoloft-Birth-Defects says:
Added on December 17th, 2011 at 2:37 pm|
Meron says:
Added on March 14th, 2012 at 8:27 amCan you explain how to bind to the DB service using the STS environment?
Gordon Dickens says:
Added on March 14th, 2012 at 10:37 amMeron,
If you have added CloudFoundry to your servers, you double click on the server. 1. Deploy your app, 2. Select services from the "application tab". 3. Drag it to the "Application Services" panel on the right.
HTH,
Gordon Dickens
@gdickens
Meron says:
Added on March 14th, 2012 at 3:07 pmThanks for the reply Gordon. Did that already with my app.
I'm probably missing here something…
I'm using PostgreSQL, can you tell me how do I get the "PostgreSQL database service"? I don't see it in the services list on the application tab.
Gordon Dickens says:
Added on March 26th, 2012 at 4:19 pmThat is odd, I would definitely reach out on http://support.cloudfoundry.com/home.