Setting up Rails 3 with PostgreSQL on Windows

The following guide shows you how to seperately install everything needed to get Rails 3 with PostgreSQL on Windows up and running.

Installing Ruby

Go to rubyinstaller.org and download Ruby and DevKit (Development Kit, needed to build the PostgreSQL-extension ‘pg’).
Simply install Ruby by following the instructions on screen (I also checked “Add Ruby executables to your PATH”):

Ruby install path (C:\Ruby192) and options

After the installation has finished you can continue with adding DevKit to your Ruby installation.

Adding DevKit to Ruby

Since DevKit is a self-extracting archive, you don’t need to install it so you can extract its content to lets say C:\DevKit. To merge it into your Ruby installation you have to run the installation script in the following order:

  1. Open the windows console and change into the DevKit directory: cd C:\DevKit
  2. Type ruby dk.rb init to generate the file config.yml
  3. Enter ruby dk.rb install to finally install DevKit into your Ruby.

If everything went OK with DevKit, your console output should look exactly like this:

Console output from DevKit

For additional information on the DevKit installation and testing visit their wiki at github.

Installing PostgreSQL

Visit the project’s official page and download the one click installer.
Very important: Choose an installation directory which doesn’t contain any spaces (I chose C:\PostgreSQL\9.0 as seen below)…

PostgreSQL installation directory (C:\PostgreSQL\9.0)

When you decide to select a Postgres installation path containing white-spaces, you’ll probably run into trouble later when using bundle to include pg with an error ending with –includedir Errno::ENOENT so you’d better select an install path without any space in.
Don’t forget to extend your PATH variable so that it includes the bin and lib directories of your PostgreSQL installation:

PATH variable extended with PostgreSQL lib and bin directory

Installing Rails

Open a new console to make sure you already have the recently extended path variable. Then just type
gem install rails
to fetch the latest version of the Rails-Framework.
Now, we’re almost done.
Create a new rails project by typing
rails new appname -d postgresql

The -d parameter tells rails to add the PostgreSQL database settings so you only have to add your connection parameters in config/database.yml.

Next, you must actually add support for PostgreSQL by adding pg using the bundle installer.
Change into your newly created project directory and type
bundle config build.pg --with-pg-config=C:\PostgreSQL\9.0\bin\pg_config.exe

Now that you’ve added the config parameters for pg you can type
bundle install
and pg will be added.
Don’t forget to create your database using
rake db:create
Fire up the development server with rails server and open the URL http://localhost:3000. After clicking About your application’s environment you should see your environment information like below:

Output of the Rails environment

Congratulations

Now you have successfully set up Rails 3 with PostgreSQL on Windows!
If something goes wrong during installation, please leave a post. I will answer it as soon as possible!
Thanks for reading, I hope this how-to can help you a bit.

 

