【问题标题】:Rails error when firing up a cloned API - ActiveRecord::ConnectionNotEstablished启动克隆 API 时出现 Rails 错误 - ActiveRecord::ConnectionNotEstablished
【发布时间】:2022-11-01 11:21:54
【问题描述】:

正如标题所说,我克隆了一个 Rails API。从第 2 点开始,我尝试按照本文中的步骤进行操作 https://dev.to/w3ndo/a-checklist-for-setting-up-a-cloned-rails-application-locally-5468,但从 db:setup 开始,我一直收到相同的错误。

请帮忙!

我试过用谷歌搜索答案并给朋友打电话。

我尝试过 rails db:setup、rails db:seed、rails db:create、rails db:migrate。

【问题讨论】:

  • 您的计算机上是否安装了 Postgres 数据库实例并在端口 5432(pg 的常用端口)上运行?基本上它只是说它无法连接数据库

标签: ruby-on-rails postgresql api ruby-on-rails-5


【解决方案1】:

你想初始化 postgres 数据库,它不是免费的。我建议在您需要生产数据库之前使用 sqlite3。如果克隆调用 PG,则:

  1. (用户)$sudo su - postgres
  2. (postgres) $createuser --interactive
  3. (本地)$sudo systemctl restart postgresql
  4. (本地)$bundle exec rails db:create:all

    $pg_isready 一目了然地告诉你 postgres 服务器/集群是否在线。

    $ pg_isready
    /tmp:5432 - accepting connections
    

    如果它变得令人沮丧,请将config/database.yml 更改为默认版本,如果可能,删除 pg gem,添加 sqlite3。然后在创建或添加 [environment].sqlite3 文件到 db/ 后简单 rake db:migrate

    #   gem install sqlite3
    #
    #   Ensure the SQLite 3 gem is defined in your Gemfile
    #   gem 'sqlite3'
    #
    default: &default
      adapter: sqlite3
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
      timeout: 5000
    
    development:
      <<: *default
      database: db/development.sqlite3
    
    # Warning: The database defined as "test" will be erased and
    # re-generated from your development database when you run "rake".
    # Do not set this db to the same as development or production.
    test:
      <<: *default
      database: db/test.sqlite3
    
    production:
      <<: *default
      database: db/production.sqlite3
    
    
    
    

【讨论】:

  • 感谢 Tidelake,我正在与一个使用 postgres 创建后端的合作伙伴一起开展这个项目。我克隆了他的仓库。我切换到 SQLite 会给他带来问题吗?我在其他地方得到的一些建议是“在谷歌上搜索第一个错误“rails db setup library not loaded libpq.5”时,看起来因为你已经重新安装了postgresql,你需要卸载并重新安装pg gem:”但是这对我也不起作用
  • 使用:git checkout -b local 创建本地开发分支,然后如果需要,您可以将 database.yml 添加到本地 .gitignore 文件中
【解决方案2】:

更新

感谢您的帮助。当我的合作伙伴创建了后端时,我发现问题是因为我克隆他的仓库时数据库的所有者不匹配而出现的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    相关资源
    最近更新 更多