-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is rake db:create
accidentally treating the default
config as an environment on par with development
/test
/production
?
#46577
Comments
default
config being picked as an environment by rake db:create?default
config being picked as an environment by rake db:create
?
default
config being picked as an environment by rake db:create
?rake db:create
accidentally treating the default
config as an environment on par with development
/test
/production
?
Cannot be reproduced. You're most likely mistakenly attributing an error with connecting your Postgres server to Rails - say for example that the server isn't actually running or can't be connected to with the default connection settings.
|
Thanks for checking & going through this with me. This can probably be closed. |
I wanted to offer that this could be possible due to a misconfiguration and/or state leaking in from the environment. It's unlikely, but possible! If you open up [5] pry(main)> ActiveRecord::Base.configurations.configs_for
=> [#<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000000147b99298
@configuration_hash=
{:adapter=>"postgresql", :encoding=>"unicode", :host=>"localhost", :pool=>10},
@env_name="default",
@name="primary">,
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000000147b98f78
@configuration_hash=
{:adapter=>"postgresql",
:encoding=>"unicode",
:host=>"localhost",
:pool=>10,
:database=>"good_job_development"},
@env_name="development",
@name="primary">,
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000000147b98d70
@configuration_hash=
{:adapter=>"postgresql",
:encoding=>"unicode",
:host=>"localhost",
:pool=>10,
:database=>"good_job_test",
:reaping_frequency=>0},
@env_name="test",
@name="primary">,
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000000147b98b18
@configuration_hash=
{:adapter=>"postgresql",
:encoding=>"unicode",
:host=>"localhost",
:pool=>10,
:database=>"good_job_development"},
@env_name="production",
@name="primary">,
#<ActiveRecord::DatabaseConfigurations::UrlConfig:0x0000000147b988c0
@configuration_hash=
{:adapter=>"postgresql",
:encoding=>"unicode",
:host=>"localhost",
:pool=>10,
:database=>"good_job_demo",
:url=>nil},
@env_name="demo",
@name="primary",
@url=nil>] You'll notice that the rails/activerecord/lib/active_record/tasks/database_tasks.rb Lines 574 to 576 in 19b08e2
While my bin/rails runner "pp ActiveRecord::Base.configurations.configs_for" ...if you don't see a |
Thanks @bensheldon , I just followed my repro steps from the first post and created a new Rails app using the commands... rails new testapp --api --database=postgresql
cd testapp
rake db:create Which resulted in the same issue: # after rake db:create
We could not find your database: postgres. Which can be found in the database configuration file located at config/database.yml. My output for # output for bin/rails runner "pp ActiveRecord::Base.configurations.configs_for"
[#<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007efe5e11cd68
@configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5},
@env_name="default",
@name="primary">,
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007efe5e11cc50
@configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"testapp_development"},
@env_name="development",
@name="primary">,
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007efe5e11cb88
@configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"testapp_test"},
@env_name="test",
@name="primary">,
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007efe5e11cac0
@configuration_hash=
{:adapter=>"postgresql",
:encoding=>"unicode",
:pool=>5,
:database=>"testapp_production",
:username=>"testapp",
:password=>nil},
@env_name="production",
@name="primary">] |
I can reproduce this issue locally if I drop the default Rails hardcodes/expects the presence of the rails/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb Lines 102 to 104 in a289f4c
Background: In order to create a database on Postgres, the connection must already be connected to an existing database. Here's a good explanation: https://dba.stackexchange.com/a/144289 If you're using the Docker image, it's possible that the I ultimately think the problem is on the application for misconfiguring Postgres and/or Rail's database configuration (all of my Rails-on-Docker experience is to have the Postgres docker image create the database rather than And I think the error message from Rails could be more accurate/helpful because the database is not defined in |
I'm also having this issue with Rails 7 on Ubuntu 22.04 with WSL2. Spent a lot of time to debug with no luck. The postgres database is in place by default. I was assuming that WSL connect to postgresql on Windows via Unix socket. However, I don't have any postgres installed on the Windows. I have more environments with same or similar setup but this is only magicly reproducible with Rails 7, nothing helps |
Here is my complete error output. Do you see the part at the very bottom that says "Caused by:" where it outputs the initial exception ( ❯ bin/rails db:create
We could not find your database: postgres. Which can be found in the database configuration file located at config/database.yml.
To resolve this issue:
- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.
To create your database, run:
bin/rails db:create
Couldn't create 'good_job_development' database. Please check your configuration.
rails aborted!
ActiveRecord::NoDatabaseError: We could not find your database: postgres. Which can be found in the database configuration file located at config/database.yml.
To resolve this issue:
- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.
To create your database, run:
bin/rails db:create
Caused by:
PG::ConnectionBad: connection to server at "::1", port 5432 failed: FATAL: database "postgres" does not exist
Tasks: TOP => db:create
(See full trace by running task with --trace) |
|
@Shedrack-Sunday Thank you! That's really helpful, and also a different error than we identified earlier. Just to summarize, I think there is a Rails bug here, which is specifically that Rails is providing an inaccurate error message and masking (or distracting from) the underlying error message. ...and then there are the two (so far!) system configuration problems, that are outside the scope of the Rails issue and the workaround is to ignore everything above the "Caused By" part of the output and instead try to address:
Just to focus on the inaccurate error message, I think this is because the logic is keying off of the presence of the rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb Lines 68 to 78 in 03a1da9
|
…stgres` database
Steps to reproduce
Not 100% sure about this, and I apologize if this turns out to be a red herring or non-reproducible; but at least one other person seems to be having the same issue as I am, so I'm creating this ticket.
Issue seems to be happening specifically in apps created with the
--api --database=postgresql
flags.Previous discussion in this stackoverflow is related.
Test case
rails new testapp --api --database=postgresql cd testapp rake db:create
This comes back with
Expected behavior
It should not try to connect to
postgres
database.Actual behavior
It tries to connect to
postgres
database. It seems to be picking up thedefault
config as an actual environment... thepg
gem's default database name ispostgres
... put 2 + 2 together and you might have yourself a bug.System configuration
Rails version: 7.0.4
Ruby version: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
The text was updated successfully, but these errors were encountered: