Sensorial’Org

RoR: Setting up Postgres

One week in and still stumbling slowly along.

Setting up Postgres with Rails on Windows

  1. Install PostgreSQL. I used the One Click Installer. Version 8.3, at the moment. I left all the default settings, including the default user postgres.
  2. Install the Ruby Postgres driver so that it can talk to the database. From the Ruby console: gem install postgres-pr.
  3. Add Postgres \bin to the PATH environment variable so that Windows will know where to find the program when you’re trying to execute a Postgres command: Start > Settings > Control Panel > System > Advanced > Environment Variables > PATH > edit. Append the path to the Postgres \bin to the end of the value.

Talking to Postgres

  1. Specifying the database option (e.g. rails -d Postgres appName) when you’re creating a new Rails application will configure database.yml with the correct adapter. Otherwise, it’s just a matter of changing the adapter value from sqlite or mysql to postgresql. Similarly, migrating for an existing application will only require changing the settings in database.yml.
  2. Set up the databases. This can be done either through the command line of through the pgAdmin III, the GUI admin panel that comes with PostgresSQL.
    1. Using the command-line: Start > Programs > PostgreSQL 8.3 > SQL Shell
      1. Answer the prompts until you’re connected to the server.
      2. CREATE DATABASE db_name; adhere Rails convention to create and name the 3 databases. If successful, CREATE DATABASE will echo back at you.
    2. Using the GUI: Start > Programs > PostgreSQL 8.3 > pgAdmin III
      1. Double click on the PostgreSQL server under the Object Browser on the left, and you’ll be prompted for your password.
      2. Right click for New Database…: Stick to all the default settings.
  3. Migrate the application tables. Restart the web server, and on the Ruby Console: rake db:migrate.
  4. Verify that the migration was successful:
    1. Using the command-line SQL shell
      1. While you’re still logged in on the server, connect to your database: \c db_name.
      2. Do \dt to list the tables that should have been created during the migration.
    2. Using the GUI
      1. The tables should be under Servers > PostgreSQL 8.3 > Databases > db_name > Schemas > public > tables.

Helpful Links

  1. Converting and Verifying the Migration to PostgreSQL
  2. Switching Rails to Postgres (on Ubuntu)

Comments

Setting Up Postgresql with Rails on Windows (August 13th, 2009, 1:52 pm):

[…] Sensorial’Org] Tags: howto, installation, installing, postgresql, rails, ror, rubyonrails, […]

Leave a Comment