1

I'm a newbie to Ruby and web development. I'm using Windows 7 64-bit with Ruby 2.0 and I have PostgreSQL 9.4 installed.

I'm trying to use ActiveRecord to create a database. I checked that my postgresql server is running and I did bundle install to make sure I had all the required gems. However, when I try to do the terminal command "bundle exec rake create:db" it tells me that "'createdb' is not recognized as an internal or external command, operable program or batch file." I also did the command with --trace but it did not provide more helpful output on what the issue is. The terminal just shows this:

C:\Users\MH\Desktop\activerecord-template> bundle exec rake db:create
Creating activerecord-template development and test databases if they don't exist...
'createdb' is not recognized as an internal or external command, operable program or batch file.

C:\Users\MH\Desktop\activerecord-template> bundle exec rake db:create --trace
**Invoke db:create (first_time)
**Execute db:create
'createdb' is not recognized as an internal or external command, operable program or batch file.

The closest thing I have found regarding this issue is located at this link: http://bobbyong.com/blog/installing-postgresql-on-windoes/. I did adjust the path of PostGreSQL as described in the link, but I still get the same createdb issue. I also uninstalled/reinstalled PostGreSQL. I can see a createdb file in the PostGreSQL directory and createdb works as a command when I use psql so I'm not sure exactly what the issue is with ActiveRecord.

This is what is in my Gemfile:

source 'https://rubygems.org'

gem 'activerecord'
gem 'pg'

gem 'rspec'
gem 'faker'

gem 'rake'

This is what is inside my Rakefile:

    require 'rake'
    require 'rspec/core/rake_task'
    require 'active_support'
    require 'active_support/core_ext'

    require_relative 'config'

    namespace :db do
      desc "Drop, create, and migrate the database"
      task :reset => [:drop, :create, :migrate]

      desc "Create #{APP_NAME} databases"
      task "create" do
        puts "Creating #{APP_NAME} development and test databases if they don't exist..."
        system("createdb #{DB_NAME} --username #{DB_USERNAME} -w --no-password && createdb #{TEST_DB_NAME} --username #{DB_USERNAME} -w --no-password")
      end

      desc "Drop #{APP_NAME} databases"
      task "drop" do
        puts "Dropping #{APP_NAME} development and test databases..."
        system("dropdb #{DB_NAME} && dropdb #{TEST_DB_NAME}_test")
      end

      desc "Migrate the database"
      task "migrate" do
        ActiveRecord::Migrator.migrations_paths << File.dirname(__FILE__) + 'db/migrate'
        ActiveRecord::Migration.verbose = true
        ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, nil)
      end

      desc "Populate the database with sample data"
      task "seed" do
        require APP_ROOT.join('db', 'seeds.rb')
      end
    end

    namespace :generate do
      desc "Create a database migration\n rake generate:migration NAME=create_people"
      task :migration do
        unless ENV.has_key?('NAME')
          raise "Must specify NAME for migration, e.g. rake generate:migration NAME=create_people"
        end

        migration_name = ENV['NAME']
        class_name = migration_name.camelize
        timestamp = Time.now.strftime('%Y%m%d%H%M%S')
        filename = "#{timestamp}_#{migration_name}.rb"
        path = APP_ROOT.join('db', 'migrate', filename)

        if File.exist?(path)
          raise "ERROR! File '#{path}' already exists"
        end

        puts "Creating migration at #{path}"
        File.open(path, 'w+') do |f|
          f.write("class #{class_name} < ActiveRecord::Migration\n\tdef change\n\n\tend\nend")
        end
      end
    end

    desc 'Start IRB with application environment loaded'
    task "console" do
      exec "irb -r./config"
    end

    desc "Run the specs"
    RSpec::Core::RakeTask.new(:spec)
    task :default  => :specs

    # Will this not work?
    #desc "Run the specs"
    #task 'specs' do
    #  exec "rspec spec"
    #end

This is what is inside my config.rb file:

require 'pathname'
require 'pg'
require 'active_record'
require 'logger'

## Load all files and configure the db

APP_ROOT = Pathname.new(File.expand_path(File.dirname(__FILE__)))

APP_NAME = APP_ROOT.basename.to_s

