【发布时间】:2015-06-09 21:04:45
【问题描述】:
我是 Ruby 和 Web 开发的新手。我正在使用带有 Ruby 2.0 的 Windows 7 64 位,并且安装了 PostgreSQL 9.4。
我正在尝试使用 ActiveRecord 创建数据库。我检查了我的 postgresql 服务器是否正在运行,并且我进行了 bundle install 以确保我拥有所有必需的 gem。但是,当我尝试执行终端命令“bundle exec rake create:db”时,它告诉我“'createdb' 未被识别为内部或外部命令、可运行程序或批处理文件”。我还使用 --trace 执行了该命令,但它没有提供有关问题所在的更多有用输出。终端只是显示这个:
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.
我发现的与此问题最接近的信息位于此链接:http://bobbyong.com/blog/installing-postgresql-on-windoes/。我确实按照链接中的描述调整了 PostGreSQL 的路径,但我仍然遇到相同的 createdb 问题。我还卸载/重新安装了 PostGreSQL。我可以在 PostGreSQL 目录中看到 createdb 文件,并且 createdb 在我使用 psql 时作为命令工作,所以我不确定 ActiveRecord 到底是什么问题。
这是我的 Gemfile 中的内容:
source 'https://rubygems.org'
gem 'activerecord'
gem 'pg'
gem 'rspec'
gem 'faker'
gem 'rake'
这是我的 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
这是我的 config.rb 文件中的内容:
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
【问题讨论】:
-
我想是的。我可以导航到我的 PostgreSQL 目录并在那里查看 createdb.exe。我不确定为什么 ActiveRecord 无法访问它。
-
嗯...您对路径中的“createdb.exe”的评论让我想到了我的 ActiveRecord 目录。如我提供的链接中所述,它必须与路径相关。我将“createdb.exe”复制并粘贴到同一个 ActiveRecord 目录中,现在“bundle exec rake db:create”可以工作了。由于某种原因,ActiveRecord 在 PostgreSQL 文件夹中找不到文件。我必须更多地考虑如何将 PostgreSQL 和 ActiveRecord 目录链接起来。
-
Windows 也有 PATH 环境变量,但也许我编辑不正确。我确实有 C:\Users\MH\PostgreSQL\bin; C:\Users\MH\PostgreSQL\lib 在 Path 变量中,但 ActiveRecord 仍然没有运行 createdb.exe。
-
有趣...上面我先输入了 bin 路径,然后输入了 lib 路径,但是当我在环境变量中设置路径变量时,我没有意识到我先编写了 lib,然后再编写了 bin。出于某种原因,如果您将 lib 路径放在 bin 路径之前,终端将不断给您“createdb”错误消息,但如果您将其与 bin 路径放在 lib 路径之前,那么它可以找到 createdb.exe 文件。不知道为什么它必须以这样的 ABC 顺序排列...另外,每次更改环境变量时,请务必重新启动终端,以便更改生效。
-
只使用 SQL 级别的命令等价物(如来自
psql的CREATE DATABASE)要容易得多。
标签: ruby windows postgresql activerecord