使用ActiveRecord的时候CREATEDB不被识别为一个命令 [英] createdb not recognized as a command when using ActiveRecord

查看:142
本文介绍了使用ActiveRecord的时候CREATEDB不被识别为一个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手,Ruby和Web开发。我使用Windows 7 64位用Ruby 2.0和我有PostgreSQL的9.4安装。

我试图使用ActiveRecord来建立一个数据库。我检查了我的PostgreSQL服务器正在运行,并且我也捆绑安装,以确保我有所有需要的宝石。但是,当我尝试做终端命令捆绑EXEC耙创建:DB它告诉我,'CREATEDB不被识别为一个内部或外部命令,可操作的程序或批处理文件。我也没有与--trace的命令,但它并没有在这个问题上是什么提供更多有用的输出。终端只是说明这一点:

  C:\用户\ MH \桌面\ ActiveRecord的模板&G​​T;捆绑EXEC耙分贝:创建
创建ActiveRecord的模板开发和测试数据库,如果它们不存在...
CREATEDB不被识别为一个内部或外部命令,可操作的程序或批处理文件。

C:\用户\ MH \桌面\ ActiveRecord的模板&G​​T;捆绑EXEC耙分贝:创建--trace
**调用数据库:创建(FIRST_TIME)
**执行DB:创建
CREATEDB不被识别为一个内部或外部命令,可操作的程序或批处理文件。
 

有关此问题的最接近我已经找到位于此链接: HTTP: //bobbyong.com/blog/installing-postgresql-on-windoes/ 。我没有为链接描述调整的PostgreSQL的路径,但我仍然得到同样的CREATEDB问题。我也卸载/重新安装PostgreSQL的。我可以看到在PostgreSQL的目录和CREATEDB作品作为一个命令CREATEDB文件时,我使用的psql所以我不知道这个问题是ActiveRecord的到底是什么。

这是什么东西在我的Gemfile:

 来源https://rubygems.org

创业板的ActiveRecord
创业板PG

创业板RSpec的
创业板骗子

创业板耙
 

这是什么东西我Rake文件里:

 要求耙
    要求RSpec的/核心/ rake_task
    需要'active_support
    要求active_support / core_ext

    require_relative'配置'

    命名空间:DB做
      DESC删除,创建和迁移数据库
      任务:复位=> [:降:创建:迁移]

      说明创建#{APP_NAME}数据库
      任务创造吗
        却将创建#{APP_NAME}开发和测试数据库,如果它们不存在......
        系统(CREATEDB#{DB_NAME} --username#{DB_USERNAME} -w --no密码和放大器;&安培; CREATEDB#{TEST_DB_N​​AME} --username#{DB_USERNAME} -w --no密码)
      结束

      降序落#{APP_NAME}数据库
      任务滴做
        却将删除#{APP_NAME}开发和测试数据库...
        系统(DROPDB#{DB_NAME}&功放;&安培; DROPDB#{TEST_DB_N​​AME} _test)
      结束

      DESC迁移数据库
      任务迁移吗
        ActiveRecord的:: Migrator.migrations_paths<< File.dirname(__ FILE__)+'DB /迁移
        ActiveRecord的:: Migration.verbose =真
        的ActiveRecord :: Migrator.migrate(ActiveRecord的:: Migrator.migrations_paths,无)
      结束

      DESC填充样本数据的数据库
      任务种子吗
        需要APP_ROOT.join('分贝','seeds.rb')
      结束
    结束

    命名空间:生成做
      说明创建一个数据库迁移\ñ耙产生:迁移NAME = create_people
      任务:迁移做
        除非ENV.has_key?(NAME)
          提高必须指定名称的迁移,如耙产生:迁移NAME = create_people
        结束

        migration_name = ENV ['名称']
        =将class_name migration_name.camelize
        时间戳= Time.now.strftime(%Y%M%D%H%M%S')
        文件名=#{时间戳} _#{migration_name} .RB
        PATH = APP_ROOT.join(DB,迁移,文件名)

        如果File.exist?(路径)
          提高错误!文件#{PATH}已存在
        结束

        使创建在#{}路径迁移
        File.open方法(路径,'W +')也| F |
          f.write(级#{}将class_name<的ActiveRecord ::迁移\ñ\ TDEF变化\ñ\ñ\倾向于\ NEND)
        结束
      结束
    结束

    说明开始IRB与应用程序环境中加载
    任务控制台吗
      EXECIRB -r。/配置
    结束

    DESC运行规范
    RSpec的::核心:: RakeTask.new(:SPEC)
    任务:默认=> :规格

    #请问这个不工作?
    #desc运行规范
    #task'规格'做
    #EXECRSpec的规格
    #结束
 

这是什么东西在我的config.rb文件:

 要求'路径'
要求PG
需要'active_record
需要'记录'

##加载的所有文件和配置数据库

APP_ROOT = Pathname.new(File.expand_path(File.dirname(__ FILE__)))

APP_NAME = APP_ROOT.basename.to_s

DB_PATH = APP_ROOT.join('分贝',APP_NAME +_development.db)。to_s

DB_NAME = APP_NAME +_development.db

TEST_DB_N​​AME = APP_NAME +_test.db

DB_USERNAME ='Postgres的

DB_PASSWORD =

如果ENV ['DEBUG']
  ActiveRecord的:: Base.logger = Logger.new(标准输出)
结束


风向[APP_ROOT.join(模型,* .RB')每做|。model_file |
  文件名= File.basename(model_file).gsub('RB','')
  自动加载的ActiveSupport :: Inflector.camelize(文件名),model_file
结束

ActiveRecord的:: Base.establish_connection:适配器=> PostgreSQL的,
                                        :数据库=> DB_NAME,
                                        :主机=> 'localhost'的,
                                        :用户名=> DB_USERNAME,
                                        :密码=> DB_PASSWORD
 

解决方案

如果终端命令捆绑EXEC耙创建:DB产生不被识别为一个内部或外部命令,可操作的程序或批处理错误'CREATEDB文件,这意味着,ActiveRecord的是不是能够找到与CreateDB.exe PostgreSQL的。

您必须追加的PostgreSQL bin和lib文件夹到PATH环境变量如下所述:的 http://bobbyong.com/blog/installing-postgresql-on-windoes/

请注意把bin路径的lib路径之前是很重要的还是ActiveRecord的仍然无法找到与CreateDB.exe在bin路径。此外,一定要重新启动命令的终端,使任何更改环境变量生效。

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

解决方案

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.

这篇关于使用ActiveRecord的时候CREATEDB不被识别为一个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