In production mode, I am testing connecting a rails 7 project to a remote server for the postgresql database. However when I try to connect to it, it starts looking to connect to the postgres
database which is not the correct database, then it tries to create correct_db which is the right db. Since it is looking to connect to the wrong database I get this error:
>> rails db:setup
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 'correct_db' 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. (ActiveRecord::NoDatabaseError)
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: FATAL: no pg_hba.conf entry for host "##########", user "new_db", database "postgres", SSL on (PG::ConnectionBad)
FATAL: no pg_hba.conf entry for host "########", user "new_db", database "postgres", SSL off
Tasks: TOP => db:setup => db:create
This is what the database.yml
file looks like:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: new_db_development
test:
<<: *default
database: new_db_test
production:
<<: *default
host: <%= Rails.application.credentials[:psql][:host] %>
# port: <%= Rails.application.credentials[:psql][:port] %>
database: <%= Rails.application.credentials[:psql][:database].tap { |v| Rails.logger.info "Database: #{v}" } %>
username: <%= Rails.application.credentials[:psql][:username] %>
password: <%= Rails.application.credentials[:psql][:password] %>
timeout: 5000
This is the credentials file:
psql:
host: ##############
database: correct_db
username: new_db
password: ************
I have checked that the environment variables that can override the database.yml
file are empty using: echo $DATABASE
and echo $DATABASE_URL
and I can manually, remote connect to the database using psql -h ########## -U new_db -d correct_db
. So I am assuming that the setup on the remote postgres server is correct and the issue has something to do with rails 7.
fetch
instead of square brackets. For exampleRails.application.credentials[:psql].fetch(:database)
. This will raise a KeyError if the configuration setting is missing which can save you huge amounts of debugging time. Also use the-e
option when running rails to specific the environmentrails -e production ...
.fetch
from now on but in this use case there are no new error messages and I am still getting the same error when runningrails db:setup
rails -e production db:setup
results inWe could not find your database: postgres
, there is something else in your app configuration that is causing the issue or database.yml you have posted is not the one under the config folder.