0

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.

7
  • I don't understand the question
    – Ese10
    Commented Jun 29 at 2:18
  • 1
    In which rails environment- development, test or production are you having the problem?
    – dbugger
    Commented Jun 29 at 13:13
  • Protip - use fetch instead of square brackets. For example Rails.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 environment rails -e production ....
    – max
    Commented Jun 29 at 13:27
  • @dbugger I am running this in production. @max Thanks, I will use the fetch from now on but in this use case there are no new error messages and I am still getting the same error when running rails db:setup
    – Ese10
    Commented Jun 29 at 14:09
  • If rails -e production db:setup results in We 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.
    – dbugger
    Commented Jun 29 at 14:41

1 Answer 1

0

The problem was that the postgresql user that was created did not have the permission to create a database so db:setup would not work so it would fallback to connecting to the default database, postgres. The user could only manipulate the tables in the database.

Not the answer you're looking for? Browse other questions tagged or ask your own question.