DB_PATH  = APP_ROOT.join('db', APP_NAME + "_development.db").to_s

DB_NAME = APP_NAME + "_development.db"

TEST_DB_NAME = APP_NAME + "_test.db"

DB_USERNAME = 'postgres'

DB_PASSWORD = 

if ENV['DEBUG']
  ActiveRecord::Base.logger = Logger.new(STDOUT)
end


Dir[APP_ROOT.join('models', '*.rb')].each do |model_file|
  filename = File.basename(model_file).gsub('.rb', '')
  autoload ActiveSupport::Inflector.camelize(filename), model_file
end

ActiveRecord::Base.establish_connection :adapter  => 'postgresql',
                                        :database => DB_NAME,
                                        :host => 'localhost',
                                        :username => DB_USERNAME,
                                        :password => DB_PASSWORD
mu is too short
  • 426,620
  • 70
  • 833
  • 800
user4992261
  • 31
  • 1
  • 9
  • I think I do. I can navigate into my PostgreSQL directory and see createdb.exe there. I'm not sure why ActiveRecord can't access it though. – user4992261 Jun 09 '15 at 22:36
  • Huh...your comment about "createdb.exe" being in the path got me thinking about my ActiveRecord directory. It must be a related issue with the path as described in the link I gave. I copy and pasted "createdb.exe" into the same ActiveRecord directory and now "bundle exec rake db:create" works. For some reason ActiveRecord can't find the files in the PostgreSQL folder. I have to think more about how to link the PostgreSQL and ActiveRecord directories up. – user4992261 Jun 09 '15 at 22:42
  • Windows has the PATH environment variable too but maybe I'm editing it incorrectly. I do have C:\Users\MH\PostgreSQL\bin; C:\Users\MH\PostgreSQL\lib in the Path variable but ActiveRecord still isn't running createdb.exe. – user4992261 Jun 09 '15 at 23:14
  • Interesting...above I have it typed with the bin path first and then the lib path but I didn't realize I wrote lib and then bin when I set the path variable in my environment variables. For some reason if you put the lib path before the bin path, the terminal will keep giving you the 'createdb' error message but if you put it with the bin path before the lib path, then it can find the createdb.exe file. Not sure why it has to be in ABC order like that...Also be sure to restart your terminal every time you change your environment variables so the changes take effect. – user4992261 Jun 10 '15 at 00:50
  • It's way easier to just use the SQL level command equivalents like `CREATE DATABASE` from `psql`. – Craig Ringer Jun 10 '15 at 01:22
  • @CraigRinger: Try telling Rails that :P But to be fair, Rails wants to connect to the database as configured for the application so connecting to some other database as some other user is probably more complexity than they want to deal with. – mu is too short Jun 10 '15 at 04:00
  • @muistooshort Well, that's all `createdb` does, it's a simple wrapper for `CREATE DATABASE`. If you don't have credentials to connect as a user with `CREATEDB` rights you can't run `createdb` or `CREATE DATABASE`. – Craig Ringer Jun 10 '15 at 04:03
  • @CraigRinger: This is actually for a Ruby on Rails class and the next assignments all use ActiveRecord so I really wanted to get it working in the way the class assignments wanted it to work. I did see that createdb did actually work with psql but that ActiveRecord just couldn't find the createdb.exe file. – user4992261 Jun 10 '15 at 04:13
  • @CraigRinger But there's no middle layer with AR, AFAIK the database-specific part of the ORM is mashed together with the database adapter so connecting to PostgreSQL means "connecting to PostgreSQL using the application's database and authentication". The lack of a separate and distinct DBI is a problem. – mu is too short Jun 10 '15 at 04:34

1 Answers1

0

If the terminal command "bundle exec rake create:db" produces the error "'createdb' is not recognized as an internal or external command, operable program or batch file.", this means that ActiveRecord is not able to find createdb.exe in the PostgreSQL directory.

You must append the PostgreSQL bin and lib folders to your path environment variable as described here: http://bobbyong.com/blog/installing-postgresql-on-windoes/

Do note it is important to put the bin path before the lib path or ActiveRecord will still not be able to find createdb.exe in the bin path. Also be sure to restart your command terminal so that any changes to the environment variables can take effect.

user4992261
  • 31
  • 1
  • 9