最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ruby - ActiveRecord::ConnectionNotEstablished tries local PostgreSQL socket instead of Heroku DB - Stack Overflow

programmeradmin4浏览0评论

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

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

Share Improve this question asked 6 hours ago jordy Bolo Caldasjordy Bolo Caldas 191 silver badge1 bronze badge New contributor jordy Bolo Caldas is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 0

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.

发布评论

评论列表(0)

  1. 暂无评论