1

I have a Rails 8.0.1 application (Ruby 3.3.4) that I’m deploying to Heroku. The code pushes successfully, but whenever I run: heroku run rails db:migrate

I get this error:

ActiveRecord::ConnectionNotEstablished: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed

It seems Rails is trying to connect via a local PostgreSQL socket instead of using Heroku’s Postgres add-on. Locally, everything works fine with rails db:migrate. On Heroku, I already ran:

heroku addons:create heroku-postgresql:essential-0

…and the add-on was created successfully. My Gemfile includes:

gem "rails", "~> 8.0", ">= 8.0.1"
gem "pg", "~> 1.5"
# ...

Here’s my database.yml:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: brikex2_development

test:
  <<: *default
  database: brikex2_test

production:
  primary: &primary_production
    <<: *default
    database: brikex2_production
    username: brikex2
    password: <%= ENV["BRIKEX2_DATABASE_PASSWORD"] %>
  cache:
    <<: *primary_production
    database: brikex2_production_cache
    migrations_paths: db/cache_migrate
  queue:
    <<: *primary_production
    database: brikex2_production_queue
    migrations_paths: db/queue_migrate
  cable:
    <<: *primary_production
    database: brikex2_production_cable
    migrations_paths: db/cable_migrate

I want help how to properly configure my project to solve the issue

1 Answer 1

0

Note: This answer is likely a workaround, and not a definite solution. Please read the bottom Edit: section for details.

Heroku exposes the details how to connect to its database by setting DATABASE_URL ENV variable. You can access it using:

  primary: &primary_production
    <<: *default
    url: <%= ENV['DATABASE_URL'] %>

You don't need database:, nor password: because it's included in the DATABASE_URL which looks like postgres://[username]:[password]@[DOMAIN]:[PORT]/[DATABASE_NAME]

This is called a DB connection string and is mostly standard (or very similar) between different database engines.

You'll need to get comfortable with the idea of ENV variables because not only platforms like Heroku use them extensively, but it's a standard for configuring web applications in general: https://12factor.net/config

When you attach Redis to your Heroku, you'll REDIS_URL env and so on.

Edit:

as commenter pointed below, both the config.yml and ENV['DATABASE_URL'] are already "known" to the Rails framework (I did not know about it, I always assumed DATABASE_URL is an arbitrary Heroku's choice)

Reading through Connection Preference documentation suggests that DATABASE_URL always takes precedence over values specified in the database/config.yml file. But this seems not to match what OP is experiencing. Possibly deeper investigation is needed.

Sign up to request clarification or add additional context in comments.

3 Comments

Interpolating ENV['DATABASE_URL'] into database.yml in this way is totally pointless. It's merged with and takes priority over the file based config anyways. The only thing you're actually doing here is overriding ENV['DATABASE_URL'] with the URL in ENV['DATABASE_URL']. guides.rubyonrails.org/configuring.html#configuring-a-database
@max Honest question for someone with much bigger creds: Is this community a place to help each other, or a place for only Platonic ideal solutions? My answer might not be a perfect solution, could be just a lousy workaround (edited the answer to make that clear, thanks for taking the time to share your thoughts) but it's evidetnly not pointless; It changed something in the OPs situation. Maybe there's a merge issue between yml and DATABASE_URL, maybe there's a bug or the docs are not precise? Are workarounds not allowed here?
This solution 100% worked for me. Rail 8.0.2, Postgres, Heroku. Added the one line and it's working. Thank you @Greg!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.