Skip to content

can't run migrations with external databases #1513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
michelson opened this issue Apr 21, 2025 · 4 comments
Open

can't run migrations with external databases #1513

michelson opened this issue Apr 21, 2025 · 4 comments

Comments

@michelson
Copy link

michelson commented Apr 21, 2025

Hello, I have my Kamal deployment up and running on the server, but I'm having trouble running migrations or preparing the app.

I am using an external database hosted on a managed server with Digital Ocean, and I have the following configuration set up:

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

production:
  primary: &primary_production
    <<: *default_production
    url: <%= ENV['DATABASE_URL'] %>
  queue:
    <<: *primary_production
    url: <%= ENV['DATABASE_QUEUE_URL'] %>
    migrations_paths: db/queue_migrate
  cable:
    <<: *primary_production
    url: <%= ENV['DATABASE_CABLE_URL'] %>
    migrations_paths: db/cable_migrate
  cache:
    <<: *primary_production
    url: <%= ENV['DATABASE_CACHE_URL'] %>
    migrations_paths: db/cache_migrate

The issue I'm facing is that the app seems to attempt connecting to a local database when I run the commands rails db:migrate or rails db:prepare. However, the server operates normally. I can also access the Rails console without any problems. When I enter the console using bash and run rails c, everything works fine. Yet, when I execute rails db:migrate, I encounter an issue with the server.

➜  pullentity3 git:(main) kamal app exec -i bash

Get most recent version available as an image...
Launching interactive command with version latest via SSH from new container on XX.XXX.XX.27...

>>>> DATABASE_URL is: postgresql://doadmin:[email protected]:25060/defaultdb?sslmode=require

rails@c8a2834e78fe:/rails$ bundle exec rails db:migrate
bin/rails aborted!
ActiveRecord::ConnectionNotEstablished: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory (ActiveRecord::ConnectionNotEstablished)
        Is the server running locally and accepting connections on that socket?


Caused by:
PG::ConnectionBad: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory (PG::ConnectionBad)
        Is the server running locally and accepting connections on that socket?

Tasks: TOP => db:migrate
(See full trace by running task with --trace)
rails@c8a2834e78fe:/rails$ rails c
bash: rails: command not found

THIS ON THE SAME SSH SESSION

rails@c8a2834e78fe:/rails$ bundle exec rails c
To use retry middleware with Faraday v2.0+, install `faraday-retry` gem
/rails/app/services/github.rb:3: warning: /usr/local/lib/ruby/3.3.0/csv.rb was loaded from the standard library, but will no longer be part of the default gems starting from Ruby 3.4.0.
You can add csv to your Gemfile or gemspec to silence this warning.
Loading production environment (Rails 8.0.2)
pullentity(prod)> Site.first
=> 
#<Site:0x000077d6693661b0
 id: 1,
 user_id: 1,
 name: "michelsongs",
 custom_domain: "",
 subdomain: "michelsongs",
 slug: "michelsongs",
 password: nil,
 created_at: "2012-04-14 17:14:32.000000000 +0000",
 updated_at: "2025-04-21 01:28:33.530618000 +0000",
 description: "",
 keywords: "",
 analytics: "",
 activated: true,
 fb_app: nil,
 screenshot: "open-uri20180211-28876-1l2q4sz",

Super weird, what could be happening here?

@djmb
Copy link
Collaborator

djmb commented Apr 21, 2025

It's trying to connect to a local Postgres DB - maybe RAILS_ENV is not set correctly?

Does this work?

RAILS_ENV=production bundle exec rails db:migrate

@michelson
Copy link
Author

It's trying to connect to a local Postgres DB - maybe RAILS_ENV is not set correctly?

Does this work?

RAILS_ENV=production bundle exec rails db:migrate

RAILS_ENV is set correctly. As I mentioned, when I enter the Rails console, I can access ActiveRecord, but when I run the' rails db: migrate' command, I encounter an issue.

@jorgefgiron
Copy link

Try prefixing your commands with dotenv, I had a similar issue and this solved it.

dotenv kamal app exec -i bash

@michelson
Copy link
Author

@jorgefgiron thanks but that does not work for me as I do not use .env file

look this example:

ENTER CONSOLE:

kamal app exec -i bash

EXEC DB: COMMAND
rails@07c79c748b0f:/rails$ bundle exec rails db:migrate

FAILS 🔴

To use retry middleware with Faraday v2.0+, install `faraday-retry` gem
bin/rails aborted!
ActiveRecord::ConnectionNotEstablished: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory (ActiveRecord::ConnectionNotEstablished)
        Is the server running locally and accepting connections on that socket?


Caused by:
PG::ConnectionBad: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory (PG::ConnectionBad)
        Is the server running locally and accepting connections on that socket?

### GET THE ENV

rails@07c79c748b0f:/rails$ echo $RAILS_ENV
production

## ENTER CONSOLE

rails@07c79c748b0f:/rails$ bundle exec rails c

To use retry middleware with Faraday v2.0+, install `faraday-retry` gem
/rails/app/services/github.rb:3: warning: /usr/local/lib/ruby/3.3.0/csv.rb was loaded from the standard library, but will no longer be part of the default gems starting from Ruby 3.4.0.
You can add csv to your Gemfile or gemspec to silence this warning.
Loading production environment (Rails 8.0.2)
pullentity(prod)> User.first
=> #<User id: 1, first_name: "Miguel", ....>

pullentity(prod)> SolidQueue::Job.last
=> 
#<SolidQueue::Job:0x00007bc925873038
 id: 88,
 ....
 created_at: "2025-04-26 01:37:31.268929000 +0000",
 updated_at: "2025-04-26 01:37:58.325551000 +0000">

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants