【问题标题】:How come my code and postgres are not connecting为什么我的代码和 postgres 没有连接
【发布时间】:2019-12-28 07:54:26
【问题描述】:

我正在尝试将我的 database.yml 文件更改为 postgresql。 这是我之前的问题Changing sqlite to pg 的扩展 希望有人能帮忙

我已将我的 database.yml 文件更改为我的问题答案

这是我的 database.yml 文件

connection: &connection
adapter: postgresql
host: localhost
port: 5432
username: postgres
password: postgres
pool: 5
timeout: 5000
min_messages: warning

development:
  <<: *connection
  database: development_db_name
test:
  <<: *connection
 database: test_db_name

production:
  <<: *connection
  database: production_db_name

这是准确的缩进

我得到了:

ActiveRecord::ConnectionNotEstalblished
No connection pool with 'primary' found



vendor/cache/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1013:in `retrieve_connection'
vendor/cache/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
vendor/cache/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:90:in `connection'
vendor/cache/gems/activerecord-5.2.3/lib/active_record/migration.rb:554:in `call'
vendor/cache/gems/actionpack-5.2.3/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
vendor/cache/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:98:in `run_callbacks'
vendor/cache/gems/actionpack-5.2.3/lib/action_dispatch/middleware/callbacks.rb:26:in `call'
vendor/cache/gems/actionpack-5.2.3/lib/action_dispatch/middleware/executor.rb:14:in `call'
vendor/cache/gems/actionpack-5.2.3/lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
vendor/cache/gems/web-console-3.7.0/lib/web_console/middleware.rb:135:in `call_app'
vendor/cache/gems/web-console-3.7.0/lib/web_console/middleware.rb:30:in `block in call'
vendor/cache/gems/web-console-3.7.0/lib/web_console/middleware.rb:20:in `catch'
vendor/cache/gems/web-console-3.7.0/lib/web_console/middleware.rb:20:in `call'
vendor/cache/gems/actionpack-5.2.3/lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
vendor/cache/gems/railties-5.2.3/lib/rails/rack/logger.rb:38:in `call_app'
vendor/cache/gems/railties-5.2.3/lib/rails/rack/logger.rb:26:in `block in call'
vendor/cache/gems/activesupport-5.2.3/lib/active_support/tagged_logging.rb:71:in `block in tagged'
vendor/cache/gems/activesupport-5.2.3/lib/active_support/tagged_logging.rb:28:in `tagged'
vendor/cache/gems/activesupport-5.2.3/lib/active_support/tagged_logging.rb:71:in `tagged'
vendor/cache/gems/railties-5.2.3/lib/rails/rack/logger.rb:26:in `call'
vendor/cache/gems/sprockets-rails-3.2.1/lib/sprockets/rails/quiet_assets.rb:13:in `call'
vendor/cache/gems/actionpack-5.2.3/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
vendor/cache/gems/actionpack-5.2.3/lib/action_dispatch/middleware/request_id.rb:27:in `call'
vendor/cache/gems/rack-2.0.7/lib/rack/method_override.rb:22:in `call'
vendor/cache/gems/rack-2.0.7/lib/rack/runtime.rb:22:in `call'
vendor/cache/gems/activesupport-5.2.3/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
vendor/cache/gems/actionpack-5.2.3/lib/action_dispatch/middleware/executor.rb:14:in `call'
vendor/cache/gems/actionpack-5.2.3/lib/action_dispatch/middleware/static.rb:127:in `call'
vendor/cache/gems/rack-2.0.7/lib/rack/sendfile.rb:111:in `call'
vendor/cache/gems/railties-5.2.3/lib/rails/engine.rb:524:in `call'
vendor/cache/gems/puma-3.12.1/lib/puma/configuration.rb:227:in `call'
vendor/cache/gems/puma-3.12.1/lib/puma/server.rb:660:in `handle_request'
vendor/cache/gems/puma-3.12.1/lib/puma/server.rb:474:in `process_client'
vendor/cache/gems/puma-3.12.1/lib/puma/server.rb:334:in `block in run'
vendor/cache/gems/puma-3.12.1/lib/puma/thread_pool.rb:135:in `block in spawn_thread'

那是我的全部踪迹

【问题讨论】:

  • 首先你必须从你的 gem 文件中删除 gem "sqlite3",然后确保你有 "pg" gem,运行 bundle install 并重启服务器
  • 我确实删除了 sqlite3,但这并没有改变任何东西

标签: ruby-on-rails ruby database postgresql sqlite


【解决方案1】:

确保您已在本地环境中安装了 postgresql,并且在打开您的网络应用程序之前已启动它

删除 sqlite gem,并将 gem 'pg' 添加到您的 Gemfile。

对于 postgresql 数据库配置,下面是我的,它可以工作


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

development:
  <<: *default
  database: my_app_development

test:
  <<: *default
  database: my_app_test

production:
  <<: *default
  database: my_app_production
  username: freelancer
  password: <%= ENV['FREELANCER_DATABASE_PASSWORD'] %>

【讨论】:

    【解决方案2】:

    像这样重新缩进你的代码 connection: &connection adapter: postgresql host: localhost port: 5432 username: postgres password: postgres pool: 5 timeout: 5000

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多