sách gpt4 ai đã đi

database - PostgreSQL "Database does not exist"但确实如此

In lại 作者:行者123 更新时间:2023-11-29 13:42:30 30 4
mua khóa gpt4 Nike

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

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

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

Error Message

这是我的 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}")
kết thúc

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")
kết thúc

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
kết thúc

desc "Populate the database with sample data"
task "seed" do
require APP_ROOT.join('db', 'seeds.rb')
kết thúc
kết thúc

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"
kết thúc

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"
kết thúc

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")
kết thúc
kết thúc
kết thúc

desc 'Start IRB with application environment loaded'
task "console" do
exec "irb -r./config"
kết thúc

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

Đây là của tôi config.rb 在同一个文件夹中,postgres Tôi đến từ 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)
kết thúc


Dir[APP_ROOT.join('models', '*.rb')].each do |model_file|
filename = File.basename(model_file).gsub('.rb', '')
autoload ActiveSupport::Inflector.camelize(filename), model_file
kết thúc

ActiveRecord::Base.establish_connection :adapter => 'postgresql',
:database => DB_NAME,
:host => 'localhost',
:username => DB_USERNAME,
:password => DB_PASSWORD

如果您对这里发生的事情有任何想法,我们将不胜感激!

1 Câu trả lời

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

这应该如何运作?

关于database - PostgreSQL "Database does not exist"但确实如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53217549/

30 4 0
Bài viết được đề xuất: java - 持久化实体时出错 : subquery in FROM must have an alias
Bài viết được đề xuất: php - 设置每节动态数据集的 mysql 查询
Bài viết được đề xuất: php - 如何调用这个php函数
Bài viết được đề xuất: python - 在 sqlalchemy 中提供模型类级别的验证
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com