0

I am fairly new to programming and I have a Ruby on Rails app deployed on Heroku. It may be worth noting that the app has a separate FE and BE, both deployed separately on Heroku. For "reasons", they both have databases. However, the FE only ever accesses the BE via API endpoints. I mention this because I wasn't sure if there was some weird issue with Heroku mixing up the db's.

This issue concerns the FE database only.

Issue:

When I try to run migrations and seed the FE database (using heroku run commands), I receive an error that states that a connection to the server cannot be established.

Ruby: 3.1.2
Rails: 7.0.4
Stack heroku-22

Main Error:

ActiveRecord::ConnectionNotEstablished: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
        Is the server running locally and accepting connections on that socket?

full error output further below

database.yml:

production:
  <<: *default
  database: db_name
  url: <%= ENV["DATABASE_URL"] %>

Terminal:

╰─ ps -ef | grep postgres                                                                                                                                                                       
  501  1306     1   0 12:50AM ??         1:36.87 /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres
  501  1382  1306   0 12:50AM ??         0:00.06 postgres: checkpointer   
  501  1383  1306   0 12:50AM ??         0:00.47 postgres: background writer   
  501  1384  1306   0 12:50AM ??         0:00.39 postgres: walwriter   
  501  1385  1306   0 12:50AM ??         0:12.22 postgres: autovacuum launcher   
  501  1386  1306   0 12:50AM ??         0:44.60 postgres: stats collector   
  501  1387  1306   0 12:50AM ??         0:00.09 postgres: logical replication launcher   
  501 37952 36446   0  9:22PM ttys005    0:00.00 grep --color=auto --exclude-dir=.bzr --exclud.....

In psql:

wetective::DATABASE-> \conninfo
You are connected to database “lettersandnumbers” as user “lettersandsuch” on host "ec2-00-000-00-00.compute-1.amazonaws.com" (address “00.000.00.00”) at port "5432".
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)

Might also be of note... I recently ran a brew upgrade which seemed to break a few things. Namely, every so often, I have to run bundle pristine pg inside of my rails app to recompile the postgres bindings against the new directory for libpq. I don't think that has anything to do with what's happening here, but figured I'd mention it.

Full error output:

heroku run rails db:{drop,create,migrate,seed} =>

