I work as a Ruby on Rails contractor for Enlight Solutions, and recently I got involved in a new project for which we wanted to use PostgreSQL because we were going to use Heroku for staging the application. Since I never used PostgreSQL on a project before, I just wanted to share with you how I got it up and running on my Ubuntu 9.10 machine.
The first thing you need to do in order to get it working is to install the postgresql package:
sudo apt-get install postgresql
Now install the postgresql-client package so that you can interact with PostgreSQL in the terminal:
sudo apt-get install postgresql-client
Then you need to install the pg gem to get the Ruby bindings for PostgreSQL:
sudo gem install pg
Next, run the following command to enter the PostgreSQL console as superuser:
sudo -u postgres psql
postgres is the user of PostgreSQL on your machine. In the console, run this command:
CREATE USER #{your_unix_username}
Replace #{your_unix_username} with the username you use to log in to your machine, in my case it’s just “david”. We’re not specifying a password because it’s our development environment – in production you should of course strive to keep things secure. Now let’s test if it’s working. First generate a Rails project which uses PostgreSQL:
rails pg_test -d postgresql
That should work just fine no matter what. Even if you haven’t installed PostgreSQL on your machine. However, if you haven’t installed PostgreSQL properly this will not work:
rake db:create
Run it and see what happens. It should work if you have followed the instructions above. Please note that you do not have to edit config/database.yml with your username. By default Rails will put the name of the application there, because that’s how you would usually set it up in production environment: create a separate user for each application. In this case, however, it works even if you leave username and password blank because we named our PostgreSQL user after our UNIX user. To illustrate the process of authentication inside PostgreSQL:
- Does “username” match a user on the machine?
- No
- Normal authentication procedure
Please leave feedback if there’s something you think should be corrected or if you have difficulties getting it to work. Hope you found it useful!