Stuck? Need startup advice?

Connecting to SQL Server from Heroku with FreeTDS. Here is how on Cedar...

by Michiel Sikkes on September 20, 2012 with comments

We are currently working on an application for a client that is going to be built on top of an existing MS SQL database. For staging and demo-ing purposes, we wanted to use Heroku to show off our progress every once in a while.

For using ActiveRecord with Rails and MS SQL server, we are using the excellent activerecord-sqlserver-adapter. This uses the tiny_tds gem that in it's turn needs FreeTDS to run.

FreeTDS was built in on the old Heroku Bamboo stack but unfortunately it was removed in the Cedar stack.

Luckily, Heroku introduced Buildpacks. Buildpacks are a way of rolling your own app deployment environment on top of Heroku's infrastructure.

After a few hours of tinkering and trying, I finally managed to assemble a buildpack based off the original Heroku Ruby build pack, which is used for Rack and Rails 2 and Rails 3 apps: heroku-buildpack-ruby-freetds.

Using this FreeTDS buildpack on Heroku

To use this buildpack you can pass in an option when creating your Heroku app:

heroku create my_new_sqlserver_app --buildpack

Or for current apps:

heroku config:add BUILDPACK_URL=

Configuring your database connection

After creating your app or setting up your existing app to use the buildpack, you need to modify the DATABASE_URL config variable to point to your sqlserver instance. We currently use a SQL Server 2008 Express edition:

heroku config:add DATABASE_URL=sqlserver://username:password@sqlserver_host:1433/database_name

Issues and questions

If you have any issues when configuring or using the Buildpack, please create an issue on GitHub.

Other Rails and SQLServer related questions? Just get in touch! :)

comments powered by Disqus