【问题标题】:PostgreSQL "Database does not exist" but it doesPostgreSQL“数据库不存在”但确实存在
【发布时间】:2018-11-08 23:10:44
【问题描述】:

我几天来一直在尝试在 PostgreSQL 中创建数据库,但遇到了几个问题,但似乎卡住了。

我在 PostgreSQL 中手动创建了一个名为 postgres_development 的数据库,因为 bundle exec rake db:create 不起作用。

现在,我正在尝试运行 bundle exec rake db:migrate,但它没有识别出我有一个名为 postgres_development 的数据库。

这是我的 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("@SET PGPASSWORD=#{DB_PASSWORD}; createdb --username=#{DB_USERNAME} --password=#{DB_PASSWORD} #{DB_NAME} && @SET PGPASSWORD=#{DB_PASSWORD}; createdb --username=#{DB_USERNAME} --password=#{DB_PASSWORD} #{TEST_DB_NAME}")
  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::MigrationContext.new("/db/migrate/").migrate
  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,postgres,我从 activerecord-template 重命名了它,但尝试让它连接到我的数据库失败。

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

对这里发生的事情的任何想法将不胜感激!

【问题讨论】:

    标签: database postgresql rake


    【解决方案1】:

    这似乎很明显:您创建了一个数据库postgres_development,然后您尝试连接到另一个名称的数据库,即postgres_development.db。

    这应该如何工作?

    【讨论】:

    • 好吧,db:create 应该创建正确的数据库,但这并没有发生,所以我必须自己创建一个,但是我通过 psql 创建的那个不包括“ .db”文件扩展名。主要问题是 rake db:create 没有创建数据库。感谢您指出为什么我的手动尝试失败了。
    【解决方案2】:
    1. 在您的Rakefile 第28 行中,在/ 前面添加.,即将其更改为ActiveRecord::MigrationContext.new("./db/migrate/").migrate

    2. db/migrate/20181108113458_create_people.rb 在第一行的末尾添加一个[4.2] 即。将其更改为class CreatePeople &lt; ActiveRecord::Migration[4.2]

    【讨论】:

      猜你喜欢
      • 2015-11-24
      • 2017-05-26
      • 2017-07-02
      • 2018-12-05
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      相关资源
      最近更新 更多