rubypostgresqlherokuheroku-postgresruby-on-rails-8

ActiveRecord::ConnectionNotEstablished tries local PostgreSQL socket instead of Heroku DB


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


Solution

  • 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.