rails aborted!
ActiveRecord::ConnectionNotEstablished: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
        Is the server running locally and accepting connections on that socket?
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/postgresql_adapter.rb:87:in `rescue in new_client'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/postgresql_adapter.rb:77:in `new_client'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/postgresql_adapter.rb:37:in `postgresql_connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:656:in `public_send'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:656:in `new_connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:700:in `checkout_new_connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:679:in `try_to_checkout_new_connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:640:in `acquire_connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:341:in `checkout'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:181:in `connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_handler.rb:211:in `retrieve_connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_handling.rb:313:in `retrieve_connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_handling.rb:280:in `connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/tasks/database_tasks.rb:65:in `check_protected_environments!'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/railties/databases.rake:18:in `block (2 levels) in <main>'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `execute'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:243:in `block in invoke_prerequisites'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `each'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `invoke_prerequisites'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:218:in `block in invoke_with_call_chain'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:188:in `invoke'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:160:in `invoke_task'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `each'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block in top_level'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:125:in `run_with_threads'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:110:in `top_level'
/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/commands/rake/rake_command.rb:24:in `block (2 levels) in perform'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/commands/rake/rake_command.rb:24:in `block in perform'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/rake_module.rb:59:in `with_application'
/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/command.rb:51:in `invoke'
/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/commands.rb:18:in `<main>'
/app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
/app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
/app/bin/rails:4:in `<main>'

Caused by:
PG::ConnectionBad: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
        Is the server running locally and accepting connections on that socket?
/app/vendor/bundle/ruby/3.1.0/gems/pg-1.4.3/lib/pg/connection.rb:756:in `connect_start'
/app/vendor/bundle/ruby/3.1.0/gems/pg-1.4.3/lib/pg/connection.rb:756:in `connect_internal'
/app/vendor/bundle/ruby/3.1.0/gems/pg-1.4.3/lib/pg/connection.rb:749:in `connect_to_hosts'
/app/vendor/bundle/ruby/3.1.0/gems/pg-1.4.3/lib/pg/connection.rb:672:in `new'
/app/vendor/bundle/ruby/3.1.0/gems/pg-1.4.3/lib/pg.rb:69:in `connect'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/postgresql_adapter.rb:78:in `new_client'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/postgresql_adapter.rb:37:in `postgresql_connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:656:in `public_send'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:656:in `new_connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:700:in `checkout_new_connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:679:in `try_to_checkout_new_connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:640:in `acquire_connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:341:in `checkout'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:181:in `connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_adapters/abstract/connection_handler.rb:211:in `retrieve_connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_handling.rb:313:in `retrieve_connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/connection_handling.rb:280:in `connection'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/tasks/database_tasks.rb:65:in `check_protected_environments!'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/railties/databases.rake:18:in `block (2 levels) in <main>'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `execute'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:243:in `block in invoke_prerequisites'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `each'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `invoke_prerequisites'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:218:in `block in invoke_with_call_chain'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:188:in `invoke'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:160:in `invoke_task'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `each'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block in top_level'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:125:in `run_with_threads'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:110:in `top_level'
/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/commands/rake/rake_command.rb:24:in `block (2 levels) in perform'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/commands/rake/rake_command.rb:24:in `block in perform'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/rake_module.rb:59:in `with_application'
/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/command.rb:51:in `invoke'
/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/commands.rb:18:in `<main>'
/app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
/app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
/app/bin/rails:4:in `<main>'
Tasks: TOP => db:drop => db:check_protected_environments

Many thanks in advance!

Brenna S
  • 1
  • 1
  • I suggest having a look at the answers to this question: https://stackoverflow.com/questions/12472988/postgresql-error-could-not-connect-to-server-no-such-file-or-directory?rq=1 – spickermann Oct 03 '22 at 07:46
  • Remove `url: <%= ENV["DATABASE_URL"] %>` from your `database.yml`. Rails will already use `ENV["DATABASE_URL"]` to configure the database configuration and it has priority over the settings from the config file. Hardcoding the username and password are also a huge no-no. If those are the actual credentials **you need to change them immediately** and investigate if you have been breached. https://guides.rubyonrails.org/configuring.html#configuring-a-database – max Oct 03 '22 at 09:54
  • Oh... my... gosh... Well, the issue was that someone else working on the repo added a method inside of the rails `environment.rb` file which pointed to another file that had some custom logic that told rails what database it should use and **that** was breaking things. Two days of troubleshooting this. Good times. Thank you all for your help - it ultimately helped me keep investigating and led me to the problem. – Brenna S Oct 03 '22 at 17:53

1 Answers1

0

Here are couple of steps you can try:

  1. Can you confirm if the database is added to your application? You can verify with below:

    heroku config --app your_app_name
    

    If not, you can create with the following

    heroku addons:create heroku-postgresql
    
  2. Here is how you can connect with DB with via DATABASE_URL ENV Variable.. Do look in this answer for more information, it might be useful

Taimoor Hassan
  • 365
  • 2
  • 11
  • The database is added to the application. Heroku automatically sets (and regularly changes for security purposes) the `DATABASE_URL` ENV value, so I usually don't think I need to mess with much there. And as I was digging into your suggestions and the issue more I **finally** found the problem... Someone else working on the repo added some custom logic in the rails `environment.rb` file that told the program what db to use and it was wrong and that is what was breaking things. – Brenna S Oct 03 '22 at 17:56
  • Glad that you were able to find and resolve the issue. Do Upvote my answer if you think my answer helped you in anyway :). – Taimoor Hassan Oct 04 '22 at 08:09