Rake test:prepare fails in Rails 4.0.8

I was testing my app (with rspec) when I noticed that rake
test:prepare
throws
an odd *FATAL: database “postgres” does not exist *error. Stranger
still,
my tests run just fine regardless.

However, whenever I use rake *test:prepare --trace *(after using rake
db:migrate
) I get this error and receive a similar error when
attempting rake
db:create RAILS_ENV=test
and also *rake db:drop RAILS_ENV=test *as
well. Any
idea what I may be overlooking?

Here is the error in question:

** Invoke test:prepare (first_time)
** Invoke db:test:prepare (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:test:purge
rake aborted!

PG::Error: FATAL: database “postgres” does not exist

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql_adapter.rb:855:in
`initialize’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql_adapter.rb:855:in
`new’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql_adapter.rb:855:in
`connect’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql_adapter.rb:556:in
`initialize’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in
`new’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in
`postgresql_connection’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in
`new_connection’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in
`checkout_new_connection’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in
`acquire_connection’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in
`block in checkout’

/Users/dalecooper/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in
`mon_synchronize’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in
`checkout’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in
`block in connection’

/Users/dalecooper/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in
`mon_synchronize’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in
`connection’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in
`retrieve_connection’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_handling.rb:79:in
`retrieve_connection’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_handling.rb:53:in
`connection’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/tasks/postgresql_database_tasks.rb:8:in
`connection’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/tasks/postgresql_database_tasks.rb:30:in
`drop’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/tasks/postgresql_database_tasks.rb:43:in
`purge’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/tasks/database_tasks.rb:137:in
`purge’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/railties/databases.rake:365:in
`block (3 levels) in <top (required)>’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:240:in
`call’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:240:in
`block in execute’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:235:in
`each’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:235:in
`execute’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:179:in
`block in invoke_with_call_chain’

/Users/dalecooper/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in
`mon_synchronize’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:172:in
`invoke_with_call_chain’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:201:in
`block in invoke_prerequisites’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:199:in
`each’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:199:in
`invoke_prerequisites’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:178:in
`block in invoke_with_call_chain’

/Users/dalecooper/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in
`mon_synchronize’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:172:in
`invoke_with_call_chain’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:165:in
`invoke’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/railties/databases.rake:371:in
`block (3 levels) in <top (required)>’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:240:in
`call’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:240:in
`block in execute’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:235:in
`each’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:235:in
`execute’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:179:in
`block in invoke_with_call_chain’

/Users/dalecooper/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in
`mon_synchronize’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:172:in
`invoke_with_call_chain’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:201:in
`block in invoke_prerequisites’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:199:in
`each’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:199:in
`invoke_prerequisites’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:178:in
`block in invoke_with_call_chain’

/Users/dalecooper/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in
`mon_synchronize’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:172:in
`invoke_with_call_chain’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:165:in
`invoke’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/application.rb:150:in
`invoke_task’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/application.rb:106:in
`block (2 levels) in top_level’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/application.rb:106:in
`each’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/application.rb:106:in
`block in top_level’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/application.rb:115:in
`run_with_threads’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/application.rb:100:in
`top_level’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/application.rb:78:in
`block in run’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/application.rb:176:in
`standard_exception_handling’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/application.rb:75:in
`run’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/bin/rake:33:in
<top (required)>' /Users/dalecooper/.rvm/gems/[email protected]_4_0/bin/rake:23:inload’
/Users/dalecooper/.rvm/gems/[email protected]_4_0/bin/rake:23:in
`’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/bin/ruby_executable_hooks:15:in
`eval’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/bin/ruby_executable_hooks:15:in
`’

Tasks: TOP => db:test:load => db:test:purge

Here is what my test environment looks like (according to my gemfile):

gem 'rails', '4.0.8'
gem 'pg', '0.15.1'

group :development, :test do
    gem 'rspec-rails', '2.13.1'
end


group :test do
    gem 'selenium-webdriver', '2.35.1'
    gem 'capybara', '2.1.0'
end

and here is what my *database.yml *file looks like :

development:
  adapter: postgresql
  endoding: unicode
  database: myapp_development
  pool: 5
  timeout: 5000
  hostname: localhost


test:
  adapter: postgresql
  endoding: unicode
  database: myapp_test
  pool: 5
  timeout: 5000
  hostname: localhost


production:
  adapter: postgresql
  database: myapp_production
  pool: 5
  timeout: 5000

On Sunday, July 27, 2014 8:12:14 PM UTC+2, Special Agent Dale Cooper
wrote:

Here is the error in question:
** Invoke environment

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql_adapter.rb:855:in

`acquire_connection’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in

`drop’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:240:in

`block in invoke_prerequisites’
/Users/dalecooper/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in

`execute’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/task.rb:201:in

`invoke_task’

/Users/dalecooper/.rvm/gems/[email protected]_4_0/gems/rake-10.3.2/lib/rake/application.rb:115:in

/Users/dalecooper/.rvm/gems/[email protected]_4_0/bin/ruby_executable_hooks:15:in

    gem 'capybara', '2.1.0'
  timeout: 5000


production:
  adapter: postgresql
  database: myapp_production
  pool: 5
  timeout: 5000

First, the correct syntax to run the above command is:

bin/rake db:test:prepare (depending on your installation, you
should run it as bundle exec rake db:test:prepare)

and r*ake test:prepare *as you stated

You can also use:

rake db:setup

It will create the database, load the schema and initialize it with the
seed.