Using PostgreSQL with Ruby on Rails (and RubyMine)


Everything when I start a new Ruby on Rails project on a new machine I am running into issues getting everything to work with Postgresql. This is not a Postgresql issue, but simply a memory problem. I don’t do this that often, so I simply forget. This has lead me to write this blog, because it will be my ‘extended memory’ next time I need it.

This blogpost was written while using the following tools:

  • Postgresql 9.3 configured on localhost on port 5432
  • Ruby 2.0.0-p0
  • Rails 3.2.13 (nope… not the latest)
  • RubyMine 6

The purpose of this blogpost is to give you a workflow that will help you do the least amount of work to get your new Rails app working with Postgresql. My assumption is that you have installed Postgresql and are sure it is properly running. I use pgAdmin to check that.

Step 1 – First create a new role in Login Roles. In this example we’ll call the role TestProject.

Step 2 – In the tab Role privileges check Can create database

Login_Role_TestProject_and_Edit_Post_‹_Ruud_s_Thoughts_—_WordPress_1

Step 3 – Create a new Ruby on Rails project in RubyMine. This step will also run rake and try to create databases. This will fail for three reasons:

By default RubyMine does not have the Postgresql driver installed. You’ll need to do that manually using the database menus in RubyMine. Later more about that.

Postgresql allows you to create new roles without setting a password, but this is misleading, because it won’t allow connections without a password login. You have to set a password  in the Definition tab. Warning: once you do that the Account expires field will be set to the current date. As a result of that you may still not be able to use the new role. Make sure to set the date to a future date.

Login_Role_TestProject_and_Edit_Post_‹_Ruud_s_Thoughts_—_WordPress

You may receive this error:

could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

Using Google Search you’ll find suggestions that require messing around with file system soft links or moving files. Don’t do that! That won’t be necessary. Just add host: localhost to each database configuration  in database.yml and that will do the job. Your configuration should now look something like depicted below.

database_yml_-_TestProject_-____RubymineProjects_TestProject_

Step 4 – Run rake db:setup followed by rake db:migrate. This will create both your development database and your test database. This will leave the production database untouched for obvious reasons.

You are now set to go and start developing your application and use the regular database migration steps from Rails.

Learning new programming languages and technology makes TDD and BDD hard (for me)


Today I find myself struggling. I have always been a backend server developer, c++, java, c# developer. I am trying to learn web development and mobile (web) development. I have never done any web development with javascript, html, css and that kind of stuff.

For over fifteen years I am a big fan of TDD and BDD. What I want is to development an application for a friend using Ruby on Rails for the backend server development and using Sencha Touch for the mobile web application. One of the reasons I stayed away from web development for a long time is the vast amount of technologies involved. As an eXtreme Programmer I love the idea of keeping things simple and I just never saw how the web development world was keeping things simple. And as a matter of fact this event of wanting to develop a web application for a friend has reminded me in not such a gentle way that this has basically never improved over the past ten year. I believe it only got worse.

Anyway… this is not what this blog is about. Forget about my whining about web development.

This blog is about an interesting phenomena that I ran into the last few weeks. While learning new programming languages, scripting languages and what else I find myself thinking time and again that it is just much easier and much faster to just write the damn code than to fiddle around and writing a proper unit test and applying TDD and BDD. Not only do I have to learn the language I also find myself struggling and learning how to write test for the new languages and technologies I am using.

This makes me wonder if this is a similar experience most people learning TDD and BDD have when they are completely new to these concepts. This probably even worse when you’re actually not an expert in the programming language that you have to use for your work.

I consider myself quite an expert on TDD, but I find myself struggling doing TDD in the new environment that I am working in. I am simply overwhelmed by the vast amount of other things I also need to learn.