21 Responses to “Setting up Rails 3 with PostgreSQL on Windows”

  1. phy Says:

    I get this error when I run “bundle install”.

    Can’t find the Postgresql client library (libpq).

    Windows 7 (x86-64).

  2. phy Says:

    Seems like I’m not the only one having trouble install “pg”. Check: https://bitbucket.org/ged/ruby-pg/issue/62/attempt-to-install-pg-following

  3. phy Says:

    This is the full error report when I do “bundle install”:

    C:/Ruby192/bin/ruby.exe extconf.rb
    checking for pg_config… yes
    Using config values from c:\PostgreSQL\9.0\bin/pg_config.exe
    checking for libpq-fe.h… yes
    checking for libpq/libpq-fs.h… yes
    checking for PQconnectdb() in -lpq… no
    checking for PQconnectdb() in -llibpq… no
    checking for PQconnectdb() in -lms/libpq… no
    Can’t find the PostgreSQL client library (libpq)
    *** extconf.rb failed ***
    Could not create Makefile due to some reason, probably lack of
    necessary libraries and/or headers. Check the mkmf.log file for more
    details. You may need configuration options.

    Provided configuration options:
    –with-opt-dir
    –without-opt-dir
    –with-opt-include
    –without-opt-include=${opt-dir}/include
    –with-opt-lib
    –without-opt-lib=${opt-dir}/lib
    –with-make-prog
    –without-make-prog
    –srcdir=.
    –curdir
    –ruby=C:/Ruby192/bin/ruby
    –with-pg
    –without-pg
    –with-pg-dir
    –without-pg-dir
    –with-pg-include
    –without-pg-include=${pg-dir}/include
    –with-pg-lib
    –without-pg-lib=${pg-dir}/lib
    –with-pg-config
    –without-pg-config
    –with-pg_config
    –without-pg_config

  4. phy Says:

    There seems to be an issue with 0.10.1. Use pg version 0.9.0.

  5. phy Says:

    gem install pg -v=0.9.0

  6. netpie Says:

    I am using pg 0.10.1 and it works on both 32-bit and 64-bit Windows 7. Did you extend the PATH as mentioned with the bin and lib directories of your PostgreSQL-installation?

  7. phy Says:

    I did exactly as in your instructions. This is the part which I added to my PATH:
    ;c:\PostgreSQL\9.0\bin;c:\PostgreSQL\9.0\lib

  8. sudhir kumar Says:

    C:\Ruby\Test>rake db:create
    (in C:/Ruby/Test)
    fe_sendauth: no password supplied
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/postgresql_adapter.rb:929:in `initialize’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/postgresql_adapter.rb:929:in `new’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/postgresql_adapter.rb:929:in `connect’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/postgresql_adapter.rb:228:in `initialize’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/postgresql_adapter.rb:25:in `new’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/postgresql_adapter.rb:25:in `postgresql_connection’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:229:in `new_connection’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:237:in `checkout_new_connection’

    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:191:in `block (2 levels) in chec
    kout’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:187:in `loop’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:187:in `block in checkout’
    C:/Ruby/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:186:in `checkout’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:94:in `connection’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:317:in `retrieve_connection’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_specification.rb:97:in `retrieve_connect
    ion’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/railties/databases.rake:92:in `rescue in create_database’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/railties/databases.rake:39:in `create_database’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/railties/databases.rake:33:in `block (2 levels) in ‘
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:636:in `call’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:636:in `block in execute’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:631:in `each’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:631:in `execute’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:597:in `block in invoke_with_call_chain’
    C:/Ruby/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:583:in `invoke’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2029:in `block (2 levels) in top_level’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2029:in `each’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2029:in `block in top_level’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2001:in `block in run’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:1998:in `run’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/bin/rake:31:in `’
    C:/Ruby/bin/rake:19:in `load’
    C:/Ruby/bin/rake:19:in `’
    Couldn’t create database for {“adapter”=>”postgresql”, “encoding”=>”unicode”, “database”=>”Test_test”, “pool”=>5, “username”=>”Test”, “password”=>nil}

    fe_sendauth: no password supplied
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/postgresql_adapter.rb:929:in `initialize’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/postgresql_adapter.rb:929:in `new’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/postgresql_adapter.rb:929:in `connect’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/postgresql_adapter.rb:228:in `initialize’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/postgresql_adapter.rb:25:in `new’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/postgresql_adapter.rb:25:in `postgresql_connection’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:229:in `new_connection’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:237:in `checkout_new_connection’

    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:191:in `block (2 levels) in chec
    kout’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:187:in `loop’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:187:in `block in checkout’
    C:/Ruby/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:186:in `checkout’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:94:in `connection’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_pool.rb:317:in `retrieve_connection’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_specification.rb:97:in `retrieve_connect
    ion’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/railties/databases.rake:92:in `rescue in create_database’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/railties/databases.rake:39:in `create_database’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/railties/databases.rake:35:in `block (2 levels) in ‘
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:636:in `call’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:636:in `block in execute’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:631:in `each’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:631:in `execute’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:597:in `block in invoke_with_call_chain’
    C:/Ruby/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:583:in `invoke’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2029:in `block (2 levels) in top_level’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2029:in `each’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2029:in `block in top_level’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2001:in `block in run’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:1998:in `run’
    C:/Ruby/lib/ruby/gems/1.9.1/gems/rake-0.8.7/bin/rake:31:in `’
    C:/Ruby/bin/rake:19:in `load’
    C:/Ruby/bin/rake:19:in `’
    Couldn’t create database for {“adapter”=>”postgresql”, “encoding”=>”unicode”, “database”=>”Test_development”, “pool”=>5, “username”=>”Test”, “password
    “=>nil}

    • netpie Says:

      @sudhir: please add a password of your db-user to database.yml. make sure you actually assign the same as your database role has. Can you follow me?

    • baash05 Says:

      username: postgres
      password: password

      When I installed postgreSQL, it asked for a password.. Being ever inventive, I typed “password”.
      In my YML file, I put this in.. and all was gold.

  9. bwvoss Says:

    @sudhir — try restarting your pgAdminIII, and making sure that you have your host and port specified in your database.yml file.

    @phy/netpie — which windows OS are you using? Netpie said that pg 0.10.1 gives no problems, but I was in the same state as you and also solved it with the 0.9.0 gem.

    Netpie is using windows 7, and I am using Vista — could be an OS version difference?

  10. bwvoss Says:

    @sudhir — I encountered the same problem.

    from: http://oldwiki.rubyonrails.org/rails/pages/PostgreSQL

    This is due to the permissions in pg_hba.conf being too restrictive by default. To allow rails to connect, simply change the bottom of pg_hba.conf to look like this.

    # TYPE DATABASE USER CIDR-ADDRESS METHOD

    # “local” is for Unix domain socket connections only
    local all all trust
    # IPv4 local connections:
    host all all 127.0.0.1/32 trust
    # IPv6 local connections:
    host all all ::1/128 trust

    And that cleared the problem for me.

    • netpie Says:

      @bwvoss: Surely, when you open all doors for everyone, Rails will also get through 😉
      Basically, it’s all about having a database role with the permissions to create db’s. The method for him in the pg_hba.conf would be md5, since he’ll be authenticating with a password. For creating such a user, enter:
      createuser -P
      and answer the following questions. Enter this user name and password in the database.yml and you definitely should be ready to go after restarting the DB service. You could also add this user to pg_hba.conf to restrict your permissions a bit. Hope this helps!

  11. kittr Says:

    great. Its working for me! Thx for this post! I had really problems to run Rails 3 and PostgreSQL on Windows 7(64bit).

  12. How to Install Ruby DevKit on Windows - Moeghan dot NET Berbagi Says:

    […] [NEW] Run the following DevKit ruby command. See “Setting up Rails 3 with PostgreSQL on Windows” By […]

  13. codinatrix Says:

    You are my hero.

  14. Loki Says:

    tks very much!

  15. Setting Windows PATH for Postgres tools Says:

    […] mostly followed these sources here and here. I installed a 32 bit version of 9.1 because some sources suggested issues with 64 […]

  16. http://google.com Says:

    This particular posting, “Setting up Rails 3 with PostgreSQL on Windows netpie” was in fact perfect.
    I’m creating out a replicate to show my personal friends.
    Many thanks,Sondra

  17. Setting Windows PATH for Postgres tools | BlogoSfera Says:

    […] mostly followed these sources here and here. I installed a 32 bit version of 9.1 because some sources suggested issues with 64 […]

  18. simonne Says:

    simonne

    Setting up Rails 3 with PostgreSQL on Windows | netpie

Leave a reply to netpie Cancel reply